How to add a wait of specific amount of seconds onto RenderStepped/Heartbeat?

I have a digital board consisted of many parts (well, not many, but it’s crucial for all of them to update simultaneously). Looping alone without RenderStepped/Heartbeat, although it’s a great idea, can display delays on “potato” PCs.

I know using RenderStepped/Heartbeat are the best options (preferrably renderstepped since it renders prior to each frame), but let’s say I want to hit it after a specific amount of time. How would I do that?

(for an example; have all boards update every 5 seconds before that frame renders)

You can utilize os.clock() for this like so:

local heartbeat = game:GetService("RunService").Heartbeat;

local function heartbeatWait(waitTime)
    local now = os.clock();
    while true do
        if os.clock() - now >= waitTime then break; end
        heartbeat:Wait()
    end
end
2 Likes

task.wait() does this for you.

I did see it, but it’s referred to as being different as it can still produce a visual delay in loops (like changing multiple images at once).

It shouldn’t unless you’re waiting between iterations or waiting before your code executes.

¯_(ツ)/¯ ¯_(ツ)/¯ ¯_(ツ)_/¯!

Is there anything like this with RenderStepped? This is a great solution, but, when thinking about it twice, RenderStepped renders it prior to the next frame.

Just change the Heartbeat event to RenderStepped event.
TL;DR: RenderStepped only works on the client.

1 Like

Wait, so I just call heartbeatWait(n) whenever I need to?

local renderStepped = game:GetService("RunService").RenderStepped;

local function renderSteppedWait(waitTime)
	local now = os.clock();
	while true do
		if (os.clock() - now) >= waitTime then break; end
		renderStepped:Wait();
	end
end

while true do
	print("this is in a 1 second delay loop using the RenderStepped wait event");
	renderSteppedWait(1);
end

image

2 Likes

Great! I also just tested it prior to you replying. I kinda got lost reading it at first cause it’s 5AM LOL

Thanks once more for helping me out! You saved me. :laughing:

1 Like