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

How can I check if the player is not moving?

Hey. I'm doing a crouch system and I want to have 2 animations for it: one that plays while the player is not moving and one that plays while the player is moving. I've checked HumanoidStateType and there is a state called running, but nothing about standing/idle. My script doesn't run the moving animation, just the standing one. This is what I have so far:

local UserInputService = game:GetService("UserInputService")
local character = game.Players.LocalPlayer.Character
local humanoid = character.Humanoid

animation = Instance.new("Animation")
animation.Name = "Moving"
animation.AnimationId = "http://roblox.com/asset/?id=2163761498"
animTrack = humanoid:LoadAnimation(animation)

standinganim = Instance.new("Animation")
standinganim.Name = "Standing"
standinganim.AnimationId = "http://roblox.com/asset/?id=1293954626"
standinganimTrack = humanoid:LoadAnimation(standinganim)

UserInputService.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.C then
		if humanoid.StateChanged == Enum.HumanoidStateType.Running then
			animTrack:Play()
			print("moving") --this doesn't work so it doesn't print "moving"
		else
			animTrack:Stop()
			standinganimTrack:Play()
			print("not moving") --This prints "not moving" even when I'm moving
		end
	end
end)

UserInputService.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.C then
		animTrack:Stop()
		standinganimTrack:Stop()
	end
end)

I assume that the Running State is not the state I am looking for so... help?

1 answer

✓ Accepted answer

There's an event called Running,which will fire everytime a humanoid changes their speed.

Also, what you're gonna have to do is create a variable 'isCrouching' to tell whether the player is crouching or not. When the player presses the 'C' key, set 'isCrouching' to true, when the player releases the 'C' key, set 'isCrouching' to false

humanoid.Running:Connect(function(speed)
        if speed > 0 and isCrouching then
               animTrack:Play()
               standinganimTrack:Stop()
        elseif speed <= 0 and isCrouching then
               animTrack:Stop()
               standinganimTrack:Play()
        end
end)

1 comment

0

Forgot an equal. It works :D Thanks!

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