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

How Would You Stop A Tool Animation After Unequipping It?

Script:

knife = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
knife.Equipped:Connect(function()
	local equip = script.Parent.Parent.Humanoid:LoadAnimation(script.Equip)
	local idle = script.Parent.Parent.Humanoid:LoadAnimation(script.Idle)
	equip:Play()
	wait(1)
	idle:Play()
end)

knife.Activated:Connect(function()
	local slash2 = script.Parent.Parent.Humanoid:LoadAnimation(script.Slash2)
	local sound = script.Parent.Handle.Slash:Play()
	slash2:Play()
end)

knife.Unequipped:Connect(function()
	local idle = character.Humanoid:LoadAnimation(script.Idle)
	idle:Stop()
end)

Everything runs fine until you unequip the tool. How would you stop the animation?

How the knife works: https://gyazo.com/6c16b268f74f9b781fa618ca101a0517

1 answer

What I would do is this: declare idle as a nil variable outside of any of the events. Define idle inside of the knife.Equipped event, so the entire script knows that it is a animation track. Than simply command the animation track to stop inside of the knife.Equipped event.

local knife = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local idle = nil --define "idle" as a nil variable outside of any event

knife.Equipped:Connect(function()
    local equip = script.Parent.Parent.Humanoid:LoadAnimation(script.Equip)
    idle = script.Parent.Parent.Humanoid:LoadAnimation(script.Idle) --since you already defined "idle", you don't have to use "local"
    equip:Play()
    wait(1)
    idle:Play()
end)

knife.Activated:Connect(function()
    local slash2 = script.Parent.Parent.Humanoid:LoadAnimation(script.Slash2)
    local sound = script.Parent.Handle.Slash:Play()
    slash2:Play()
end)

knife.Unequipped:Connect(function()
    idle:Stop() --no need to define "idle" here.
end)

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