Is it even possible to wait the specified amount of time?

My script has been using the built in wait() function for a while. This works, but it gets less and less precise the longer the server is running, so I decided to switch to a RunService.Heartbeat:Wait() loop, which has the same problem for whatever reason. I looked around and I’ve been seeing that all wait functions have this problem.
Is it even possible to achieve this? if not, does anybody know why this problem happens?

If you’re talking about exact time(down to the millisecond) I don’t think it’s possible. Time is really hard to get exact, most computers are a bit off.

Wait() is always iffy, but can you explain what you mean by its getting less and less precise?

As servers get old, the extra delay time to the wait function gets longer.
This can be fixed by shutting down old servers, but I would rather not resort to that.

Are you trying to accomplish anything specific? Is there a certain reason that you need the wait to be exact, there may be other solutions

I have a stun script, so I can’t have the waits getting longer for no reason.
It sometimes gets really bad, up to 2 seconds or more.

You could always use an if statement and tick() or os.time() in a function. If you’re using it for a stun gun you could add an attribute to the gun which saves the time last used and then compare that to the current tick().

I use this function

while time() - Start < Length do
	RunService.Heartbeat:Wait()
end

It has the same problem as the normal wait, only it’s quite a bit less.

Ok, but I’m saying you could do a check when the player tries to use the stun gun.

-- Player uses tool.
if tick() - tool:GetAttribute("timeval") > timelimit then
-- rest of script
-- if the player successfully uses the tool
tool:SetAttribute("timeval", tick())

wait(), global function, doesn’t mean that it will yield the thread for an exact amount of second sales specified in the brackets. Due to this, there’s legendary developers out there made a Timer module, where you can exactly yield a thread more accurately.