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

How to check if tool is equipped/unequipped?

Asked by 4 years ago
Edited 4 years ago

This is local script in the tool and it is not working properly, it plays animation after tool is unequipped. I think I need to add "check if tool is equipped" but I don't know how to do it.

local Tool = script.Parent
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 Animation = Instance.new("Animation")
Animation.AnimationId = AnimationID
local LoadAnimation = Humanoid:LoadAnimation(Animation)
local key = 'R'
local debounce = true

-- how to check if tool is equipped?

Tool.Equipped:Connect(function()
    UserInputService.InputBegan:Connect(function(Input, IsTyping) 
        if  IsTyping then return end
        if Input.KeyCode == Enum.KeyCode[key] and debounce == true then
            debounce = false
            LoadAnimation:Play()
            wait(3)
            debounce = true
        end
    end)
end)

-- how to check if tool is unequipped?

Tool.Unequipped:Connect(function() 
    LoadAnimation:Stop()
    LoadAnimation:Destroy()
end)

2 answers

Log in to vote
0
Answered by 4 years ago

You could use variables to check if the tool is equipped like this:

local Equipped = false

Tool.Equipped:Connect(function()
    Equipped = true

    UserInputService.InputBegan:Connect(function(Input, IsTyping)
        if Equipped == true then -- Check if equipped when input begins
            -- Put code here
        end
    end)
end)

Tool.Unequipped:Connect(function()
    Equipped = false
end)
0
Thank you so much!!! I have been trying to finish my script for 2 weeks but I am bad at scripting, and today I finally did it :D qaiaqaia 13 — 4y
0
:) WaddelsG 69 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Or alternitively you could just check tool.Parent. A tool should be Parented to Player.Backpack if it's unequipped and parented to their character if it's equiped.

if Tool.Parent == Player.Backpack then
    print("Unequipped")
elseif Tool.Parent == Player.Character then
    print("Equipped")
end

Answer this question