Repeat wait() until tool doesn't exist doesn't work!

I’m trying to let the script wait at "this part" until the tool doesn’t exist anymore, but it doesn’t work. I tried functions, but they don’t stop the script at that part. What did I do wrong? (This part is in a while true do loop).

local ToolClone = tool:Clone()
	ToolClone.Parent = playersAlive[math.random(1, #playersAlive)]:WaitForChild("Backpack")

	repeat wait() until not ToolClone -- This part
	wait(5)

The reference to an instance will always exist, and therefore be true. You may want to index its parent property, since instances are parented to nil when destroyed.

In addition, a repeat-wait loop is highly inefficient - I would consider a repeat loop which waits on the parent property changing (can get a RBXScriptSignal for this from GetPropertyChangedSignal) and sees if it is to be nil.

ie.

local parent;
local parent_changed = ToolClone:GetPropertyChangedSignal("Parent");

repeat
    parent = parent_changed:Wait();
until (not parent)
1 Like

Thank you for your help, but this is when the parent is changed, I’m looking for the change “Removed”. Because the tool’s parent will change frequently in-game.

Parent.ChildRemoved might be what you’re looking for.

1 Like

Okay, I got this, but this doesn’t work.

local ToolClone = tool:Clone()
local parent;
	local parent_removed = ToolClone:GetPropertyChangedSignal("ChildRemoved");

	repeat
		parent = parent_removed:Wait();
	until (not parent)
    wait(5)
repeat wait() until ToolClone.Parent == nil

Yeah repeat wait()'s are not good but who cares! We are not building a real-time application where a 1ms matters

1 Like

This will keep running until the parent is nil, just runs when the parent is changed. The current solution continuously run until it’s nil which is inefficient as I previously mentioned - wait() can yield longer and longer the more it is used.

1 Like

Sorry for the long wait, but ChildRemoved is an event, not a property, so instead of using ToolClone:GetPropertyChangedSignal, you would use ToolClone.ChildRemoved

1 Like