My script runs too early, what solutions are there?

Ooh! Sorry I was not understanding. I have encountered this same problem before. The fix I have found is to put this code near the beginning of your script, which relies on AncestryChanged to tell when the character’s parent is no longer nil.

while character.Parent == nil do
	character.AncestryChanged:wait()
end

This should solve the problem.

1 Like

This would probably be better.

local rs = game:GetService(“RunService”)

repeat rs.Heartbeat:Wait() until character and character.Parent

If you’re just waiting for it to be in the workspace you could do until character.Parent == workspace

That would not be better. Your’s runs every frame, putting a significant strain on performance, whereas Excess’s only runs when the Parent changes.


In regards to OP, this is an unfortunate consequence of the current avatar loading event order. Currently, the only way to guarantee the character will be fully loaded by the time your script runs is by calling LoadCharacter and waiting for it to return. This should be fixed at some point, and is planned, but for some reason it’s been heavily delayed.

4 Likes

That technically uses up more resources though, since you are checking every Heartbeat, instead of waiting for the AncestryChanged event.

3 Likes

Oh right. Sorry lol I didn’t read it right

Just a heads up, the while loop isn’t needed since the character’s parent is currently nil. When the character is reparented, its new parent will never be nil.

1 Like

It is just a bit of coverage in case something goes haywire. This is generally true, but you never know what might happen.

No, I mean logically, something parented to nil cannot be parented to nil. There’s no reparenting.

1 Like

Very good point. However, I still feel safer using a loop, just in case the event somehow misfired. You are right that it is logicality not necessary though :+1:.