Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why won't the module "Destroy()" work on my tool?

I have these weights. When I unequip them it is supposed to return them to their original spots. The handle disappears from the tool and it stays in my backpack. I also get something in the output saying -

"16:26:48.820 - Something unexpectedly tried to set the parent of Weights to NULL while trying to set the parent of Weights. Current parent is Backpack."

Anyways here is the code. Thanks in advance

Code inside tool

local tool = script.Parent
local handle = tool:WaitForChild('Handle')
local loop

game.Players.PlayerAdded:Connect(function(plr)
	tool.Equipped:Connect(function()
		loop = true
		local p = workspace:WaitForChild(plr.Name)
		local waittime = plr.WaitTime.WaitTimeStr
		local Power = plr:WaitForChild('Stats'):WaitForChild('Power')
		repeat
			wait(waittime.Value)
			Power.Value = Power.Value + 1
			waittime.Value = waittime.Value + 1
		until loop == false
	end) 

	tool.Unequipped:Connect(function()
		loop = false
		local newHold = tool:Clone()
		plr.Backpack:WaitForChild('Weights'):Destroy()
		newHold.Parent = game.Workspace
		newHold.Name = 'Weights'
		newHold.Handle.Position = Vector3.new(208.402, 3, 42.727)
	end)
end)

Code inside Handle

local part = script.Parent
game.Players.PlayerAdded:Connect(function(plr)
	local function OnTouched(hit)
		if hit.Parent.Name == plr.Name then
			part.Parent = plr.Backpack
		end
	end
end)

1 answer

✓ Accepted answer

That warning happens when you try to destroy something right after it was created/reparented. You need to wait a bit before destroying the tool (pun intended).

local tool = plr.Backpack:WaitForChild('Weights')
wait()
tool:Destroy()

Or you can do something like this but I generally don't recommend it unless nothing else works:

local tool = plr.Backpack:WaitForChild('Weights')
repeat
	wait()
	tool:Destroy()
until not tool.Parent

This discussion is preserved as it was. New answers, comments, and votes are closed.