Checking If a Tool Is Equipped

Greetings, developers! I have made a wonderful drink system, however, I need to check what tool is equipped in a player’s backpack amongst other possible tools named the same thing, and name the one that’s equipped. I have read other threads about this, nothing seems to work. Here is the script, do your magic!

local function onTriggered(Player)
	if Player and Player.Character then
		local Backpack = Player:WaitForChild("Backpack")
		
		if Backpack:FindFirstChild("Cup") then
			local Cup = Backpack:FindFirstChild("Cup")
			
			
			if Cup and Cup:IsA("Tool") then
				Cup.Name = "Cup w/ Ice"
			end
		end
	end
end

script.Parent.Triggered:Connect(function(Player)
	onTriggered(Player)
end)
1 Like

Try this:

local equippedTool

char.ChildAdded:Connect(function(child)
	if child:IsA("Tool") then
		equippedTool = child
	end
end)

You can use Tool.Equipped to check if its equipped and tool.Activate to check when youve clicked with the tool.

For more information you can visit the roblox wiki on tools.
Tool (roblox.com)

Unfortunately, it didn’t work. :thinking:

1 Like

I am trying to avoid this event, as the script would possibly run way after the tool was equipped.

Which part didn’t work? And did you properly define char

The Backpack would be where the child was added, and I just noticed it was an event, which as I stated, could possibly fire way before the script is used.

You can avoid that by using a bool and checking after the tool is equipped.

If you want to check which tool is equipped, you would look inside the player’s character as that’s where equipped tools are stored. I don’t know what you mean by

So you’re suggesting I should sacrifice performance, and add a script, and a bool value to every tool, (possibly hundreds), just to check if a tool is equipped? I feel like there’s a better solution.

You could also just make the event when it’s needed, or disconnect it until needed

So my mistake, it did work, however, now it names ANY new child that’s added “Cup w/ Ice”. How would I combat that? Script:

local function onTriggered(Player)
	if Player and Player.Character then
		local Backpack = Player:WaitForChild("Backpack")
		
		if Backpack:FindFirstChild("Cup") then
			local Cup = Backpack:FindFirstChild("Cup")
			
			local equippedTool

			Player.Character.ChildAdded:Connect(function(child)
				if child:IsA("Tool") then
					equippedTool = child
		            equippedTool.Name = "Cup w/ Ice"
				end
			end)
		end
	end
end

script.Parent.Triggered:Connect(function(Player)
	onTriggered(Player)
end)

Tool.Equipped and a bool wouldn’t affect performance all that much actually. Especially doing it client side. I don’t know what information you’ve been reading, but unless your game has hundreds of tools it wouldn’t hurt your performance. 100’s of Tools meaning you physically have hundreds of tools not that every player accounts for 100 or more tools. Tool.Equipped is ran client side anyways there wouldn’t be performance issues server side.

There are also 26 more machines I have to add this script to, I could try your solution, but it feels like a few extra un-needed steps. :thinking:

If you don’t want it in an event, you could also use
equippedTool = character:FindFirstChildWhichIsA("Tool")

But with it naming everything “Cup w/ Ice” I don’t know why it would do that, as the script checks to make sure that a Tool is added

:sweat_smile: So the other machines name the cup totally different names, not just “Cup w/ Ice”, and I will try the one without the event right now!

local char = Player.Character
local equippedTool = char and char:FindFirstChildWhichIsA("Tool")
if equippedTool then
	print(equippedTool)
else
	print("No tool equipped!")
end

Do you have any idea where equipped tools get stored? Is there a specific location?

They get parented to the plrs character. Nothing fancy. (lik a folder named ‘Tool’)

When a tool is equipped it’s parented inside the player character, then when they unequip the tool it gets parented inside their backpack.