This should happen every second. If the server load is very high though wait becomes slower. This means that it will take more than 1 second to print “Target set”. In turn, this messes up other animations. How to fix this problem where wait becomes unreliable?
There is no way to get reliable time yielding without heartbeat or renderstepped.
local lasttime = 0
local time = 0
game:GetService("RunService").Heartbeat:Connect(function(dt)
time = time+dt
if time-lasttime>=1 then
lasttime = lasttime+1
do
print("Target set")
end
end
end
Instead of creating a security system to prevent exploits, I would better use the heartbeat solution. Both are cumbersome but the latter is a bit easier to implement and predict.
AFAIK this is pretty much what the wait function and thread scheduler does internally already, so that function will most likely have the same issues under a lot of stress.
Are you actually seeing this issue? If so, there’s probably something unnecessarily slowing down the thread scheduler and you should look into solving that first.
Using wait is generally fine where it makes sense and the time difference is usually barely noticeable.
The function wait is not guaranteed to wait the exact amount of seconds you specify. Here’s an old thread that briefly talks about wait and its return values (as it’s a callback function):
tl;dr wait(n) does not wait exactly n seconds.
This whole thread sounds more like you have code optimisation problems in which you’re running generally inefficient code. I don’t think there’s anything wrong with wait. You’re asking about the wrong topic here - look into your code efficiency. This problem roots from a bigger one.
The difference between 2 waits will not be consistent just like a standard wait(1) loop; however, it will avoid wait time build up.
After running 1000 cycles of a wait(1) loop, you will accumulate a total of around 1040 seconds, maybe even more, while my solution will accumulate around 1000.1 seconds. There will always be an offset, but my solution keeps it always within 1 second.
This is true. I have used this approach before. Does not matter how much the server lags. If your animations depend on steps generated in this way then they look chopier but in the end they are accurate.
Never really noticed that being the case. I had a benchmarking place (that I accidentally deleted after formating ) with this and didn’t really see any issues even with throttling.