Script for removing tools from player not working

My script isn’t working. the idea is that when a player equips an item and clicks a part, if the item is correct the item will get removed from their inventory.

this is the script

if player.Character and player.Character:FindFirstChild(itemName) and player.Character[itemName]:IsA(“Tool”) then
player.Character[itemName]:Destroy()

anyone know the issue

Can I have full script and errors?

if player.Character and player.Backpack:FindFirstChild(itemName) then
    player.Backpack[itemName]:Destroy()
end
1 Like

I believe this is because it is not found in their character. Make sure to try and find it through their backpack too.

if player.Character and player.Character:FindFirstChild(itemName) and player.Character[itemName]:IsA(“Tool”) then
    player.Character[itemName]:Destroy()
end
if player.Backpack:FindFirstChild(itemName) and player.Backpack[itemName]:IsA("Tool") then
    player.Backpack[itemName]:Destroy()
end

Edit: Oh, someone already beat me to it lol

1 Like

You actually wouldn’t want to check their character for it, because it would end up breaking the script. All you want to search is their backpack, so the script doesn’t break!

if player.Character:FindFirstChild(itemName) then
	player.Character:FindFirstChild(itemName):Destroy()
elseif player.Backpack:FindFirstChild(itemName) then
	player.Backpack:FindFirstChild(itemName):Destroy()
end

As I stated, you most likely don’t want to search the players character, because if that’s returned nil, the rest of the script won’t run.

Hence the if statement, if it’s nil then the elseif statement is checked instead.

But if the tool is just under the player, you can’t enable it.

:FindFirstChild() returns false/nil if the tool is not found so, if used in an if statement, the code inside the if statement will not run if it is not there.

And if you can’t enable it, then this script won’t run. (My guess is there’s a part of the script above that checks enabled tools.)

Yes, and if the tool isn’t there, then the code won’t run. That’s why I chose FindFirstChild instead of WaitForChild, because WaitForChild will pause the script until the tool is found and if it isn’t, then the code will not run.

I’m sorry? He used FindFirstChild, not WaitForChild lmfao, maybe read what they typed before replying?

local clickDetector = script.Parent
local equipped = false
local itemName = "" --change to name of tool

clickDetector.MouseClick:Connect(function(player)
	local character = player.Character
	if character:FindFirstChild(itemName) then
		character:FindFirstChild(itemName):Destroy()
		--do code
	end
end)

Server script, place it inside the “ClickDetector” instance.

And I do know.

I never said DONT USE PLAYERS CHARACTER!!! I said you most likely don’t want to, and I am totally fine with being wrong. I never said I was always correct. We’re all humans and make mistakes. (Adding onto that, I was self-taught so I don’t always understand certain things.)

I’m also self taught, and that has nothing to do with this. I’m pretty sure you are completely misinterpreting what I said, and I will not fight about this. Please, do not continue this and just read through all of these chats and think about it instead of continuing this. I never said you were wrong about the WaitForChild, but you started this without even properly reading what he sent. (btw, I won’t be replying anymore)

I get you won’t be responding anymore. The fact that you had to literally laugh at my mistake is just inconsiderate. If you made a mistake, I wouldn’t say Imfao at you, I would kindly correct you and move on. Next time, if someone makes a mistake, instead of hurting their feelings, you can show them the developer API and correct them in a kindly manner.

local toolName = 'flour'
local bucket = game.Workspace.pizzaBucket
local clickDetector = bucket.ClickDetector


		game.Players.PlayerAdded:Connect(function(Player)
			clickDetector.MouseClick:Connect(function(Player)
				
		if Player.Backpack:FindFirstChild(toolName) then
			if Player.Character:FindFirstChild(toolName) then
				Player.Character:FindFirstChild(toolName):Destroy()
				--do whatever we want to do if the player has the tool 
					print("player has the item in their inventory")
					
				else
				
		
			end
	end
end)
end)

There is no need for PlayerAdded since MouseClick already obtains the player.

You can directly check if the tool exists in the Character. You might want to double-check if it is a tool.

Once you find the tool, you can simply index it by its name, no need for another FindFirstChild.

local toolName = 'flour'
local bucket = game.Workspace.pizzaBucket
local clickDetector = bucket.ClickDetector

clickDetector.MouseClick:Connect(function(Player)
	local Character = Player.Character;

	-- In case player has no character.
	if (not (Character)) then
		return;
	end

	local Tool = Character:FindFirstChild(toolName);

	if (not (Tool) or (Tool.ClassName ~= "Tool")) then
		return;
	end

	Tool:Destroy();
	-- Do whatever wanted.
end)
1 Like

can you reiterate? What do you mean by that