I think this is what is causing my game to lag and how would I still be able to use them but not lag and is it the stuff inside the loop or the actual loop that lags?
Check the script performance pane in studio. It’s all about the rate at which said loops run.
how would i find out which script is where?
The stuff inside
so if i had 90 while true do loops with nothing in them there would be no lag?
How do you read the data provided so you know what is good and what is bad?
Not strictly true. If you had:
while true do end
this will definitely crash your computer regardless of the stuff inside of it.
As long as the loop has at least a wait() inside, it will be the contents of the loop that affect the “lag”
while true do wait() end
won’t cause any lag provided you don’t have a billion of them (but a billion of probably anything would cause lag anyway).
while true do
wait()
very_expensive_function() -- does a load of square roots and complicated math
end
This could cause some significant lag, in this case the culprit being the function. If this is the case, try running it less often (using a larger wait() like wait(2) for 2 seconds etc if it doesn’t need to be run literally every 1/60th second)
edit: (thanks)
Tiny detail: Scripts run at 30 Hertz on roblox, and that’s the timing you’ll get with while wait() do.
The default value for wait()
is not 0.3
. The default is 0.03
. 30 Hertz is the same as 1/30
, which is 0.03333
(repeating).
(not exact because of floating point imprecision)
Changing your LuaSettings → DefaultWaitTime value is not suggested as the default for the server and client is 1/30. Changing your settings in Studio won’t change the server or client settings, so it will only make testing your game inaccurate.
In practice, waiting for 0.03
seconds will generally have you wait at 30 Hertz because that’s how often the thread scheduler will resume your script. In effect, wait()
runs at 30 Hertz.
I usually like just using the RunService.Stepped event or something like that instead. And use other sorts of events rather than loops whenever possible. You usually shouldn’t need to use a lot of loops in a game. Especially not loops that run forever.
Having too many loops will lag your game, especially if you don’t break
them after they are no longer necessary or if you call expensive functions in small intervals, such as the free model zombie follow script which iterates through all immediate children of the workspace to find the closest NPC or player target every 0.1 seconds.
When using while loops, try to set the condition as well.
local A = false
while A == false do -- Runs until A is true, then breaks
print("yes")
end
local A = false
while true do
if A then break end -- Manual stop
print("yes")
end
Of course, whenever you want to have something happen based on a certain condition, it’s best to try to find an Event that is relative to that condition.
For example, I want to play an animation whenever the character jumps.
I would probably look up on the wiki the Humanoid object and look through it’s events and find the event “Jumping”.
local Anim = Instance.new("Animation")
Anim.AnimationId = 123123123
local Humanoid = game.Players.LocalPlayer.CharacterAdded:Wait():WaitForChild('Humanoid')
wait() -- because of game descendants error
local Track = Humanoid:LoadAnimation(Anim)
Humanoid.Jumping:Connect(function(b)
if b == true then
Track:Play()
elseif b == false then
Track:Stop()
end
end)
If your game has 90 while true do loops, it will almost certainly lag because its been written in a very sloppy manner, and probably needs a rewrite!
If you actually have a load of while true loops, I recommend posting on the forum, and asking us about how you could write them better. Good luck!
Excessive while true do loops can cause noticeable lag; I’d recommend using alternatives wherever possible. A common albeit poor practice I used when I was starting to script was using while true do loops where the Changed event is preferable. Depending on what you use a while true loop for, you may be able to rewrite some of your code to be more efficient.
For example,
while wait() do
if (property meets condition) then
-- code
end
end
likely should be substituted with
x.Changed:Connect(function()
if (property meets condition) then
-- code
end
end)
I’m not sure what exactly you’re using your while true loops look like or what their intended purpose is, but I hope this is useful. Cheers!
Wouldn’t it be 1/30?
yeah I already edited the post to reflect that a few days ago at the very bottom with the provided quote from @daireb
Don’t forget to break loops that are no longer needed.
while something do
if (condition) then break end
-- stuff here; add a wait or you're done!!!
end
while not (condition) do -- Auto break when condition met
-- thing; make sure you add a wait or you're done!!!
end
If a script running the loop is destroyed, will that break the loop?
You could carry out a simple test to check, but yes, it will break the loop.
does this apply to module scripts if it is in the server storage? See i need to redirect a projectile with the mouses movement, and i have found that with the server script in the server script service it creates that performance lag, so instead i tried copying a folder that contains all the script stuff that i need including the server script and i put it in the starter pack which functions in the server as well. However i would like to use it in the server script service, and i am wondering if a module will help fix the loop lag.