Many scripts want to run little bits of code at frequent intervals. For example, a script might update the behavior of a zombie. It needs to update conditions repeatedly, like checking the location of the nearest player.
Today, ROBLOX’s Chief Scientist, Erik Cassel, discusses proper use of the wait function.
The Problem
A very common way to do this in ROBLOX is with the handy “wait” statement. Here is an example:
There is one problem with this code. Can you see it? The wait function takes an optional argument, which is the time that the script should sleep. If you don’t supply an argument, then the function will typically return in 0.03 seconds. That means the script is looking for an enemy to attack 30 times a second! The findClosestEnemy function isn’t cheap. It probably needs to iterate over all the players and find the closest one. That might be fine if you have one zombie in your game, but what about a hoard of 50 zombies? Do you really want 50 zombies rethinking who they will attack 30 times a second? That amounts to 1,500 enemy updates per second.
This isn’t a contrived example. I’ve been analyzing laggy games. A large fraction of them are laggy because of scripts that suck up all the compute time.
The Solution
Let’s think about zombies for a minute. Zombies are stupid. Once a zombie finds an enemy, he is unlikely to change his mind and go after a new enemy for a while. Let’s make a tiny change to our script:
We supplied a reasonable wait time of five seconds. Once the zombie spots a player, it will go after him for five seconds before looking to see if anybody else is closer. Now our 50-zombie game does 10 updates per second, rather than 1,500.
I have found games that try to wait for 0.0000001 seconds. That means they try to run complex operations 10 million times per second. We may have fast computers these days, but not that fast! These kinds of games suffer from terrible lag.
If your game is laggy, check your script performance. In ROBLOX Studio, there is a great Script Performance panel. Look down the Activity column. It will tell you what percentage of your overall time that a script is taking up. Anything greater than 3% should set off alarm bells.
Better Resuming from a Wait
Another thing to keep in mind is that wait is not guaranteed to return in precisely the time interval you requested. Let’s say your game fills up with 1,000 zombies. Even our optimized scripts might take up a lot of time in aggregate. In that case, ROBLOX will decide that it can’t run all the scripts fast enough, and it will automatically wait longer than requested. We do this because ROBLOX needs to run other processes, like networking, physics and rendering. By “throttling” the script code, we can try to avoid lag.
Let’s write a script that gradually changes the time of day. If you put this script in a game you will see the sun gradually rising:
The script asks to wait for one quarter of a second. However, let’s say that your game is temporarily running out of CPU time. In that case ROBLOX will decide to make your script wait longer than 0.25 seconds, and the sun will rise slower! Here is the solution to the problem:
As stated in the documentation, the wait function returns the actual elapsed time. With that bit of information, we can adjust the time of day accordingly. If the elapsed time is longer than 0.25 seconds, then we’ll skip the time of day forward proportionally.
Conclusion
Lag is often preventable. I’ve found that many games are laggy because of runaway scripts that spend valuable compute time on unnecessarily frequent computation. If you use wait correctly, you’ll be on the way to lag-free game building.



