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

Run animation plays while standing still fix?

Asked by
FBS_8 25
4 years ago

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

Log in to vote
0
Answered by
sngnn 274 Moderation Voter
4 years ago

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

0
No I mean if you stand still and press shift it still plays altough you're not moving FBS_8 25 — 4y
Ad
Log in to vote
0
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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)

0
Wouldn't it be easier to check the HumanoidStateType? sngnn 274 — 4y
0
Which if statement would I put it in? FBS_8 25 — 4y
0
You would put it into the if statement on line 10. You could also check the humanoidstatetype to be fair Shawnyg 4330 — 4y
0
do i put " if velocity > 3 and i.KeyCode == Enum.KeyCode.LeftShift then "? FBS_8 25 — 4y
View all comments (3 more)
0
Yep! Next time, try trying something out first! You'd obviously define velocity right before the if statement Shawnyg 4330 — 4y
0
that's the problem it doesnt work FBS_8 25 — 4y
0
Updated my code for you Shawnyg 4330 — 4y

Answer this question