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?
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
i was asking just how to change the anims but you literally made an entire script! what a giga chad!
forgot the animation part, lemme add that
oh ok also i got an error
is there a way to fix the error?
ok, first use updated script and show me any errors in output
tagging for notification
alright, updated should work now
create an animation, looped optional since i coded it to be and replace animationId with yours
oh bruh. i forgot to put the animation id. thanks lemme try
im trying everything.[and it is a valid animationid] but i keep getting this error.
Wait. i can try one more thing. lemme see.
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
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"