How can I detect if a the local Character is moving or standing still?

I’ve been trying to figure out how to play a gun walk animation when the player is moving and stop it when he is not moving. This has been very difficult, I’ve tried different methods like UserInputBegan and UserinputEnded but that glitched the animation, and I tried Character.PrimaryPart.LinearVelocity.Magnitude but that was also glitchy. I just want to know a simple way I can detect if the character is moving or not.

4 Likes

You can detect whether the player is moving or not by using MoveDirection.

It still doesn’t work when I try to play the animation with it
It printed “Stopped” Once and then it stopped working

if input.KeyCode == Enum.KeyCode.W and IsEquipped.Value == true   then

		if Humanoid.MoveDirection.Magnitude > 0 then -- Is the character walking?

			gunWalkAnim:Play()
			print("Test")
		else

			gunWalkAnim:Stop()
			print("Stopped")
		end
	end
	
	```
1 Like

When you made the animation was it looped? If so, make it so its not looped.

1 Like

Yeah it is a looped walk animation that plays smoothly. If I unloop it wont it stop after the first time?

1 Like

I am not good when it comes to animation but try seeing what it does when you untick looped and make sure to overwrite it so it makes a change.

1 Like

I recorded a video of the problem. It seems like it sort of working but the animation is not being played all the way

Video:Animation bugging when moving - YouTube

2 Likes

Interesting. I remember having this issue once. Sorry but I’m not too sure, I don’t have enough knowledge when it comes to animating but I am sure someone else may be able to assist.

1 Like

Alright its fine dont worry about it.

1 Like

Did it print Test ? Or everytime is just “Stopped”

Actually, one last thing, maybe set the looped value to true when the character is walking and then set it to false when the character is not?

Is the animation made by you ?
if no, try playing in Roblox Player, it might work there

This is not the best way to do it, you can instead use this,

game.Player.LocalPlayer.Character.Humanoid.StateChanged:Connect(function()
   if IsEquipped.Value then
      if Humanoid.MoveDirection.Magnitude > 0 then -- Is the character walking?

			gunWalkAnim:Play()
			print("Test")
		else

			gunWalkAnim:Stop()
			print("Stopped")
		end
   end
end)
5 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.