Now I feel stupid. D:
wait i think made my game go down hill soooo much like 100% i had to make it inactive
The new wait() function update ruined A LOT of games, including one of mine. i didn’t see anything wrong with the way it was before…. actually i think it was 100x better. But, the way it is now, i just dont wanna play anymore cosidering how long it took me to create the scripts depending on the wait() function. it would take another 3 months to redo it… Please change it back… :C
And I don’t understand Lua. LOL I may be able to make SOME sense of it, but I don’t know how to script. I’ll learn, though! :P
NOOOOO, the wait() was perfect!! please change it back!!! it causes major lagg in games that already use wait() rather than wait(0.5). please i beg you roblox, change it back!!! PLEASE???
Who’s hand is that? :P
Gee, thanks for the condescending article on how we’re not coding properly, when Roblox Gear is glitchier than Fallout 3 V1.0
the client is faster than server what happen 2 clone method it lag the server but not the client.
This explains why in Davidii’s Survival 202 there would be a lot of lag when there were large fires. Every brick in the world would get examined for its distance-from-fire and whether it’s flammable. When there’s a few thousand blocks in the world, that’s simply too much computation for the server.
Not bad solution. But what about scanners? They have to run constant checks using ‘Wait()’. Unless there’s a complex version I haven’t discovered yet.. Also, there’s 2 solutions you have created here. Anti-lag and preventing players from getting zombies to chase other players rather than themselves. Unless they time it right of course..
cool roblox
Going to implement this into future scripts, thanks!
I wish Roblox made a new ‘Graduation Cap 2012′.:)
Well.. the day-night time optimization is pretty smart. I actually heard of that technique 2 years ago, in a programming language Actionscript 3.0(I do know how to script in that prog. lang.) The section was titled “Time-based Animation”, where the object moves according to the amount of time passed, so the object will move at the same distance at the same amount of time, regardless of the frame-rate.
roblox u should make an antilag script.finish all updates like the ingame wardrobe k?
There are no such thing as Antilag scripts. They make your place more laggy.
Not true. While most of them are simply blank scripts that do nothing but waste memory, or a complicated way of doing nothing, some do work. For example. Oysi’s.
I should help you ROBLOX, i’m known one of the scripters (what i’m told from my work!)-jksdfg
they have just type in in free models antilag these pages of it
Thanks again guys for another great column of advice! I myself will be inspecting my games for efficiency, i know that.
Why are so many people complaining about other features tehy want released here if this is just a small story about scripting they probably made to improve user enjoyment and give scripters a little advice, and probably because they haven’t had an article for a bit and wanted to make one.
This is nice and all, but cant you fix the exploiting? Also, when is the buoyancy update going to be released?
:P ROBLOX is trying to work on lag and that buoyancy. This is a big step forward for all the players who didn’t know about this, scripter or not, because some witty players might be able to edit free models now to run more effectively.
roblox patched exploits on may 17 2012
What does
Wait Example 5As stated in the documentation, the wait function returns the actual elapsed time. With that bit of information, we can adjust the time of day accordingly. If the elapsed time is longer than 0.25
mean?
> games that try to wait for 0.0000001 seconds
Okay, what? I thought wait() was pretty much hardcoded to have an absolute minimum wait time of one frame (not sure if logic frame – like Minecraft Ticks – or render frame, but either way “default wait is 0.03s” -> 30fps -> reasonable explanation).
In fact, I’m very sure wait() can’t be less than one frame, because I’ve seen scripts where they actually dared to wait(0) (why have a wait at all then?), and it didn’t cause an Infinite While True Do Crash like not having a wait() at all would.
hear me roblox over 1000 people quit roblox last week cause 1 the catalog is broken 2 not aw lot of people know how to script 3 its just starting to fail im going to be one of them finish those updates
you might lose money so when you see if i can say this out loud HEAR ME ROBLOX!!!!!
no the catalog is not broken
And where did you get these facts?
ok so here is the catalog thing fact me my bro and my sister and my cousin awnd my cousins cousins and a lot of other people i know agree the recently updated models dont show
and i know lots of peolpe i see lot of people that quit last week
Not a bad bit of advice,but still,GET TO THE PICTURE!!!EXPLOITING AND THE TRADING SYSTEM ARE MORE IMPORTANT THAN SCRIPTING ADVICE BROSEPHS!!!Also,I like trains :P
So, trading is more important than better and less laggy games?
Gameplay over customization. I personally knew that wait()’s were laggy, but I did not know about the activity panel, so that would be nice.
Also, I had NO IDEA that wait returned the actual time delayed! You can do that to make scripts that auto-correct.
I want the trade system to come out pretty badly. I have been waiting awhile for that, But good.
May I add, sometimes while wait() do isn’t a good way to check for things. Using the .Changed event or .DescendantAdded, or whatever it is you need to check is much more efficient. However, using .Changed for moving parts, the workspace or camera isn’t efficient, because they change frequently.
*below* lol
Everybody knows that scripts make a game slow even Anti-Lag scripts! Like zombie scripts make the game as slow as a zombie! Posted by UnionROBLOX on Roblox.
Thank you for sharing that with us. This could help with alot of them zombie game’s lagg ishew’s. (Pardon me dyslexia)
Well, You can make zombies with zombie starter kits and the zombie script is already inside the zombie.
Anti-Lag scripts don’t work. They just create more workload to process, which in turn would create more ‘lag’ or delayed networking.
Good. Very Good.
Agreed.
Now I know why games with tons of “zembie” AIs are laggy.
well, the fact that zombies dont really do much, zombies arent that exciting anymore. we need a way to make them more exciting!
i wonder whos hand that is lol
Zombies aren’t stupid, they just don’t have brains! :P