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

If something isn't there do..?

Okay, so I've tried a few methods such as:

base = script.Parent.Base

while true do
wait()

if base == nil then
	print("Base isn't there")
end
end

--Will finished code

So basically, I'm trying to make a script that will keep checking to see if something is there, if not, then.. whatever the code is put after, I will finish off the code myself.

Any help would be appreciated.

1 answer

If you can get a reference (variable) what object you want to check, the best way to execute code when it gets removed is to use the Changed event and then check the Parent property of the object.

local part = game.Workspace.BasePlate

part.Changed:connect(function()
	if not part.Parent then
		print("The part has been removed.")
	end
end)

If you can't get a reference to the object, then the FindFirstChild function would be appropriate.

if not game.Workspace:FindFirstChild("BasePlate") then
	print("The part was removed.")
end

The reason simply comparing the variable to nil doesn't work (if base == nil then) is because the variable is still a reference to that object, regardless of if the Parent property is nil or not. In some cases, you can even put the removed object back in the world this way, by just setting Parent to an object again. However, if the object was removed from the world with the Destroy method, this won't work.

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