I know that
wait() is 1/3 of a second (0.3)
task.wait() is 1/60 of a second (0.015)
I know this might sound insane but anything faster then task.wait?
I know that
wait() is 1/3 of a second (0.3)
task.wait() is 1/60 of a second (0.015)
I know this might sound insane but anything faster then task.wait?
You could do something that runs every frame, which would be 1/60 of a second.
What are you trying to achieve?
I am writing on the server and I am trying to make some world generation system which needs to load fast and not go too fast then the loops break.
then you can do breaks for loop:
if math.random(1,3) == 3 then
task.wait()
end
so it can randomly stop for a minimal server wait time.
it can be kinda buggy and sometimes luck goes so it will not wait but still, working method.
So I made this test (very messy I know, it was just to test a theory I had, it can definitely be optimized):
If you look at this code:
local currentTime = os.clock();
local loopAmountPerSecond = 10000;
local startTime, lastIndex = os.clock(), 1;
for i=1,1000000,1 do
if(os.clock() - startTime == 1) then
lastIndex = i;
startTime = os.clock();
elseif(i - lastIndex > loopAmountPerSecond) then
task.wait();
lastIndex = i;
startTime = os.clock();
end;
end;
print("Done!", os.clock() - currentTime);
then this one (for only task.wait):
local currentTime = os.clock();
for i=1,1000,1 do
task.wait();
end;
print("Done #2!", os.clock() - currentTime);
and this one with normal wait:
local currentTime = os.clock();
for i=1,100,1 do
wait();
end;
print("Done #3!", os.clock() - currentTime);
You can see that surprisingly, even after removing a lot of 0s from the other loop, the method in the first one finished first:
You might ask why not just do this:
local currentTime = os.clock();
local loopAmountPerSecond = 10000; --Not per second but per tick
local lastIndex = 1;
for i=1,1000000,1 do
if(i - lastIndex > loopAmountPerSecond) then
task.wait();
lastIndex = i;
end;
end;
print("Done #4!", os.clock() - currentTime);
It is because this isn’t waiting when it needs to, it’s just waiting each amount of indexes, so if I have nothing going on, and then doing a huge operation that takes time, it will unnecessarily add a task.wait() to the big operation
Also, just so people don’t get confused, yes task.wait is faster than normal wait, it’s just that I added a 0 to the task.wait loop, making it 1000:
my solution was a math.random
or an i%x==0
.