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()
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
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
: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.
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.
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.
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)