How to find a tool that can have any name in a player's character

Today I found out, when a player equips a tool, it leaves the backpack and goes to the player’s character, so I realized this line of code won’t work anymore:

local tool = game.Players.LocalPlayer:WaitForChild("Backpack"):GetChildren()

Is there another way to get a tool in the player’s character that can have any name?

local player = — player variable
local char = player.Character or player.CharacterAdded:Wait()
local tool = char:FindFirstChildWhichIsA(“Tool“)

This should work

local tool = game.Players.LocalPlayer.Backpack:GetChildren() or game.Players.LocalPlayer:WaitForChild("Character"):FindFirstChildWhichIsA("Tool")	

Would this work too?

Well I would not use “or“. Incase the player has more then 1 tool the Character would not be searched.
Also, this is how you can do it locally for the character, you had it slightly wrong.

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local tool = char:FindFirstChildWhichIsA(“Tool“)

I’m actually using a loop in the process, it won’t work.

	local tool = game.Players.LocalPlayer.Backpack:GetChildren() 		  -- we will take the tool away from the player
				local plrtool = game.Players.LocalPlayer:WaitForChild("Character"):FindFirstChildWhichIsA("Tool")
				plrtool:Destroy()
				for _, tools in tool do
					tools:Destroy()

I would do this:

for _, v in pairs(game.Players.LocalPlayer.Backpack:GetChildren()) do
    v:Destroy()
end
for _,v in pairs(game.Players.LocalPlayer:WaitForChild(“Character“):GetChildren()) do
    if v:IsA(“Tool“) then
        v:Destroy()
    end
end

Edit: Remember this is run locally!!

1 Like

Ohhh. It’s two different scripts. Wow thanks 100

This could also work (It is still running locally):

local player = game.Players.LocalPlayer
local character = player.Character

local function RemoveTools()
	player.Backpack:ClearAllChildren()
	local tool = character:FindFirstChildOfClass("Tool")
	if tool then tool:Destroy() end
end

RemoveTools()
1 Like

Thanks to you too! I’m not used to naming functions I just do it on the spot, thanks again!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.