You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I wish the for loop below me can be more efficient.
What is the issue? Include screenshots / videos if possible!
The loop below me ran too slow for my game.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I searched keywords such as “more efficient loops”, “alternative for loops” and such. However, I can’t find any solutions.
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local Loop1 = function()
for i = 0, 1000
do
for i = 0, 1000
do
--[[
Code
--]]
end
wait();
end
end
Loop1();
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.
You have a wait() statement in it. A wait thing will wait 1/30 seconds. You run it 1000 times, leading to a wait of over 30 seconds! Remove the wait statement and try again!
old
local Loop1 = function()
for i = 0, 1000
do
for i = 0, 1000
do
coroutine.resume(coroutine.create(function()
--[[
Code
--]]
end))
end
wait();
end
end
Loop1();
coroutine basically makes it run on a seperate thread, speeding up the script
local Loop1 = function()
for i = 0, 1000
do
for i = 0, 200 do
--[[
Code
--]]
end
for i = 0, 200 do
--[[
Code
--]]
end
for i = 0, 200 do
--[[
Code
--]]
end
for i = 0, 200 do
--[[
Code
--]]
end
for i = 0, 200 do
--[[
Code
--]]
end
task.wait();
end
end
Loop1();
Could you provide more information on what exactly you’re trying to accomplish? Without much context, it’s hard to deduce the exact approach to your problem. However, I can still provide the following suggestions:
You have a loop inside of another loop (nested loop), but they both use the same variable i. This means the code would do the same thing exactly 1,000,000 times. I can’t really think of a reason why you would want to run something 1000 times per frame in 1000 frames, which brings me to point 2;
Rethink your approach. Are you trying to modify instance properties? Perhaps you could use Tweens instead?
Does your Code portion also use wait? As @SeargentAUS suggested, you can run the code in separate threads;
The solution that @SeargentAUS provided is a band-aid solution (and a really bad one, especially the last one) that should be improved upon using my suggestions and especially point 5;
Use the task library instead of wait and coroutine:
Instead of wait(), use task.wait();
Instead of coroutine.resume(coroutine.create(function(), use task.spawn(function() to immediately run the function, or task.defer(function() to run the function on the next frame.
Optional (and somewhat biased) code quality suggestions that do not affect performance:
Instead of writing local Loop1 = function() you can write local function Loop1();
Semicolons are optional;
You can write for i = 0, 1000 do on the same line to make it better to read.
Just A Small Fix, You Should Do This If You Want It To Be Faster And Not Crash At The Same Time
local Loop1 = function()
for i = 0, 1000
do
for i = 1,2 do
for i = 0, 100 do
--[[
Code
--]]
end
for i = 0, 100 do
--[[
Code
--]]
end
for i = 0, 100 do
--[[
Code
--]]
end
for i = 0, 100 do
--[[
Code
--]]
end
for i = 0, 100 do
--[[
Code
--]]
end
local t = tick()-tick()+tick()-tick()+tick()-tick() --To Introduce Some Delay Between Frame Iterations (Like A Wait But Faster)
end
game:GetService("RunService").RenderStepped:Wait()
end
end
Loop1();