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

How to detect if a tool is equipped?

For example, if I have a punching script and I do not want it to activate if a player has a tool equipped.

3 answers

✓ Accepted answer

When you equip a tool, the tool's parent gets set to your character.

You can use a loop and check if there is a tool inside the player's character.

repeat wait() until game.Players.LocalPlayer.Character:IsDescendantOf(workspace)

local Character = game.Players.LocalPlayer.Character

function CHECK_TOOL(character)
	for _, v in pairs(character:GetChildren()) do 
		if v:IsA("Tool") then 
			print("tool found!")
			return true 
		end
	end
end

function PUNCH()
	if not CHECK_TOOL(Character) then 
		-- code
	end
end

5 comments

0

Did not know that, cool thanks.

0

No, no, no. sickings, NO! Don't write a loop that does nothing but wait() for something to happen! Use events instead!

0

CharacterAdded and repeat wait() until Character:IsDescendantOf(workspace) do the same thing. I just prefer using repeat wait() until.. it works on my gun script lol

0

No, they don't do the same thing. The result is functionally the same, but repeat wait() until whatever is very inefficient.

0

You just said they don't do the same thing then say they are functionally the same..

local tool = character:FindFirstChildWhichIsA("BackpackItem")

if tool then
	-- the character has a tool equipped
else
	-- the character does not have a tool equipped
end

This is the proper way to check for an equipped tool. It will detect tools as well as hopperbins. This works because both the Tool and HopperBin classes inherit the "BackpackItem" abstract class.

2 comments

0

I'm having an error that says "character is an nil value." I don't know why? But, I think your way works good..

0

umm sorry for the very late response but you have to first get the player and then the character for this script to work check on this site to find out how

Insert this script inside your handle.


tool = script.Parent.Parent

tool.Equipped:connect(function()
print("Tool Equipped")
end)

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