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")

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”)

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
	local event = game.ReplicatedStorage:FindFirstChild("Event");

	if (not event) then
		warn("An Item Was Found NIL");
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:

It doesn’t print or anything.

local event = game.ReplicatedStorage.Event

	if value == nil then
		print("object destroyed")

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

The code were you destroy the instance

It’s this one:


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.