Achieving a built-in framerate cap for my 2D gui client-based game

I think this should make it run every 1 / 60 of a frame.

local RunService = game:GetService("RunService")

local elapsed = 0

RunService.RenderStepped:Connect(function(dt)
	elapsed += dt
	
	while elapsed >= 1 / 60 do
		elapsed -= 1 / 60
		
		-- Simulate code
	end
end)
1 Like

Ah, that fixes everything. This method appears to work great. Thank you!

This will not work if your framerate is slower than your desired fixed update rate. For example, your UpdateSimulation() function will never be called faster than 40hz if your framerate is at 40hz.

The way @bytesleuth and I suggested is the correct way of doing this.

1 Like

No I need it to work that way. DeltaTime is useless for my game as it is a cellular automaton game and works under ticks per second (or Hz). I dont care if the game can slow down, I just need it to stay within 60 TPS/Hz.

Another thing to, the slowdown from low end devices is a good thing, because it wont put stress on the device from trying to update the game multiple times a frame to keep up consistency. This will also allow the game run higher in framerate too.

The game also has a feature where you can set the TPS (e.g. 60, 40, 25, etc)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.