Hello!
- What do you want to achieve?
I would like to change the animation of a character based on its speed. If he’s moving slowly (say less than 20 WalkSpeed), I want to play a “walking” animation, if he’s running (more than 20 WalkSpeed) I want to play a “running” animation. And an “idling” animation if he’s not walking (I guess 0 WalkSpeed?). I do have these 3 animations and I am able to play them individually.
- What is the issue?
The issue is that the Humanoid.Running event isn’t firing correctly, sometimes, when I run and then stop, the following code won’t print the final “0”, and thus my code won’t stop playing the “walking” animation for my character. Which is quite troublesome…
character.rbx.Humanoid.Running:Connect(function(speed)
print(speed)
if speed > 30 then
character.AnimationTracks["Idle"]:Stop()
character.AnimationTracks["Walking"]:Stop()
character.AnimationTracks["Running"]:Play()
elseif speed > 0 then
character.AnimationTracks["Idle"]:Stop()
character.AnimationTracks["Running"]:Stop()
character.AnimationTracks["Walking"]:Play()
elseif speed == 0 and character.EquippedTool == character.Tools.Katana then
character.AnimationTracks["Running"]:Stop()
character.AnimationTracks["Walking"]:Stop()
character.AnimationTracks["Idle"]:Play()
else
character.AnimationTracks["Running"]:Stop()
character.AnimationTracks["Walking"]:Stop()
character.AnimationTracks["Idle"]:Stop()
end
end)
(yes the code works, character is just a custom object wrapper of the “official” roblox’s character object)
Another problem is it takes a bit of time to react to a change in speed, I can’t really show you but the character will sometimes start moving but no animation will be played for a really short time (like half a second to a whole second, but it’s enough to be noticeable). Most likely due to how the event fires.
- What solutions have you tried so far?
• I’ve tried setting a threshold (10) for the speed, that way, even if the last recored speed is 2.313 like in the last example, the idling animation will play. But that’s kind of a hacky way of doing things, plus in very rare case the last recorded speed will be over 10…
• I’ve tried putting that code in a loop and checking myself the HumanoidRootPart’s velocity. That’s hacky too, but it detects speed quite well (well I still do have a threshold, but at least I know it won’t be higher than 3 or any crazy value). However, this will reset my animation to the first frame of the animation every loop. Which is, again, quite troublesome…
• For that last solution I’ve tried to check if AnimationTrack.IsPlaying is true or false, and it does let the animation play for a bit, but sometimes it will immediatly reset the animation to frame 0, sometimes not. So, yeah, this doesn’t work either
(instead of having the katana stay at his waist)
.
.
.
Basically, what I’m looking for, is
- A way to properly detect changes in the speed of a character so I can play an animation accordingly.
- Depending of the first solution, a way to avoid reseting the animation frame to 0 properly (reseting to frame 0 like when you :Play() an animation every 20ms)