Hi. I'm making a punch tool.
The following code stops all animations, resulting in the player having no animations until a key is pressed.
How can I stop only one animation without the others being stopped?
tool.Unequipped:connect(function()
local hum = char:WaitForChild("Humanoid")
for _,anim in pairs(hum:GetPlayingAnimationTracks()) do
anim:Stop()
end
end)
Here's the full localscript:
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()
local tool = script.Parent
local swingsound = tool:WaitForChild("Swing")
local canattack = tool:WaitForChild("CanAttack")
local debounce = false
local UIS = game:GetService('UserInputService')
tool.Equipped:connect(function()
local idle = script.Parent.Parent.Humanoid:LoadAnimation(script.Idle)
idle:Play()
idle.Looped = true
end)
tool.Activated:connect(function()
if debounce == false then -- check if debounce is false
local choose = math.random(1,2)
canattack.Value = true
if choose == 1 then
debounce = true -- set debounce as true
local swing1animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch1)
swingsound:Play()
swing1animation:Play()
wait(.5) -- wait
debounce = false -- set debounce as false so the code can run again
elseif choose == 2 then
debounce = true -- set debounce as true
local swing2animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch2)
swingsound:Play()
swing2animation:Play()
wait(.5) -- wait
debounce = false -- set debounce as false so the code can run again
end
end
end)
tool.Deactivated:connect(function()
canattack.Value = true
end)
tool.Unequipped:connect(function()
local hum = char:WaitForChild("Humanoid")
for _,anim in pairs(hum:GetPlayingAnimationTracks()) do
anim:Stop()
end
end)

2 comments
To clarify what I mean by initialize, I mean have it run the moment the script executes, rather than assigning it to any specific event.
Thanks! I had already thought of that before but I got an error code. I tried again and it works.