Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I stop an animation without stopping other animations?

Asked by 4 years ago

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)


1 answer

Log in to vote
0
Answered by 4 years ago

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.

0
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. IStarConquestI 414 — 4y
0
Thanks! I had already thought of that before but I got an error code. I tried again and it works. SamuelaNutella 14 — 4y
Ad

Answer this question