How to detect players tools

Hi my names is pixeluted and i want to detect if player have tool here is my script

local player = game.Players.LocalPlayer
local BackPack = player.Backpack

BackPack.ChildAdded:Connect(function()
	print("Yes")
	if BackPack:FindFirstAncestor("Test") then
		print("Finded")
		local Text = Instance.new("TextLabel",script.Parent.Handler)
		Text.Text = "Test"
		Text.Name = "TestTool"
		local What = Instance.new("StringValue",Text)
		What.Name = "What"
		What.Value = "Test"
		local Value = Instance.new("IntValue",Text)
		Value.Name = "Value"
		Value.Value = 20
	end
end)

Thank you for reading

You just gave us code without even explaining an issue?

1 Like

Please elaborate on what you mean by the player has a tool. I’m not quite sure what your code is doing and don’t know the functionality you’re looking for.

I want detect if player got in Backpack tool

Please, I want to help, but you have to elaborate further than “player got in Backpack tool”, it’s not a proper sentence and I don’t understand what you mean by it. I understand you’re from the Czech Republic so it might not be so easy to articulate what you want in English, but I’m unclear on what you’re asking for.

But does it not work, is there an exception, simply posting code and saying “it doesn’t work” is not helpful.

I just want detect if player have specific tool in player Backpack

For checking if an instance contains a specific child, we can use Instance:FindFirstChild(Name)

I think you may have confused this with Instance:FindFirstAncestor(), which you used in your code.

If you want to check if the tool when it gets added to Backpack, use this:

local Player = game:GetService("Players").LocalPlayer
local Backpack = Player:WaitForChild("Backpack")

local ToolName = "Test"

Backpack.ChildAdded:Connect(function(Tool)
    if Tool.Name == ToolName then
        print("This is the tool I'm looking for.")
    end
end)

If you want to simply check the tools already in the backpack, you would use a for loop. This would give you all the tools named “Test”.

local Player = game:GetService("Players").LocalPlayer
local Backpack = Player:WaitForChild("Backpack")

local ToolName = "Test"

for _, Tool in pairs(Backpack:GetChildren()) do
    if Tool.Name == ToolName then
        print("This is the tool I'm looking for.")
    end
end

Finally, if you only want one tool with the name “Test” if there are multiple named that, you could use:

local Player = game:GetService("Players").LocalPlayer
local Backpack = Player:WaitForChild("Backpack")

local ToolName = "Test"

local Tool = Backpack:FindFirstChild(ToolName)

if Tool then
    print("This is the tool I'm looking for.")
end
3 Likes

Okay i tes it thx for this is code

1 Like

Its work Thank you much :heart_eyes: :heart_eyes:

4 Likes