--//Services
local UIS = game:GetService('UserInputService')
--//Variables
local player = game.Players.LocalPlayer
local char = player.Character
local tool = script.Parent
--//Use this since it's a local script inside a tool
player.CharacterAdded:Connect(function(character)
char = character
local hum = char:WaitForChild('Humanoid')
--//Animation
local animation = Instance.new('Animation')
animation.AnimationId = "rbxassetid://2680328238"
local spinSlash = hum:LoadAnimation(animation)
--//Equipped
tool.Equipped:Connect(function()
--//Input
local debounce = false
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.X and not debounce then
debounce = true
spinSlash:Play()
wait(2)
debounce = false
end
end)
end)
--//Unequipped
tool.Unequipped:Connect(function()
spinSlash:Stop()
end)
end)
This is an animation of a sword slashing so it's not a looped animations. When the tool is equipped, x can be pressed in order to play the slashing animation but when i unequip it, x can still be pressed to play the animation again. How can i fix this so that x can only be pressed when the tool is equipped?

4 comments
make a variable for the :LoadAnimation() at the beginning of the script, and call :Start() and :Stop() on it, rather than reloading it everytime
If i made the variable local spinSlash = hum:LoadAnimation(tool.SpinSlash) at the top, wouldn't it be the same as just reloading it because the variable is still loading the animation.
have you tried setting debounce to false after unequipping?
I just tried that and it didn't make a difference because debounce is already set to false after the animation played