Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

humanoid.Running() firing at annoying times?

humanoid.Running:connect(function(speed)
	if speed > 2 then
		print(speed)
		walking = true
	else
		walking = false
		humanoid.WalkSpeed = 10
	end
end)

This is the code I'm using to detect if a player is walking. This works, but when a player jumps, the .Running event fires again. When the player lands, the event fires 3 more times. How can I make it so that the event isn't fired again when the player jumps? And how can I make it so that the event doesn't fire rapidly when they land(add a debounce?), or better yet, not at all?

1 answer

Unfortunately, you can't prevent the event from firing in specific circumstances unless you can deactivate the event and reconnect it later (which isn't helpful here).

Fortunately, you don't need to prevent it from firing (based on the code you've provided). The worst that happens is that your code runs a few extra times - but the important thing is that it doesn't make any of the values wrong ("walking" will still be correct).

Note that adding debounce to save processing time doesn't work in this case: just checking for debounce would often double the time it takes for Roblox to execute your function!

However, say you only wanted some code to execute if the player's walking status changed. In that case, do:

humanoid.Running:connect(function(speed)
	local newWalking = speed > 2
	if newWalking == walking then return end --no change
	walking = newWalking
	--Walking has changed, add code to deal with that here
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.