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

How can I fix this shift to sprint animation?

Essentially, when you press left shift, it plays an animation. However, the humanoid plays the running animation even if they aren't moving.

local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character

if not character or not character.Parent then
    character = player.CharacterAdded:wait()
end

local humanoid = character:WaitForChild("Humanoid")


UIS.InputBegan:connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        humanoid.WalkSpeed = 25
        local Anim = Instance.new("Animation")
        Anim.AnimationId = "rbxassetid://4786194263"
        PlayAnim = humanoid:LoadAnimation(Anim)
        PlayAnim:Play()
    end
end)

UIS.InputEnded:connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        humanoid.WalkSpeed = 16
        PlayAnim:Stop()
    end
end)

5 comments

0

Perhaps the animation priority isnt Movement or something. Im kinda new to animations too

0

humanoid.Running:Connect(function() ? ?

0

I'm currently writing an answer, found the solution.

3 answers

you could change the default walk animation when they press shift, and change it back when they release it..

1 comment

0

good idea, although i found there is a way to fix the script without having to change the way you do it entirely

Hello! I found the solution.

So there were two things I did to fix it. I put the localscript in startergui, and changed it so that it defines the variable Anim outside of the input begun and ended. Here's the code:

(I had to modify the asset ID since roblox doesn't allow you to fire someone else's animation in your code)

local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character

if not character or not character.Parent then
    character = player.CharacterAdded:wait()
end

local humanoid = character:WaitForChild("Humanoid")

local Anim = Instance.new("Animation") --Define it here instead and not assume it's already been created when input ends
Anim.AnimationId = "rbxassetid://4095423668"
PlayAnim = humanoid:LoadAnimation(Anim)


UIS.InputBegan:connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        humanoid.WalkSpeed = 25
        PlayAnim:Play()
    end
end)

UIS.InputEnded:connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        humanoid.WalkSpeed = 16
        PlayAnim:Stop()
    end
end)

I published the game I tested it in with its source code copyable in case you wanted to take a look or copy it that way into your game.

I made the animation priority 'core' and it worked as well.

If any of this doesn't work for whatever reason or you are confused, please say so in the replies to this answer and I will help you. If this works, please accept it as the answer.

Hope this helps!

-ProqrammedGreen

3 comments

0

The same problem still arises. I'm trying to check if the humanoid is running or not. The animation still runs even if the humanoid isn't running.

0

then check the walkspeed or use humanoid:running(), right? But in this code the animations work though, only when youre pressing shift I tested it

0

No no, the script I showed runs the animation correctly, but still runs even when the humanoid doesn't run. So I'm trying to figure out how to add humanoid:running() and where I should put it.

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