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

How do i stop an animation after the tool is unequipped?

I made a sword which works perfectly fine with it's slashing and idle animation, but when i unequip the sword the idle animation remains active.How do i fix it?

Here is the script:

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)

2 answers

Most events have an opposite event. Tool has one too, Equipped --> Unequipped

You can simply use it and connect it to the animation

Tool.Unequipped:connect(function()
	animation:Stop()
end)

Change 'animation' with the animation you want to stop

3 comments

0

I am thankful to your answer but it didn't work the situation still remains the same

0

Okay, can you set the LoadAnimations on top of your script and try again?

0

(not inside the equipped event

I believe what you should do is make an unequipped function.

So:

script.Parent.Unequipped:connect(function()
	local idle = script.Parent.Parent.Humanoid:LoadAnimation(script.Idle)
	idle:Stop()
end)

Or something along those lines.

4 comments

0

Why would you load an animation already loaded on? (Also, I answered this already)

0

I am thankful to your answer but it didn't work the situation still remains the same

0

I was trying to define "idle," since the other functions had "idle" under local. Otherwise, unequipped wouldn't have known what "idle" meant.

0

It would be better to put the loadanimations on top so you're sure both anis arethe same

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