Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Animation wont stop playing or it is overwriting the idle animation?

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

0

Are you sure you have the animation priorities correct? This is a common problem when using multiple animations.

0

yes, they all are on movement,

0

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.

0

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

0

want me to just put the fixed script in answers and explain it?

0

Sure and thank you!

1 answer

Here is a script that fixes the local variable problem. Also, if this doesn't work, it's likely it is a animation priority problem, try using different priorities to allow specific animations to overwrite others to fix the problem

local Player = game.Players.LocalPlayer
local run =   local run = Instance.new('Animation')-- naming all the animation local variables up here instead
local run = Instance.new('Animation')	
local idle = Instance.new('Animation')	
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
	        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()
	        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.

	        idle.AnimationId = 'rbxassetid://5022482368'
	        local idleAnim = Character.Humanoid:LoadAnimaton(idle)
	        idleAnim:Play()
	    end
	end

1 comment

0

Also, if there is a problem with my answer, please go ahead and correct me

This discussion is preserved as it was. New answers, comments, and votes are closed.