Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

If something isn't there do..?

Asked by
iFlusters 355 Moderation Voter
9 years ago

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

Log in to vote
5
Answered by 9 years ago

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.

Ad

Answer this question