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)
You could load the animationtracks when the script initializes, rather than when you want the animations to play. That way, you could call stop on only the animations that the tool would be using.