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

Run animation plays while standing still fix?

How would I make it so you could only run when you are moving?

wait(1)
local UIS = game:GetService("UserInputService")
local p = game:GetService("Players").LocalPlayer
local c  = p.Character or p.CharacterAdded:Wait()
local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://4588222471"
local RunAnimation = c:WaitForChild("Humanoid"):LoadAnimation(anim)
UIS.InputBegan:Connect(function(i,a)
    if not a then
        if i.KeyCode == Enum.KeyCode.LeftShift then
            c:WaitForChild("Humanoid").WalkSpeed = 20
            RunAnimation:Play()
        end
    end
end)

UIS.InputEnded:Connect(function(i,a)
    if not a then
        if i.KeyCode == Enum.KeyCode.LeftShift then
            c:WaitForChild("Humanoid").WalkSpeed = 13
            RunAnimation:Stop()
        end
    end
end)

2 answers

There's nothing wrong with your code. Try setting the animation's priority to "Action".

1 comment

0

No I mean if you stand still and press shift it still plays altough you're not moving

You should check if the player is moving, and add that into your if statement!

local velocity = math.ceil(c.HumanoidRootPart.Velocity.magnitude)

Check if velocity > 3 (means they're actively moving), and then allow the sprint.

wait(1)
local UIS = game:GetService("UserInputService")
local p = game:GetService("Players").LocalPlayer
local c  = p.Character or p.CharacterAdded:Wait()
local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://4588222471"
local RunAnimation = c:WaitForChild("Humanoid"):LoadAnimation(anim)
UIS.InputBegan:Connect(function(i,a)
    if not a then
        local velocity = math.ceil(c.HumanoidRootPart.Velocity.magnitude)
        if i.KeyCode == Enum.KeyCode.LeftShift and (velocity > 3) then
            c:WaitForChild("Humanoid").WalkSpeed = 20
            RunAnimation:Play()
        end
    end
end)

UIS.InputEnded:Connect(function(i,a)
    if not a then
        if i.KeyCode == Enum.KeyCode.LeftShift then
            c:WaitForChild("Humanoid").WalkSpeed = 13
            RunAnimation:Stop()
        end
    end
end)

7 comments

0

Wouldn't it be easier to check the HumanoidStateType?

0

Which if statement would I put it in?

0

You would put it into the if statement on line 10. You could also check the humanoidstatetype to be fair

0

do i put " if velocity > 3 and i.KeyCode == Enum.KeyCode.LeftShift then "?

0

Yep! Next time, try trying something out first! You'd obviously define velocity right before the if statement

0

that's the problem it doesnt work

0

Updated my code for you

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