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.



Oh COME ON.
Just get to the exploiter problems!
THEN focus on lag and things!
I agree.the exploitng is very bad.
excactly, do you know how many times i have seen a hacked place?
Agreed, ROBLOX is becoming a hacking playground.
i agree also to many haxors…jsut happened in 2012..odd isnt?
Great tutorial. Although in my game, I have SMART AI’s, so they navigate around obstacles and jump over/climb over them. Really advanced coding. Anyways, since it does SO many calculations to find out if the player/target is to the left/right/above/below/behind something, this is REALLY useful. And it is also very BAD too. Because if it needs to jump over something, it will wait 5 seconds to jump. So, I shall make it .25 seconds and use some handy-dandy globals to remove some lag. :D
END OF LAG YAH :D
Keep it up! really useful advive
O hai
O hai to oyu. =3
Ohaider. :3
Hai yall. :3
I haven’t had this problem before but I’ll try it with my scripts. I hope people use this in zombie games. Now I can help my buddies without getting attacked instantly. I get maybe 2 – 5 seconds to kill it! :D
I wish scripting was eisyer, I can’t script. D:
exactly my problem. my games suck
I agree with you there. I can kind of make sense of some of the stuff in scripts, but most of it is gibberish to me.
Its just like writing journal for the game. Its like commands and stuff.
I know how to script. :3
At first I felt this way then I learned a bit of Java language (thanks code academy.com!) and it is practically the same language. Never realized the similarity.
this is awesome but what about 20 zombies you can get one too chase that person but how do you get the rest of 19 zombies to chase that person too alsoi how do you make yourown gunn and your own zombie
Solution to Zombies Following Other Zombies:
In the FindNearestTorso function in the Script in the zombie is this line:
if (temp2.className == “Model”) and (temp2 ~= script.Parent) — Is target a model, and not me?
Change it to something like this:
if (temp2.className == “Model”) and (temp2 ~= script.Parent) and (temp2.Name ~= script.Parent.Name) and (temp2.Name ~= “A.I.”)then — Is target also, not named the same as me, and not named “A.I”?
GL,
Brian
Please will you guys updates update roblox test site
Wow cool! No lag no more :D
No, this is just an advice. People will still have to change their scripts to avoid wait(). Simply saying people to change their scripts does NOT immediately create less lag. -.-
That’s nice.
Thank you for the help. This will really help speed up my games. I use too many
wait()
good! now my epic duck is better!!!!!!!!
I knew that :)
@lackingstriker1000
They wont add this to games the creator dose,it’s his/her loss if they don’t see this.
what is wrong with the games i play when i click play it takes about 20 minutes to come up and sometimes never comes up!
is there a way to override the default lowest-possible delay time? what i’m trying to do mostly relates to being able to tween a GUI object’s transparency, and 1/30 second doesn’t look like it’s smooth enough.
That’s impossible. Use your common sense. wait() runs at the frame-rate. You can’t just say “I want the game to go twice as fast”, the reason wait() isn’t running quickly enough is your processor isn’t fast enough to process the action fast enough (Or in the case of a non-local script, roblox’s servers)
Actually, on free models, I found a transparency Intro-GUI that had perfectly filtered transparency fade. It wasn’t fast, it just had a filter. I was blown away.
My code had PP in it. =3
Well,
for i = 1, 10 do and using wait() seems smooth enough for me.
So does this mean “wait (5)” Dosnt work anymore?
While true do?
While loops with no real statement.
Holy.
Learn to use events, bros.
Well thank you for the hint also the script :3
Hmm
Zombies…
Roblox’s Typical zombies are have brown chestpalte and brown legs and green head and green arms…
What Changes Doing in The Stamper Zombie…
I CANT WAITTTTTTT
Ideas: We have a Zombie Hat Using this OR
Make a New Meshes [like terordactyl]Moving and atacking…
We Know it Easter zombies Are Stupid, they says “BLOAAHHRGH BLUULUARGHHH BLUUUUGH”
They Make A New Sound I Think…
And They Useful in stamper tool+with a cage xd
I don’t even know how to create scripts or whatever… But it’s still usefull! xD
Dark886/D4rk886 could use this for his “Base War”
Then type of zombie functioning is fine, however it would be better to baych process them…
This is helpful.
I’ve played ROBLOX for 4, almost 5 years now.. I know how to use “wait” correctly. Thanks for taking the time in making this though.
This has really helped me since I am just starting scripting and did not fully understand the “wait” function. Now I do!
Also I am making a plugin so anyone who can help, PM me at ultimatecreatorA1.
~ultimatecreatorA1
#worth the weight
Thanks for the hint, i have games with one brick and like one script that lag you out when you enter. Now I know not to use wait(0.00000000000000001)
The problem: wait(0.00000000001) will actually be the same as wait()
Anything less then one ‘Frame’, or 0.03 or so will be adjusted to wait().
But yes. I would suggest doing wait(0.7) for most tight loops, and wait(5+) for loose ones.
Thank you Roblox, I was having trouble understanding the wait concept until now. :)
Good for my new robots.
If you do not understand this article you probably aren’t that familiar with studio or lua. this article helps.
That’s good now we can finally have less laggy games Are you going to ad this to every game on roblox?
Lackingstriker1000,This isn’t an update,Its optimizing the script so it runs smoothly without creating lag.The creators of the place themselves need to do this to their scripts.
Lackingstriker1000, Gingka2k10′s right this isn’t a update it’s just advice. -_-
(I’m a bad scripter and this won’t help me much.)
In my game, I use other peoples scripts, so people like me won’t have any effects unless we download an updated script, which I’m too lazy to do :b
thats dumb
Your just saying that because you can’t script.
I agree to you eic dude
You’re*
lol.
Another way to reduce lag is by simply NOT using ‘wait()’. In this example, p is the player, and kills in the Kills stat in the leaderboard:
while true do
wait()
if kills.Value >= 50 then
game:GetService(“BadgeService”):AwardBadge(p.userId,13337)
end
end
That will run 2000 times EVERY second. In most cases, that script would be in the leaderboard. So say you have a full server of 30, it will be running 60,000 times a SECOND.
Try this instead:
kills.Changed:connect(function()
if kills.Value >= 50 then
game:GetService(“BadgeService”):AwardBadge(p.userId,13337)
end
end)
This will only run when the stat changes.
@Zack That Is A Great Idea To Patch It, But I Really Dont Think You Can Patch It.
@Zack i really dont know if you can patch exploits yet, but its a start.
@Smiley how do i patch it?
hey what is going on with roblox? pengychat2 is having problems while playing roblox. someone might be using a exploit that kicks everyone off their servers.
This seems very interesting and very helpful. Hmmm, now if only I knew how to script.
Interesting, but my form of scripting is better that this layout. Dont need this. (Opionion)
But say You have 1,00 zombies in your place. You edit a wait script into one for maybe… 5.0 seconds.
After that zombie starts to follow you you then want the other 999 zombies to chase that player. How can that be fixed?
findClosestEnemy() would simply return as the closest enemy the one closest to a specific point – you solve this by having the script on every zombie to track from a position relative to them(say, closest to that zombie’s torso for instance)
You have to split the wait-time.
If you have 5 seconds of waiting, you would have to put 2.5 seconds before the findClosestEnyme function, and the other 2.5 after it.
That’s a great way to reduce lag especially in any zombie games I would plan on making in the future. Thanks, for the tips it was really understandable.
If I can do the same with the zombie and a gun I can maybe think of making a aimbot.
Makes sense, but being wise with wait()s isn’t going to make every game out there not-laggy. My game lags because of physics. Now if it were possible to somehow downgrade the quality of physics, make it be less precise, that could maybe help also.
My Roblox Studio won’t show the little screen to let me customize my scripts :(
If you know how, message me on Roblox please. My username is BarbaricBrandon
thanks a lot, i actually needed this, my games were pretty laggy and now i could change that.
Another important thing to remember: before starting a script with a “while” loop ALWAYS double check for the wait function or it will lag up the game as well.
Wouldn’t it be easier to create raycasting or magnitude intercepted event?
firing the function every time the zombie moved would cause bugs, and also be very inefficient(the events pretty much fire every frame, which means that it will be just as worse as a simple no argument wait() function call.
super cool :) that’ll help a lot
Weird, I never get lag anywhere even with infinite loops waiting only 0.0000000001 seconds (the wait time my zombies normally use for both ray-casting, interpolated path finding, and enemy tracking) per cycle.
your 0.0000000001 seconds are automatically rounded to 1/30 second, courtesy of roblox’s lag management system.
Not sure if trolling or stupid. It’s not a ‘lag managermenet system’, it’s called roblox’s framerate.
So THATS how you prevent lag….
… in a good way.
Awesome…..I guess….
Is there no way to make scripts or place none lag
But it can always HELP with lag. .-.
Dude, they’re improving ROBLOX. What do you expect them to do. Read the conclusion.
That is very intresting but i have to be true, i didn’t understand 20% of that.
This seems good
Where is this ‘Script Performance panel’
Click “View” on the studio, then hit “performance” I think.
I was just about to ask this question. Okaydenz TY. :P
There have no Script Performance panel in the “View” or anywhere else that I can find.
WERE IS IT ? !!
I think this would help if I knew how to script :(
Thx Now I Know About Laggy Gun And How To Fix Them Making An Ultimate Weapon!
1ST one to reply cool
Useful :)
Wait how do I script? is it a code for scripting? or do I say it in the game then something happens?
Nice post, but considering the community I doubt anyone would learn anything from this and those who have a brain know this already.
@Above, I agree.
Couldn’t say it any better.
The real issue, though, is that the people with these terribly laggy games don’t have the know-how to remedy the lag or just really don’t care.
Despite the latter, this post sheds some light on dark areas of the wait function and I understand it better as an advanced scripter.
Oh, good.
Great post, will help alot of new places with lag, good to know there is a proper way without removing bricks etc
FIRST COMMENT :D now im going to read the article :D
Thanks for the article! I hadn’t thought of using the elapsed time to adjust the code accordingly. I’ll be sure to keep that in mind in the future.
Nice lesson. It should make a big difference.
Thank God!