Tool not deleting?

This is the script to delete tools in a player’s backpack. The thing is, although there is nothing in the backpack afterwards, if you are equipping a tool before touching the part, it gets “deleted” but you can still hold it and use it. Can anybody help???

local Players = game:GetService("Players")
local part = script.Parent

part.Touched:Connect(function(touch)
	if touch.Parent:FindFirstChild('Humanoid') then
		local character = touch.Parent
		local player = Players:GetPlayerFromCharacter(character)
		for _, v in pairs(player.Backpack:GetChildren()) do
			print(v.Name..' was removed for '..player.Name)
			v.:Destroy()
		end
	end
end)

When you equip a tool, it is removed from your backpack and placed into your character. You’re only deleting tools from the player’s backpack, so equipped tools will be missed. Make sure to use Instance:FindFirstChildOfClass with the “Tool” class on the character to find any equipped tools

2 Likes

Is the tool being held in the backpack or in the player? If it’s in the player then your script won’t delete it.
When testing check to see where it’s located when it’s equipped.

I have changed the code to this but there’s an error which appears:

 Workspace.DeleteItems.Script:12: invalid argument #1 to 'pairs' (table expected, got nil)
local Players = game:GetService("Players")
local part = script.Parent

part.Touched:Connect(function(touch)
	if touch.Parent:FindFirstChild('Humanoid') then
		local character = touch.Parent
		local player = Players:GetPlayerFromCharacter(character)
		for _, v in pairs(player.Backpack:GetChildren()) do
			print(v.Name..' was removed for '..player.Name)
			v:Destroy()
		end
		for _, v in pairs(player:FindFirstChildOfClass("Tool")) do
			print(v.Name..' was removed for '..player.Name)
			v:Destroy()
		end
	end
end)

Isn’t it because GetChildren() creates a table? You may want to use something like WaitForChild.

Do a check to see if the contents of the backpack are nil first and if they are then
print("no backpack contents")
Don’t force the script to continue and error.

It may be taking a second for the items in the backpack to load in.

for _, v in pairs(player:FindFirstChildOfClass("Tool")) do  <- The error said it was here
			print(v.Name..' was removed for '..player.Name)
			v:Destroy()
		end

error:

 Workspace.DeleteItems.Script:12: invalid argument #1 to 'pairs' (table expected, got nil)

@UrAnoob991199
pairs are meant for dictionaries (tables with keys and values) ipairs are for normal tables that only have keys. replace pairs with ipairs or remove them completely that will work too, no need for any pairs.

make sure that you are destroying the tool from a server script and not a local script

Here. This script will work! :grin:

local Players = game:GetService("Players")
local part = script.Parent

part.Touched:Connect(function(touch)
	if touch.Parent:FindFirstChild('Humanoid') then
		local character = touch.Parent
		local player = Players:GetPlayerFromCharacter(character)
		for _, v in pairs(player.Backpack:GetChildren()) do
			print(v.Name..' was removed for '..player.Name)
			v:Destroy()
		end
         local toolInCharacter = player:FindFirstChildOfClass("Tool"))
		if toolInCharacter then
			 print(toolInCharacter.Name..' was removed for '..player.Name)
			toolInCharacter:Destroy()
		end
	end
end)
1 Like