Is it possible to change how much time a wait() has left to wait *while* it is waiting?

Since the code is a little complex and I don’t feel a need to type it out, I thought I’d just put it simply. If I have a function that is constantly changing a number variable that is used in a wait() (e.x. wait(waittime)), is it possible to force the wait to use the new value instead of the one it received at the time the wait was called (or something else that could help with this problem)?

To put it more clearly, the initial time the wait is called the variable is set to 5–so it waits 5 seconds until the wait is active again. But in that 5 second span the wait variable is already down to around .3 seconds, so there’s this big gap between it waiting 5 seconds to .3 seconds. The context to this is attempting to calculate the wait time between footsteps based on the player’s WalkSpeed, where the WalkSpeed changes from 1-16 based on how long the player has been walking for.

Any help would be much appreciated!

Instead of using one wait(waittime) you could do

repeat
timer = timer + 0.1
wait(0.1)
until timer >= preferredtime

changing preferredtime would make it the loop end if it’s already waited for longer than the preferredtime

after a bit of tinkering this worked! thank you very much : )

1 Like
local run = game:GetService("RunService")

local timeElapsed = 0

run.RenderStepped:Connect(function(step)
	timeElapsed += step
	if timeElapsed >= 1.23456789 then
		return
	end
end)

print("Wait ended!")

Here’s a more accurate approach using an event which fires every frame (for the client).