I am trying to loop through several thousands of objects in the shortest amount of time possible.
I wonder, what causes a while true do end loop to crash studio?
And is there a consistent way to figure out how many loops can run before crashing occurs(cross-computer)?
If I can figure out how many loops can run before crashing occurs then I can do something along the lines of:
local maxIndex = 1000 —1000 as an example
local index = 0
while true do
index += 1
if index%maxIndex == 0 then
index = 0
task.wait()
end
—code here
end
Well while true do causes crashes do to the sheer amount of iterations it’s doing. I’m talking billions of iterations and it has to do math for each one, causing memory usage and lag.
Take this example script:
local startTime = os.clock()
local iterator = 0
while iterator < 10^9 do -- 1 billion
iterator += 1
end
print(os.clock()-startTime) -- time taken to run
print(iterator)
It took my computer 4.3235905 to run 1 billion times in the command bar in studio. Try it out your self and see your time. If it was true then it would run as much as it wants without needing to count up and would run even faster.
In studio, if something loops without yielding for 10 seconds, it automatically terminates it with the error “Script timeout: exhausted allowed execution time”. You can actually change it in settings here:
Typically, studio crashes aren’t because of the loop (It can escape from it after the script timeout), it’s typically from the loop creating instances. For example, if you did:
while true do
Instance.new("Part",workspace)
end
This would still crash studio, since it has to simulate and render for an insane number of parts in such little time, so it typically locks up and doesn’t have the time or power to reach the timeout.