Humanoid.Running event isn't firing correctly, (and I can't check if an animation is already playing properly)

Hello!

  1. 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.

  1. 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)
image

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.

  1. 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
animationreseting
(instead of having the katana stay at his waist)
.
.
.
Basically, what I’m looking for, is

  1. A way to properly detect changes in the speed of a character so I can play an animation accordingly.
  2. 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)

OK so I’m back.
I have an update on this.
One of the problem was on my end, on the if-elseif statement.

Because I was checking if the speed was over 30 (which is ok)
but then I was checking if the speed was over 10 (and only over 10).
It worked fine until I added a second condition to check if the animation is currently playing.
The thing is that because I was only checking if the speed was over 10, the first condition would not execute, but the second one would, and because the instructions in the second condition would execute, the first one would execute, thus making a loop between my walking animation and the running animation. The solution is to check if the speed is over 10, AND under 30, and not only over 10.

But I’m still left with the fact that I’m checking the speed in a loop, rather than depending on an event. Which I think is “bad” performance-wise.
It’s not noticeable yet, but it might cause me some problem in the future. (I think? maybe someone with more exeperience about that could tell me. I’m using the Heartbeat event for that “loop” (yeah that’s actually not a real loop))

I’ve helped someone with something like this before. What I ended up doing was editing ROBLOX’s Animate script. I believe this is the best way to handle this. The way the code works is that it plays another animation (“sprint” animation) if your WalkSpeedis greater than the default WalkSpeed. I can easily see you implement multiple speed thresholds using the code I wrote.

Here’s a screenshot from a video I posted. Note that in the code there are rounding errors with the speed variable which I properly did not handle. if speed > defaultWalkSpeed + 1 then should probably be if math.round(speed) > defaultWalkSpeed then.

https://devforum.roblox.com/t/i-need-to-improve-on-stamina-sprinting-system/842562/7?u=hamsterloverboy3

2 Likes

Hey, thank you for your answer!
I’m tinkering with the Animate script right now, trying to get rid of all the unnecessary stuff like emotes, etc.

I’ve managed to put my own walking and running animations and it’s sooooo quick to detect changes, this really feels good, I’ll set your answer as the solution.
But I do have one quick question, do the animations replicate automatically onto the server/other clients? I’d guess yes because that’s the default animation script and it would make sense since it was already replicating before my own scripts, but I didn’t see any code that tells the server to replicate yet, so I’m asking.

Anyway thank you again, it feels much more reactive now!

The animations do automatically replicate to the server/other clients. The reason you don’t see any code where the client is talking with the server is because certain things replicate. I personally have not been able to find a list when ever I tried to search for what replicates. ROBLOX doesn’t have a good track record of having professional documentation, clearly the interns are writing it.

What I know for sure replicates are sounds, animations and physics. I don’t know what else does.

2 Likes