local function wait(n)
local elapsed = 0
local n = n or 0
repeat
elapsed = elapsed + RunService.Heartbeat:Wait() -- or renderstepped if you are running on client
until elapsed >= n
return elapsed
end
the default wait function is usually really inaccurate when you have lots of lag, otherwise, its fine
your function seems fine too, so ok
Your function custom wait function is nearly as accurate as the default wait() time, so I don’t really think the slim increase in accuracy is worth the expense.
You can do something like this which is very close to perfectly accurate, but be warned that it will definitely self destruct if try to use a large wait time. And again, I don’t really think the accuracy is worth the extra expense.
local function accuWait(seconds)
local start = tick()
while tick() - start < seconds do end
end
EDIT:
I decided to add to this because I think I came up with a much better idea for a wait function. It uses the default wait function for the wait time, except for the last 0.5 seconds which will use the while do end loop, drastically decreasing the expense and will fix the issue of breaking with large wait values. Plus, based off of a single test, this function seems a bit more accurate.
local function accuWait(seconds)
local start = tick()
if seconds >= 1 then
wait(seconds - (seconds-.5)) -- leaves 0.5 seconds left
end
while tick() - start < seconds do end
print("Time:" , tick() - start)
end
I understand this, but I figured it was the easiest way to implement the accuracy of the while do end loop without creating a “bomb”.
Your current Stepped loop is far more accurate, but I think it might do you good to do something like what I did with the stock wait, and use the Stepped/Heartbeat loop for the majority of the wait to prevent the bomb, and then use the while do end for the last half second or so to maintain that accuracy.