Waiting For Object

Just a wonder;

repeat wait() until …

or

…:WaitForChild()

(… means an object path.)

I was wondering which one would be better for most scenarios when waiting for an object. I heard the second method of waiting for an object is better practice, but it would warn that the object could be infinitely yielded, if the object is something that needs loading, such as a player’s character’s torso. Which do do you think is good practice? Which works best when waiting for anything?

:WaitForChild is a better practice than repeat wait.

If you’re afraid of infinite yields, you can specify a maximum wait time within the :WaitForChild() parameters before it simply returns nil with the following:
local part = game.Workspace:WaitForChild("Part", 5)

1 Like

I’d say 2nd is better for optimisation and practise. But with custom objects and tables you’d use the top one to get the certain object/ement.

function kernal:waitFor(elementName)
	repeat RunService.Heartbeat:Wait() until self[elementName]
	
	return self[elementName]
end

Thats the code I have inside of my kernal that handles my modules. It’s an method to wait for a element within the table itself.

1 Like

I wouldn’t know a specific load time for the player’s character, due to some devices lagging upon loading the character once the player has joined the game.

Because I hadn’t known that, it helped; thanks for the tip!

Epic. Thanks for the info, I will keep this in mind!