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

My sprint script not changing animation! How to fix?

Asked by 3 years ago

Hello i have some problems with my sprinting script because it doesn't change the animation from walking to running and only works when i stop and start moving again

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Cam = workspace.Camera

local NormalSpeed = 16
local SprintSpeed = 33

local Sprinting = false

mouse.KeyDown:connect(function(key)
    if key:byte() == 48 then
        if Sprinting == false then
            player.Character.Humanoid.WalkSpeed = SprintSpeed
            Sprinting = true
            Cam.FieldOfView = 80    
            player.Character.Animate.walk.WalkAnim.AnimationId = ("rbxassetid://6708766874")    
        elseif Sprinting == true then
            player.Character.Humanoid.WalkSpeed = NormalSpeed
            Sprinting = false   
            Cam.FieldOfView = 70    
            player.Character.Animate.walk.WalkAnim.AnimationId = ("rbxassetid://6706074754")                    
        end
    end
end)

this is the sprint script

2 answers

Log in to vote
0
Answered by 3 years ago

This should fix it, but you have to change both of your animation's priorities to "Movement" and toggle looping for both of the animations that should fix everything.

local player = game.Players.LocalPlayer--The local player variable
local Camera = workspace.Camera--The camera variable
local NormalSpeed = 16--The normal speed variable
local SprintSpeed = 33--The sprint speed variable
local UserInputService = game:GetService("UserInputService")--The UserInputService service

UserInputService.InputBegan:Connect(function(input)--Fired when an input begins
    if input.KeyCode == Enum.KeyCode.LeftShift then--Checks if the KeyCode equals to LeftShift
        Camera.FieldOfView = 80--Sets the cameras field of view
        player.Character.Humanoid.WalkSpeed = SprintSpeed--Sets the walk speed to SprintSpeed
        player.Character.Animate.walk.WalkAnim.AnimationId = ("rbxassetid://6708766874")--Sets the walk animation to your walk animation
    end 
end)

UserInputService.InputEnded:Connect(function(input)--Fired when the input ended
    if input.KeyCode == Enum.KeyCode.LeftShift then--Checks if the KeyCode equals to LeftShift
        Camera.FieldOfView = 70--Sets the cameras field of view
        player.Character.Humanoid.WalkSpeed = NormalSpeed--Sets the walk speed to NormalSpeed
        player.Character.Animate.walk.WalkAnim.AnimationId = ("rbxassetid://6706074754")--Sets the walk animation to your sprint animation
    end
end)
--//Go to both of animations and set the to animation priority to "Movement" and toggle looping for both of the animations that should fix everything\\--
Ad
Log in to vote
1
Answered by
Xapelize 2658 Moderation Voter Community Moderator
3 years ago

You need to use UserInputService and get the input because it updates everytime when you unpress your key so it unfunctions. Getting the KeyPress using player:GetMouse() is not recommended for now.

Answer this question