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

How to stop tool animation when tool is unequipped?

Asked by 4 years ago

I inserted local script in the tool and now it plays animation when I press "R", but it still plays animation after tool is unequipped, how to fix that? my current script:

local Player = game.Players.LocalPlayer
local Character = Player.Character or script.Parent
local Humanoid = Character.Humanoid
local UserInputService = game:GetService("UserInputService")

local AnimationID = 'rbxassetid://4868889544'

local key = 'R'
local debounce = true

UserInputService.InputBegan:Connect(function(Input, IsTyping)
    if  IsTyping then return end
    if Input.KeyCode == Enum.KeyCode[key] and debounce == true then
        debounce = false
        local Animation = Instance.new("Animation")
        Animation.AnimationId = AnimationID
        local LoadAnimation = Humanoid:LoadAnimation(Animation)
        LoadAnimation:play()
        wait(5)
        debounce = true

        end
    end)

2 answers

Log in to vote
0
Answered by 4 years ago

You could do something like this:

local enabled = false

UserInputService.InputBegan:Connect(function(Input, IsTyping)
    If IsTyping or not enabled then return end
    -- code
end)

tool.Equipped:Connect(function()
    enabled = true
end)

tool.Unequipped:Connect(function()
    enabled = false
end)

Basically this lets you know if the player has the tool equipped.

0
I didn't add all of the declared variables, but you should get the gist. Brandon1881 721 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

You could try using the Tool.Unequipped event! This fires every time a tool is unequipped. You can read about Tool.Unequipped here!

Answer this question