There needs to be a better way to wait until objects are destroyed (need help)

Now basically, repeat wait() until game.Players.LocalPlayer.PlayerGui.StarterGui == nil doesn’t work. (for reference, StarterGui is a folder in PlayerGui containing titlescreen things). Every time ive ever used this in any case, it always throws an error object doesnt exist WHEN YOUR LITERALLY WAITING UNTIL IT DOESN’T!
Solutions?

1 Like

Destroying.

i want to slap myself on the head

Fun fact: I take it back, throws me the same error

game.Players.LocalPlayer.PlayerGui.StarterGui.Destroying:Connect(function()
	
end)

Open a blank template and press play.

In Explorer panel, expand the Players folder, then player’s folder.

You will see PlayerGui and inside that you will see Freecam.

This is default Rbolox Stuff.

You don’t need to destroy it.

It’s not particularly clear what’s nil here since you didn’t say in the thread. You’ll know what the nil instance is when you read the error message: “attempt to index nil with something”.

LocalPlayer implicitly exists on LocalScripts and so does PlayerGui, but it doesn’t exist in a Script. StarterGui isn’t guaranteed to exist either, so ideally you’d want to use WaitForChild on it or something. If by any chance StarterGui does get deleted, the next iteration will attempt to find something that doesn’t exist. It also can’t be equal to nil.

The proper way to write this out would be

repeat task.wait() until game:GetService("Players").LocalPlayer.PlayerGui:FindFirstChild("StarterGui") == nil

Though I don’t recommend repeat loops for scenarios like this. Signals are better for most cases where you need loops, hence suggesting Destroying. Though you’re accessing it from the wrong environment it looks like, or accessing it too early (which can be solved with WaitForChild).

Also: this only works if you created something called StarterGui. StarterGui itself as you see it in the explorer does not get wholly copied to a player’s PlayerGui.

You didnt read it correctly.
Also startergui doesnt even exist in the player object, so destroying nothing doesnt really work

Works, I swear ive done that before but whatever

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.