Tool:Destroy() issue (server script)

I want to fix the Tool:Destroy() problem. It doesn’t destroy, equipped or unequipped.

The tool’s CanBeDropped is turned off, tool is Enabled, and has no more than one “Handle”. The “Handle” is a BasePart.

The function:

	print("Cleaning up hiders")
	for _, hider in ipairs(modules.game_stuff.hiders) do
		if hider then
			local backpack: Backpack = hider:WaitForChild("Backpack")
			local hider_char: Model = hider.Character or hider.CharacterAdded:Wait()

			-- destroy the tools
			for _, tool in ipairs(backpack:GetChildren()) do
				if not tool then
					-- the equipped tool
					for _, tool in pairs(hider_char:GetChildren()) do
						if tool and tool:IsA("Tool") then
							print("Destroying equipped:", tool.Name)
							tool:Destroy()
						end
					end
				else
					-- the unequipped tools
					if tool and tool:IsA("Tool") then
						print("Destroying unequipped:", tool.Name)
						tool:Destroy()
					end
				end
			end
		end
	end

Only “Cleaning up hiders” is in the output. :frowning_face:

1 Like

I fail to see the point in this
if not tool then
An object will never be falsey, thus there’s no point to this (not to mention it will cause the hider_char check to not run ever). Rather to make sure that the tools are destroyed, you could simply skip that check and go to destroying the tools.

local function destroyHiderTools(par)
	if par == nil then return end
	for _, tool in ipairs(par:children()) do
		if tool.ClassName == "Tool" then
			tool:Destroy()
		end
	end
end

...

for _, hider in ipairs(modules.game_stuff.hiders) do
	print("Removing tools for <", hider.Name, ">")
	local backpack = hider:FindFirstChild("Backpack")
	local hider_char = hider.Character
	
	destroyHiderTools(backpack)
	destroyHiderTools(hider_char)
end
2 Likes

Simple answer is to loop through character children and find a tool then destroy it

2 Likes

either hiders is a dictionary and ipairs doesn’t work on dictionaries or you forgot to require the hiders module

2 Likes

The tools still don’t want to destroy for some reason.

1 Like

Are there any players present in the modules.game_stuff.hiders table? Let’s start from there.

2 Likes

I am using table.insert for hiders: table.insert(modules.game_stuff.hiders, plr)

1 Like

I made a quick change to the code I’d written out before. What outputs when it runs?

2 Likes

Removing tools for < Player1 >
Children is not a valid member of Backpack "Players.Player1.Backpack"

1 Like

That error doesn’t make sense. Is there another script that’s attempting to index an object called Children?

2 Likes

I only changed children to Children: for _, tool in ipairs(par:Children()) do

1 Like

Ah, that explains that. Remember, lua is case-sensitive. There’s no method for objects called Children. There is, however, one called GetChildren. children is the deprecated form of GetChildren that I still use.

2 Likes

Output said not today or nothing is output

Kinda lost me here. An output of not today or nothing is outputted? Is there no longer an output of Removing tools for < hider_name >?

1 Like

Nothing is outputted. Sorry for confusion

That would mean there’s no players present in the modules.game_stuff.hiders table. Is there some reason why they’re no longer being added?

1 Like

it warns ‘no hiders’ after using next()

Where was that change made? The initial code I’d written out nor does the portion you provided previously contained next.

1 Like

Here’s the debugging I added:

	local function destroy_hider_tools(par)
		if par == nil then return end
		for _, tool in ipairs(par:GetChildren()) do
			if tool:IsA("Tool") then
				tool:Destroy()
			end
		end
	end
	
	if next(modules.game_stuff.hiders) ~= nil then
		print("Cleaning up hiders", modules.game_stuff.hiders)
		for _, hider in ipairs(modules.game_stuff.hiders) do
			print("Removing tools for <", hider.Name, ">")
			local backpack = hider:FindFirstChild("Backpack")
			local hider_char = hider.Character or hider.CharacterAdded:Wait()

			if backpack ~= nil then
				destroy_hider_tools(backpack)
			else
				warn("No backpack!")
				return
			end
			if hider_char ~= nil then
				destroy_hider_tools(hider_char)
			else
				warn("No character!")
				return
			end
		end
		
		-- after the hiders' cleanup, say cheese
		table.clear(modules.game_stuff.hiders)
	else
		warn("No hiders!")
		return
	end

There’s no point to including
if next(modules.game_stuff.hiders) ~= nil then
A generic for will not run the code if the table’s empty.

if backpack ~= nil then
	destroy_hider_tools(backpack)
else
	warn("No backpack!")
	return
end

I don’t recommend having this check present. If the hider doesn’t have a backpack nor character, it’ll exit out the code and won’t check the rest of the hiders. The checks there can be removed entirely, as there’s already a safe-check for that in the destroy_hider_tools code.

local backpack = hider:FindFirstChild("Backpack")
local hider_char = hider.Character

destroyHiderTools(backpack)
destroyHiderTools(hider_char)
1 Like