I know that wait delays a script by a certain amount but how long does using wait delay a script? If I used wait(30) in a script does that delay the script for 30 seconds or 30 milliseconds? Thank you for your time
wait(SECONDS) --waits in seconds
wait() --waits 0.03 seconds iirc
Yep, it waits in seconds, not milliseconds
Try to avoid using wait() without argument (aka with empty brackets) tho. Many people consider this to be a bad practice.
It will delay for 30 seconds. wait(0.03)
would delay for 30 milliseconds.
A good alternative could be yielding for an event to fire. I usually use heartbeat-
game["Run Service"].Heartbeat:Wait() --//fires up to 60 times per second
Is there a reason you use game["Run Service"]
instead of using :GetService()
?
You can use :GetService()
if you’d like, but @C_Sharper decided to reach Run Service
in a regular fashion.
the best possible case is 0.03 seconds but on average its a bit higher than that. Now heartbeat:Wait() runs into the same problems but at a much minute level. In general people suggest not to use wait() because people will do something like this
local sum = 0
for i=1,1000 do
sum += i
wait()
end
return sum
and then wonder why their integral function takes 30 seconds to compute the integral from range 0 to 1 of sine of x. Ultimately for things like this you can just get rid of wait() completely but if you need some sort of delaying system that needs to be accurate the best you can do is to use event based waiting with heartbeat:Wait(). It should also be noted that theres nothing wrong with using wait(seconds) for like intermission scripts and stuff
Just curious, what does wait(.5) mean? Is it the same as wait(0.5)?
its the same. they wait half a second
You don’t have to write 0
if it comes before decimals. (Not just in wait
function)
It’s better to use game:GetService("RunService")
most of the time
Also, Heartbeat:Wait()
is tied to physics (it fires every frame after the physics simulation has completed) , wich means it dosent fire exactly every rendered frame
The solution to waiting every frame is game:GetService("RunService").RenderStepped:Wait()
, though this runs only locally (on local scripts).
So Heartbeat:Wait()
for server scripts and RenderStepped:Wait()
for local scripts. There’s much more to the :Wait()
lore though.