So, I am making a map in which sprinting would ruin the objective. I looked for about an hour and couldn't find anything about how to do it other than keeping a player's hunger down (which didn't work for me) and I felt that was not very aesthetically pleasing anyhow. So after some testing I figured it out.
First you must create a scoreboard to check if the player is sprinting:
Once you have this setup you can test if a player is sprinting by putting the following command in a command block: (On a clock or a Repeating Command Block)
testfor @p[score_Sprinting_min=1]
--Note: This only activates for one player, the one who is sprinting nearest the command block running the command.
This can then run to another command block (Through comparator or Chain Command Block) that stops their movement for one second:
Nice solution. I also needed to disable sprinting (though only in certain situations), but instead of giving the player slowness, I just teleport them to where they are already (with /tp <target player> @p). That magically slows the player down, somehow. It makes sprinting slower than walking, although it also makes it a little janky. I wanted to make sprinting the same as walking, but at least it doesn't completely disable movement.
That's how I'm doing it right now, anyway. I might consider switching to instead using just enough slowness to make sprinting the same or less speed as walking.
By the way, you can reduce your method to two command blocks by doing the test in the same block you apply the effect:
This also solves the problem of your commands only running for the player nearest the blocks because they run for all players. If there was a player sprinting, but there was a player nearer to the command blocks who was not sprinting, the player who's nearer would be penalized with not moving instead of the sprinting player because you only use @p in your effect application, which will target the nearest player to the blocks with no regard to who was actually sprinting. (Depending on the number of players and the layout of the command blocks in your map, this might not be a problem, but…I solved it anyway ).
Nice solution. I also needed to disable sprinting (though only in certain situations), but instead of giving the player slowness, I just teleport them to where they are already (with /tp <target player> @p). That magically slows the player down, somehow. It makes sprinting slower than walking, although it also makes it a little janky. I wanted to make sprinting the same as walking, but at least it doesn't completely disable movement.
That's how I'm doing it right now, anyway. I might consider switching to instead using just enough slowness to make sprinting the same or less speed as walking.
By the way, you can reduce your method to two command blocks by doing the test in the same block you apply the effect:
This also solves the problem of your commands only running for the player nearest the blocks because they run for all players. If there was a player sprinting, but there was a player nearer to the command blocks who was not sprinting, the player who's nearer would be penalized with not moving instead of the sprinting player because you only use @p in your effect application, which will target the nearest player to the blocks with no regard to who was actually sprinting. (Depending on the number of players and the layout of the command blocks in your map, this might not be a problem, but…I solved it anyway ).
Yeah, I see what you are saying. I actually tried the teleport to self method as well; however, for what I needed that didn't work so I just reset the player for the level they are on. and for tutorial purposes I decided to show it with slowness. For your case I believe slowness 1 or 2 will make sprinting the same speed as walking.
I suggest your method for most people. So, note to others, adjust the method I said to this one for better application. I personaly am making a single player map and had no need to detect all players.
So, I am making a map in which sprinting would ruin the objective. I looked for about an hour and couldn't find anything about how to do it other than keeping a player's hunger down (which didn't work for me) and I felt that was not very aesthetically pleasing anyhow. So after some testing I figured it out.
First you must create a scoreboard to check if the player is sprinting:
Next add all players to this scoreboard:
Once you have this setup you can test if a player is sprinting by putting the following command in a command block: (On a clock or a Repeating Command Block)
--Note: This only activates for one player, the one who is sprinting nearest the command block running the command.
This can then run to another command block (Through comparator or Chain Command Block) that stops their movement for one second:
--Note: "2" is the Potion ID for "Slowness."
Nice solution. I also needed to disable sprinting (though only in certain situations), but instead of giving the player slowness, I just teleport them to where they are already (with /tp <target player> @p). That magically slows the player down, somehow. It makes sprinting slower than walking, although it also makes it a little janky. I wanted to make sprinting the same as walking, but at least it doesn't completely disable movement.
That's how I'm doing it right now, anyway. I might consider switching to instead using just enough slowness to make sprinting the same or less speed as walking.
By the way, you can reduce your method to two command blocks by doing the test in the same block you apply the effect:
This also solves the problem of your commands only running for the player nearest the blocks because they run for all players. If there was a player sprinting, but there was a player nearer to the command blocks who was not sprinting, the player who's nearer would be penalized with not moving instead of the sprinting player because you only use @p in your effect application, which will target the nearest player to the blocks with no regard to who was actually sprinting. (Depending on the number of players and the layout of the command blocks in your map, this might not be a problem, but…I solved it anyway ).
Yeah, I see what you are saying. I actually tried the teleport to self method as well; however, for what I needed that didn't work so I just reset the player for the level they are on. and for tutorial purposes I decided to show it with slowness. For your case I believe slowness 1 or 2 will make sprinting the same speed as walking.
I suggest your method for most people. So, note to others, adjust the method I said to this one for better application. I personaly am making a single player map and had no need to detect all players.