:wait() vs wait()

So recently, I’ve been tasked with going through an old place my friend got from his friend and fixing the scripts - I noticed an API with the deprecated LoadLibrary() which was related to the leaderboard stats. After converting (most) of it to a more “new” version, I found a script that registers whether a player has died.

Player.CharacterAdded:connect(function(Character)
	local Humanoid = Character:WaitForChild("Humanoid")
	Humanoid.Died:wait()
	leaderstats.Deaths.Value = leaderstats.Deaths.Value + 1
	--Check if killed by player etc
end)

Which drew my attention to line 3:

Humanoid.Died:wait()

After looking into it a bit, it seems people use :wait() alongside an event - i.e. event:wait()

What does :wait() do? If it was just a matter of waiting before the event ran or afterwards, why can’t you just do wait() the line before/after? Surely there’s a difference between :wait() and wait()?

Tl;dr Difference between “wait()” and “event:wait()”

5 Likes

wait() will simply pause the script for a very short period, but event:Wait() will pause the script until the event fires. You can learn more here: Events | Documentation - Roblox Creator Hub

26 Likes

Thanks for the link! Much appreciated. :slight_smile:

3 Likes

FYI the :Wait() method of events should have an uppercase “W”. The lowercase one is deprecated. Technically still works, but best to stick with what is supported. The normal wait() function is still lowercase.

11 Likes

The lowercase w still works. I have a car that uses

  while true do
      game:GetService("RunService"):wait()
  end

It still works :>

2 Likes

It still works, but it’s deprecated. When something is deprecated, it’s not removed (because that could break a lot of stuff), but it will no longer be supported. Thus, if it does break (or they make a change to the capitalized Wait version), then it will probably not be fixed.

6 Likes

I have a car that uses

while true do
    game:GetService("RunService"):wait()
end

Why?

What is the use of looping forever to :Wait() for RunService? Is that really something you use?

3 Likes

Vehicles like the ones in jailbreak use this same code to calculate their suspension physics.

1 Like