Summary:
Hello, I am trying to make a sprint/animation script but the walking animation wont stop playing when I am trying to play the idle animation. If you could help me it your be appreciated. My animations are all on movement and are mine. My script in is 'StarterCharacterScripts' and is a local script.
Video:
(copy an paste into search)https://gfycat.com/sorrowfulajarbluebottlejellyfish
Code:
local Player = game.Players.LocalPlayer
Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
repeat wait() until game.Players.LocalPlayer
m = game.Players.LocalPlayer:GetMouse()
m.KeyDown:connect(function(key)
if key == "0" then
local run = Instance.new('Animation')
run.AnimationId = 'rbxassetid://5013482632'
runAnim = Character.Humanoid:LoadAnimation(run)
runAnim:play()
Character.Humanoid.WalkSpeed = 20
end
end)
m.KeyUp:connect(function(key)
if key == "0" then
runAnim:stop()
local walk = Instance.new('Animation')
walk.AnimationId = 'rbxassetid://5013378526'
walkAnim = Character.Humanoid:LoadAnimation(walk)
walkAnim:Play()
Character.Humanoid.WalkSpeed = 8
end
end)
--start of idle animation--
Character.Humanoid.Running:Connect(function(speed)
if Character.Humanoid.WalkSpeed < 0.5 then
walkAnim:Stop() --turning off walk animation.
local idle = Instance.new('Animation')
idle.AnimationId = 'rbxassetid://5022482368'
local idleAnim = Character.Humanoid:LoadAnimaton(idle)
idleAnim:Play()
end
end

6 comments
Are you sure you have the animation priorities correct? This is a common problem when using multiple animations.
yes, they all are on movement,
Maybe try making the idle anim to priority idle, run animation to the highest priority, and the walk anim to right under the run anim, aside from that, the script seems fine to me.
also, local variables will only work inside the function you put them in, you need to move the local variables outside of the functions so you can reference the local variable in all of the functions. This is something that has to be fixed because you use local variables inside multiple functions, and the variable will not work outside of the function/loop it was assigned in
want me to just put the fixed script in answers and explain it?
Sure and thank you!