Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I check if the player is not moving?

Asked by 6 years ago

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

Log in to vote
-1
Answered by 6 years ago
Edited 6 years ago

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)
0
Forgot an equal. It works :D Thanks! bruceywayne 35 — 6y
Ad

Answer this question