How do i change the walk animations with a local script?

Hello! i am making a game. [duh] and i want to change the players walking animation when holding shift. [to sprint] is there a way to do it?

7 Likes

add a local script inside StarterCharacterScripts (if not inside that then anim wont replicate to other clients and script breaks)

name it whatever and add this:

--Top Variables (MUST CHANGE)
local animationId = 1

--Main Variables (do not change)
local humanoid = script.Parent:WaitForChild("Humanoid")
local sprinting = false
local CAS = game:GetService("ContextActionService")
local UIS = game:GetService("UserInputService")
local animation = Instance.new("Animation", script)
animation.AnimationId = "rbxassetid://"..tostring(animationId)
animation.Name = "Run"
local track = humanoid:WaitForChild("Animator"):LoadAnimation(animation)
track.Looped = true

--Config (optional changes)
local walkSpeed = 16
local runSpeed = 32
local toggle = true --false if you want to hold sprint
local createButton = false --whether to show button to sprint on mobile devices

----------------------------------------------------------------------------------
function sprint()
if UIS:IsKeyDown(Enum.KeyCode.LeftShift) and UIS:IsKeyDown(Enum.KeyCode.RightShift) then return end
sprinting = not sprinting
if toggle then
if sprinting then
humanoid.WalkSpeed = runSpeed
track:Play()
else
humanoid.WalkSpeed = walkSpeed
end
else
humanoid.WalkSpeed = runSpeed
track:Play()
repeat task.wait(0.1) until UIS:IsKeyDown(Enum.KeyCode.LeftShift) == false and UIS:IsKeyDown(Enum.KeyCode.RightShift) == false
humanoid.WalkSpeed = walkSpeed
sprinting = false
end
end

CAS:BindAction("Sprint", sprint, createButton, Enum.KeyCode.LeftShift, Enum.KeyCode.RightShift)
humanoid.WalkSpeed = walkSpeed

while not sprinting and track.IsPlaying do
task.wait()
track:Stop(0.0000001)
end

feel free to config the optional variables

6 Likes

i was asking just how to change the anims but you literally made an entire script! what a giga chad!

4 Likes

forgot the animation part, lemme add that

3 Likes

oh ok also i got an error
image

3 Likes

is there a way to fix the error?

3 Likes

ok, first use updated script and show me any errors in output

3 Likes

tagging for notification

2 Likes


here is an error.

4 Likes

alright, updated should work now

3 Likes

image
image

create an animation, looped optional since i coded it to be and replace animationId with yours

1 Like

oh bruh. i forgot to put the animation id. thanks lemme try

1 Like

im trying everything.[and it is a valid animationid] but i keep getting this error.
image

1 Like

Wait. i can try one more thing. lemme see.

1 Like

local player = game.Players.LocalPlayer – defining player
local character = player.Character or player.CharacterAdded:Wait() --defining character
local humanoid = character:WaitForChild(“Humanoid”)–defining humanoid
humanoid.WalkSpeed = 20 --Changing Humanoid WalkSpeed Property

Okay. i just added an animation in the script. through the explorer.

it works but. theres 2 problems. i
1: it only works for RightShift
2 : The animation does not stop

1 Like

Ok wait it does work for leftshift too. but the animation wont stop when i let go of shift. [i have the toggle false]

I will update and fix these issues, including the invalid assetid (easiest fix)

There are already resources online to do this.

To make it work with a LocalScript instead of Server Script, I would make a local script inside of StarterCharacterScripts and go like:

wait(3)
script.Parent.Animate.walk.WalkAnim.AnimationId = "rbxassetid://YOURANIMATIONID"
script.Parent.Animate.run.RunAnim.AnimationId = "rbxassetid://YOURANIMATIONID"
2 Likes