Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What's the difference between using :Wait() and wait() in this scenario?

I read somewhere that using :Wait() along with an event would significantly improve performance compared to just using wait(). I'm wondering if the line of codes that i wrote has any difference and which one is more practical?

First code

local number = 0

spawn(function()
	repeat wait() until number == 10
	print('Ten!')
end)

for i = 1,10 do
	wait(1)
	number = number + 1
	print(number)
end

Second code

local runService = game:GetService('RunService')

local number = 0

spawn(function()
	repeat runService.Stepped:Wait() until number == 10
	print('Ten!')
end)

for i = 1,10 do
	wait(1)
	number = number + 1
	print(number)
end

1 comment

1

:Wait() is a method of RBXScriptSignal. It is a yield function, meaning it will pause the current Lua thread until a response is returned from the event.

1 answer

✓ Accepted answer

wait() will try to wait for at least 1/30 seconds but it can wait longer if the game is lagging, whereas Stepped:Wait() will wait exactly a frame You should use Stepped:Wait() over wait() if you can

1 comment

0

It will regulate the load-time between the frame-rates of different devices. Similar effect to void Update {} from Unity Engine.

This discussion is preserved as it was. New answers, comments, and votes are closed.