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

How to check if tool is in Character/Equipped?

I want to keep checking if my tool, pistol, is in the character. Here's the script I tried to make:

game.Players.PlayerAdded:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	while true do
		wait(1)
		for i, v in pairs(character:GetChildren()) do
			if v.Name == "Pistol" then
				print("Pistol found")
			end
		end
	end
end)

2 comments

0

do you want to check if it's equipped?

0

yes

1 answer

Tool has two useful events which may be helpful for you, these are Equipped and Unequipped

Using your example you could detect what tool is equipped through this method:

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		for _, Tool in pairs(Player.Backpack:GetChildren()) do
			if Tool:IsA('Tool') then
				Tool.Equipped:Connect(function()
					print(Tool.Name .. ' has been equipped by ' .. Player.Name)
				end)
			end
		end
	end)
end)

1 comment

1

Yes. This is the way I would do it too. Using :FindFirstChild() is not needed here. (Related to the other answer too)

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