Looping to see if an item doesn't exist not working

So I’m using this script

while wait(3) do
	local yes = game.ReplicatedStorage.Event
	if yes ~= nil then
		warn("An Item Was Found NIL")
	end
end

but when I destroy it nothing prints / warns.

This belongs in #help-and-feedback:scripting-support

~= means “not equals”

You are saying if yes does not equal nil then

while wait(3) do
local yes = game.ReplicatedStorage.Event
if yes.Parent ~= game.ReplicatedStorage then
warn(“An Item Was Found NIL”)
end
end

so this should work?

Simply indexing something that is nil will throw up an error i.e. you should attempt to look for a child using FindFirstChild("name") instead.

while (true) do
    wait(3);
	local event = game.ReplicatedStorage:FindFirstChild("Event");

	if (not event) then
		warn("An Item Was Found NIL");
	end
end
1 Like

Yes but you should not use that code anyway, using while loops to check for things are very messy and extremely inefficient. If you expect it’s an instance will be destroyed you can connect a changed event or an ancestry changed event.

I tried it and it didn’t work :frowning:
image

It doesn’t print or anything.

local event = game.ReplicatedStorage.Event

event.AncestryChanged:Connect(function(inst,value)
	if value == nil then
		print("object destroyed")
	end
end)

This is the code you should be using, never use while loops to check for things as I said they are very inefficient and also hard to work with.

It’s working on my side. It’s most likely a mistake in your code.

He still should not be using that code.

Nope still nothing.
here’s proof:
https://gyazo.com/22e56be83789e11d7415e96a32621c00 – I dont know why the video won’t embed but here:

My guy you code is erroring, you need to fix the error.

The error is from a different script, not this one.

Can you show your whole script

yep this is it
image

The code were you destroy the instance

It’s this one:

game.ReplicatedStorage.PlayerJoinedEvent:Destroy()

I’m assuming you are destroying the instance before the event connects.

Proof that it works
InstanceDestroyTest.rbxl (29.2 KB)

I’m not destroying anything I’m confused on why it won’t work.

Why aren’t you destroying anything? The whole point of the code is to detect when an Instance is destroyed.

I also realized you were running it from the command line, in that case can you send a screen shot of the instance in the explorer tree, I want to make sure the script is running properly.

I meant that the Remote Event isn’t being destroyed before I executed it, also i got it working, turns out it needs to be in a separate script by it’s self, not sure why.