This is something I always had in the back of my head. When the player joins, it takes time for them to be fully loaded, their player scripts, their guis, etc… but if I do wait for child for lets say a GUI in their playergui, is it possible that they can leave quickly and then the player instance is destroyed and I will get a infinite wait time error? I know that when you destroy something, it still actually exists but its parent is now nil if you try messing with the original variable of it, but I’m not sure if this is also the same as how players are unloaded in Roblox. I also have another question, do I have to worry about the player just taking forever to load? if I have a server script which wait for child for lets say a GUI, again, how long could it possible take? if it has a possibility of taking long, how should I handle the situation? just don’t do anything and ignore it? is it possible to have some sort of I guess multithread for waitforchild which will make it so that it won’t yield in the original script when waiting for child, and it will essentially just make another thread for that specific thing to load before I manipulate the instance? Thank you!
In my opinion, when the player leaves quickly, the local scripts that called the Instance:WaitForChild()
function will be destroyed upon the player leaving.
You can specify an optional time out value in the second parameter of the function if your game takes a lot of resources in order for the client to load fully. Check it out here.
You can just do this for example:
local Player = game:GetService("Players"):WaitForChild("3DesignD", 5 --[[timeout duration]])
if not Player then
print("Player did not load")
end
do you know anything about how server scripts will handle it? just infinite wait I’m guessing?
I am a bit lost of what you are trying to ask. Are you asking how server scripts handle the function if it gave a warning saying an infinite yield warning?
Correct me if I’m wrong, but the waitforchild times out after a few seconds. It doesn’t keep a constant loop if it can’t find the object. If you try to reference something in-game that does not exist, it errors out.
You are indeed correct, however you can specify a time-out duration in the second argument of the function to make it wait for a certain amount of seconds before timing out.
True, but unless you intentionally set it to a high number, it will just error out.
well, I guess to answer my question you would need to know specifically how players unload. If a player unloads, or leaves the game in the middle of loading, does everything in the process of loading not get loaded? If it does, it means that server scripts will have a wait for child infinite wait if that thing you tried to reference didn’t load, if it doesn’t, i could just put the object in a variable and if I detect its parent is nil then I wont manipulate it.
Oh I have another question now, if you delete a model with a bunch of children, will the children parents be the model or be nil?
I am quite unsure about this, the client could either let it load finish and unload it, or cancel the process of loading that instance.
I think you are confusing yourself with some grammar errors. But to put simply, I assuming you are trying to say that, if the stuffs do get loaded, then the function will have the infinite yield warning. This is false, because the stuffs did load, meaning the function is able to reference it.
How are you going to put the object in a variable if it doesn’t load?
Regardless of whether or not if the function meets an infinite yield warning, it’s just a warning after all, meaning it won’t entirely stop running your script like how an error does when an error is raised in a script. You can definitely use the function, and pair it with a retry system for a certain number of times.
sorry I articulated what I wrote a little too fast, I meant the other way around, if stuff do get loaded, then it wont have a infinite yield warning and I will put it in a variable to see if its parents are nil.
I don’t think that is necessary. You can always do conditional checks to see if the variable still exists if you ever reference it in your script.