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

How to stop a tool's idle animation after unequipping?

Asked by 2 years ago

The idle animation does not stop after the tool is unequipped.

This is what I have:

local Screen
local Tool = script.Parent

local EquipAnimation = script.Parent.Equip
local UnequipAnimation = script.Parent.Unequip
local IdleAnimation = script.Parent.Idle



Tool.Equipped:Connect(function()


    local Humanoid = Tool.Parent.Humanoid
    local Track = Humanoid:LoadAnimation(EquipAnimation)

    Track:Play()


    if not game:GetService("Players").LocalPlayer.PlayerGui:FindFirstChild("ScreenGui") then
        wait(2)
        Screen = script.Parent.ScreenGui:Clone()
        Screen.Parent = game:GetService("Players").LocalPlayer.PlayerGui
    end

    local Trackidle = Humanoid:LoadAnimation(IdleAnimation)

    Trackidle:Play()

end)


Tool.Unequipped:Connect(function()
    Screen:Destroy()
    Trackidle:Stop()
end)

1 answer

Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
2 years ago
Edited 2 years ago

You gotta initialize the IdleAnimation first, so you can play and pause it as you need:

local Screen
local Tool = script.Parent

local EquipAnimation = script.Parent.Equip
local UnequipAnimation = script.Parent.Unequip
local IdleAnimation = script.Parent.Idle

-- I put the AnimationTracks at the top, this method is called "initialize"

local Trackidle = Humanoid:LoadAnimation(IdleAnimation)
local Track = Humanoid:LoadAnimation(EquipAnimation)

Tool.Equipped:Connect(function()


    local Humanoid = Tool.Parent.Humanoid

    Track:Play()


    if not game:GetService("Players").LocalPlayer.PlayerGui:FindFirstChild("ScreenGui") then
        wait(2)
        Screen = script.Parent.ScreenGui:Clone()
        Screen.Parent = game:GetService("Players").LocalPlayer.PlayerGui
    end

    Trackidle:Play()

end)


Tool.Unequipped:Connect(function()
    Screen:Destroy()
    Trackidle:Stop()
end)
Ad

Answer this question