Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to check if tool is equipped/unequipped?

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

✓ Accepted answer

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)

2 comments

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

0

:)

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

This discussion is preserved as it was. New answers, comments, and votes are closed.