Problem:
Basically, I have a sprint script which works very well but whenever I hold the LeftShift key the animation starts playing when the player is standing still which is something I’m not going for. Example: - GIF - Imgur
Things I tried:
Detecting when player is holding the W key (didn’t work well)
Detecting player velocity (did not work at all or maybe I did something wrong)
Detecting if the HumanoidStateType is idle (there’s no idle state)
Code:
Too many lines, so I uploaded it: Script.lua (2.5 KB)
if (humanoid.MoveDirection:Dot(hrp.MoveDirection > 0) then
-- player is moving, continue
end
-- a shorter, but to my knowledge slightly more performance demanding alternative:
if (humanoid.MoveDirection.Magnitude > 0) then
-- player is moving, continue
end
It’s used when checking whether humanoid’s velocity is higher than zero or not.
Humanoid.Running event fires whenever the players movement speed changes, so it can tell you if the player is moving or not.
Humanoid.Running:Connect(function(speed)
if speed <= 0 then
-- Player is not moving
RunAnim:Stop()
elseif IsRunning and not RunAnim.IsPlaying then
-- for when the player continues running after stopping
RunAnim:Play()
end
end)
@20amir02’s Humanoid.Running can be very useful and helps you get the walk speed, but is not so practical in this case, because there is an unnecessary connection, which is more expensive than dot product checks. However, same logic applies, and it’s nice that you mentioned it.
Wait so, where would I implement this on the script?
Edit: Nevermind, if sprint.KeyCode == Enum.KeyCode.LeftShift and Humanoid.MoveDirection.Magnitude > 0 then worked.