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

Idle animation wont stop playing?

So ,i have a script for my sword

local CanAttack = true

script.Parent.Equipped:connect(function()
	local idle = script.Parent.Parent.Humanoid:LoadAnimation(script.Idle)
	local attack = script.Parent.Parent.Humanoid:LoadAnimation(script.Attack)
	
	idle:Play()
end)

script.Parent.Activated:connect(function()
	local idle = script.Parent.Parent.Humanoid:LoadAnimation(script.Idle)
	local attack = script.Parent.Parent.Humanoid:LoadAnimation(script.Attack)
	
	if CanAttack == true then
		attack:Play()
		idle:Stop()
		CanAttack = false
		wait(1)
		attack:Stop()
		idle:Play()
		CanAttack = true
		script.Parent.canDamage.Value = true
	end
end)

script.Parent.Unequipped:Connect(function()
	local character = game.Players.LocalPlayer.Character
	local idle = character.Humanoid:LoadAnimation(script.Idle)	
	
	idle:Stop()
end)

basically it plays the idle animation unless the tool is activated, then it will run the attack animation and go back to the idle animation after 1 second.

HOWEVER, the problem is.. when i unequipped the sword my character model still plays the idle animation, so he pretty much runs around with his hand up but no sword in it.

2 answers

✓ Accepted answer

idle needs to be global across the script. Don't use LoadAnimation every time you want to play/stop it, because that would make a new animation playing on top of the other one. I might also suggest removing the code on line 6 as you don't use it in that function.

local CanAttack = true
local idle = nil

script.Parent.Equipped:connect(function()
    idle = script.Parent.Parent.Humanoid:LoadAnimation(script.Idle)
    local attack = script.Parent.Parent.Humanoid:LoadAnimation(script.Attack)

    idle:Play()
end)

script.Parent.Activated:connect(function()
    local attack = script.Parent.Parent.Humanoid:LoadAnimation(script.Attack)

    if CanAttack == true then
        attack:Play()
        idle:Stop()
        CanAttack = false
        wait(1)
        attack:Stop()
        idle:Play()
        CanAttack = true
        script.Parent.canDamage.Value = true
    end
end)

script.Parent.Unequipped:Connect(function()
    local character = game.Players.LocalPlayer.Character

	idle:Stop()
	idle:Destroy()
end)

4 comments

0

awesome! this code worked! i do have a question though, what did making local idle = nil do? and also, what is the difference of having local in front of a variable name when creating it do?

0

hey can i ask why you made a local variable character at the bottom?

0

Actually, it doesn't need to be there. But if you want to access the character, put the variable at the top so you can use it across the whole script.

You need to put, on unequip stop animation. Because the animation plays on equip, so the script does not have a stop point it still thinks the sword is equipped. So it is still playing the animation. When you put it in a stop point the script will know the sword is not equipped so I need to stop playing animation.

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