Repeat wait vs :Wait()

Simple question but is there any difference between using these two for waiting until the players character is loaded?

repeat wait() until Player.Character
Player.CharacterAdded:Wait()

The end result is the same but Player.CharacterAdded:Wait() could be considered more performant and returns the character when it loads

1 Like

local Character = Player.Character or Player.CharacterAdded:Wait()
just looks more clean to me personally

also you dont run the risk of your code magically yielding for no reason and you cant figure out why it wont run untill 24 hours later full of stress realizing it was the one piece of code. player.CharacterAdded:Wait() just yields till the object is added

also here

No difference in theory, but it’s better you use Player.CharacterAdded:Wait() since it is not constrained to a pipeline capped at a certain frequency (30Hz) and fires immediately when the character is added to the workspace as opposed to wait():

Consider this scenario using repeat wait() (and running at >30 fps):

  • Does the character exist? No. wait() and continue looping
  • 1/60 seconds later: Does the character exist? Yes, but you can’t continue because the thread is paused due to the wait(). Wait until it’s resumed by the task scheduler loser, lol

Now consider the scenario with player.CharacterAdded:Wait():

  • Thread paused until CharacterAdded is fire
  • Some time later: CharacterAdded fired and :Wait() will immediately return the character and resume the thread

In my opinion, Player.CharacterAdded:Wait() looks better too

8 Likes

There is a difference, the first method
repeat wait() until Player.Character
is a polling method that waits until you observe the Character.
The other, is signal based where instead of connecting a function you wait on the event to fire before resuming.

The later is generally better practice and more standard in programming industries

You meant Player.CharacterAdded:Wait(). Also this makes some difference: If the character already exists in place, the yielding will last forever until player respawns the character. Only get the character whenever needed.

I have used both, but in my experience, using the repeat method was more reliable. I kept getting errors when I used :Wait().

However, I still refuse to use the repeat method lol.

How does one get errors from the usage of :Wait()?

Allow me to clarify if it wasn’t already clear. The character was not apparently already loaded despite the fact that I used :Wait().

local Players = game:GetService("Players")

local localPlayer = Players.LocalPlayer or Players:GetPropertyChangedSignal("LocalPlayer"):Wait()
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()

print(character.Humanoid)

image

local Players = game:GetService("Players")

local localPlayer = Players.LocalPlayer or Players:GetPropertyChangedSignal("LocalPlayer"):Wait()
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()

repeat
	wait()
until character

print(character.Humanoid)

image

Which only gets weirder since if I only use the repeat method, I still get an error lol:

local Players = game:GetService("Players")

local localPlayer = Players.LocalPlayer or Players:GetPropertyChangedSignal("LocalPlayer"):Wait()
local character = localPlayer.Character or localPlayer

repeat
	wait()
until character

print(character.Humanoid)

image

That’s because Humanoid inside character usually isn’t loaded when CharacterAdded fires. You can literally just use :WaitForChild() to get around this problem:

local Players = game:GetService("Players")

local localPlayer = Players.LocalPlayer or Players:GetPropertyChangedSignal("LocalPlayer"):Wait()
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()

print(character:WaitForChild("Humanoid"))

That’s not the problem that :Wait() has, that’s just how CharacterAdded works.
Also on your last example, you’re setting character variable to player object instead if character exist when the code runs, that’s why it’s erroring.

1 Like

I know why it’s erroring. The whole idea of WaitForChild is dumb in coding, though, which is the whole issue with Roblox code lol. I was just showing an example, not a use-case.

please explain how it is “dumb” in coding

1 Like

Pardon me if it seemed to be spoken from an excess of pride in scripting, because it does not. I know that I am not a high-level programmer.

However, I do still stick with the idea that WaitForChild() does rather seem like a silly function. It seems to take on the role of a small band-aid over the massive issue that the Roblox platform has: lack of confidence in the existence of a child.

But if that alone still does not sway your stance on it, then I apologize for giving my opinion and not being able to change your mind. I am probably asking too much of Roblox.

as it may seem silly, instances need to load in and I don’t think that is a problem and instances loading in will always be a thing, all WaitForChild does is waits for the child to load

btw this isn’t a massive issue

1 Like