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

Having troubles with making doing a shift to sprint with animation?

Asked by 3 years ago

Not sure what I did wrong but it isn't working and I need help.

local player = game.Players.LocalPlayer local Character = player.Character or script.Parent local humanoid = player.Character.Humanoid local Humanoid = Character.Humanoid local UIS = game:GetService("UserInputService")

local AnimationId = 'rbxassetid://6267230901'

UIS.InputBegan:Connect(function(input) if input.KeyCode == Enum.KeyCode.LeftShift then if player.Character ~= nil then

        while player.Character.Humanoid.WalkSpeed >= 50 do 
            wait()

            local Animation = Instance.new("Animation")
            Animation.AnimationId = AnimationId
            local LoadAnimation = Humanoid:LoadAnimation(Animation)
            LoadAnimation:Play()

        end
    end
end

end)

UIS.InputEnded:Connect(function(input) if input.KeyCode == Enum.KeyCode.LeftShift then if player.Character ~= nil then player.Character.Humanoid.WalkSpeed = 16

        while player.Character.Humanoid.WalkSpeed <= 16 do 
            wait()

            local Animation = Instance.new("Animation")
            Animation.AnimationId = AnimationId
            local LoadAnimation = Humanoid:LoadAnimation(Animation)
            LoadAnimation:Stop()

        end
    end
end

end)

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Put a local script on the StarterPlayerScripts, then enter this:

repeat wait() until game.Players.LocalPlayer
local Player = game.Players.LocalPlayer
local Character = workspace:WaitForChild(Player.Name)
local Humanoid = Character:WaitForChild('Humanoid')

local AnimID = 123456789 -- put your animation ID here

local RunAnimation = Instance.new('Animation')
RunAnimation.AnimationId = 'http://www.roblox.com/asset/?id='..AnimID
RAnimation = Humanoid:LoadAnimation(RunAnimation)

Running = false

function Handler(BindName, InputState)
    if InputState == Enum.UserInputState.Begin and BindName == 'RunBind' then
        Running = true
        Humanoid.WalkSpeed = 50
    elseif InputState == Enum.UserInputState.End and BindName == 'RunBind' then
        Running = false
        if RAnimation.IsPlaying then
            RAnimation:Stop()
        end
        Humanoid.WalkSpeed = 16
    end
end

Humanoid.Running:connect(function(Speed)
    if Speed >= 10 and Running and not RAnimation.IsPlaying then
        RAnimation:Play()
        Humanoid.WalkSpeed = 50
    elseif Speed >= 10 and not Running and RAnimation.IsPlaying then
        RAnimation:Stop()
        Humanoid.WalkSpeed = 16
    elseif Speed < 10 and RAnimation.IsPlaying then
        RAnimation:Stop()
        Humanoid.WalkSpeed = 16
    end
end)

Humanoid.Changed:connect(function()
    if Humanoid.Jump and RAnimation.IsPlaying then
        RAnimation:Stop()
    end
end)

game:GetService('ContextActionService'):BindAction('RunBind', Handler, true, Enum.KeyCode.LeftShift)
Ad

Answer this question