How does this "FPS Cap" code work?

hey guys, as the title already explained, how does this snippet of code work?

local FPS = 20
local frametime = 0
runService.PreSimulation:Connect(function(deltaTime)
	
	frametime += deltaTime

	-- FPS CAP --
	if frametime >= 1/FPS then
		frametime %= 1/FPS
	else
		return
	end

	print("do something")
end

That’s the thing, it doesn’t work

If you wanna cap fps then do this:

local cap = 20
local clock = os.clock()
runService.PreRender:Connect(function(deltaTime: number)
    while os.clock() - clock < 1/cap do end
    clock = os.clock()
end)

That doesn’t exactly limit/reduce the FPS, it just waits until enough time has passed to run the code (which is what you should do instead of intentionally limiting FPS).

It will early return if frametime (the sum of time passed) is below 1/20th of a second. If it’s equal to or over that amount, it resets frametime to the remainder of time over the limit (i.e., in normal cases, it would be equivalent to frametime -= 1/FPS) to account for error, and then runs any following code.

oh nice, but I don’t fully grasp the concept, could u explain this like I’m 5 years old? why do I even need this?

edit: is this to run the code 20 times per second?

You keep adding how much time passes until enough time has passed. If enough time hasn’t passed, you don’t do anything. If enough time has passed, you reset the timer and run code.

Yes, the point of this is to run code at a certain rate. This is basically a timer, so it’s useful for anywhere an infinitely looping timer is needed. Typically you could see this for complex operations that need to be done regularly but are too computationally expensive to run every frame.

  1. what will happen if I increase the FPS value (60 for example), will it run 60 times in a second?
  2. if FPS value was kept at 20 and the client is running at 10 FPS, will it still run 20 times for them, or still 10 times?
  3. do I need to implement this into all of my infinitely looping scripts?
  4. is this a better script?
local FPS = 20
local timePassed = 0
local interval = 1 / FPS

runService.PreSimulation:Connect(function(dt)
	timePassed += dt

	if timePassed < interval then
		return
	end

	timePassed -= interval
	print("do something")
end)

  1. Yes
  2. In this implementation, it would run 10 times.[1]
  3. No. It’s something you’ll have to be judicious about whether or not it’s desired. If you have, for example, some code that updates the position of a laser based on the player’s mouse, it might look really bad if it isn’t synced with framerate. Other stuff, like NPCs or gameplay systems, are more applicable.
  4. For the most part I think so, as it’s less confusing, but at low framerates it’s possible that timePassed compounds more and more above interval, which may or may not cause bugs.

  1. Solving this requires looping the “execution part” for as long as timePassed is still greater than interval. Inverting the if statement’s condition makes for a perfect while loop condition, where you then repeatedly subtract interval from timePassed until it’s under the threshold. ↩︎