Add official method to get FPS

As a Roblox developer, it is currently too hard to get a player’s FPS.
We have to implement our own way, which is not 100% accurate.

I think it would only make sense to add a Player:GetFPS() method as we already have a Player:GetNetworkPing() method.

Just a quick little information, you can just do local dt = task.wait(); local fps = 1/dt to get the current FPS at that time, or even connect function(dt) local fps = 1/dt end to RunService.PreRender to update it each frame.

It’s an okay feature for beginners in local scripts, but it’s definitely a time-saver if you’re talking about it being server-side since we don’t have to setup events for querying a player’s current FPS.

From what I’ve heard, that method is not 100% reliable.

..can you explain why it’s said not to be reliable?

This does not count the number of frames in a second, you are only extrapolating from the time between frames. If 10 frames occur over 1 second, but 1 of the frames only takes 16ms to render, then this calculation will report the FPS as 60 for that frame. The only way to accurately know what the framerate is, is to actually measure and count how many frames occurred within a second with a counter.

This is a pretty trivial thing to implement on your own, not in any way hard.

local RS = game:GetService("RunService")
local frames = {}

RS.Heartbeat:Connect(function() 
    local now = tick()
    local toremove

    for i, v in frames do
        if (now - v) > 1 then toremove = i
        else break end
    end

    if toremove then table.move(frames, toremove + 1, #frames + toremove, 1) end
    table.insert(frames, now)
end)


return function() 
    return #frames
end

This should be accurate enough.

There’s also this approach, which I personally use for my FPS measurements. It’s a little simpler, however the FPS value only updates once per second.

local FramesPerSecond = 0 -- The actual value to display

local FrameCount = 0
local LastSecond = 0

RunService.PreRender:Connect(function()
    local currentTime = workspace.DistributedGameTime
    
    if currentTime - LastSecond >= 1 then
        FramesPerSecond = FrameCount
        LastSecond = currentTime
        FrameCount = 0
    end
    
    FrameCount += 1
end)

This works too, but you’ll be limited to updating fps once a second.

I think a “frames per second counter” is a slightly different thing than “frames per second”. An FPS counter counts the number of frames in a second, while a “frames per second” is the ratio of frames per second. It can be written as 1/delta_between_2_frames. PreRender gives us the delta between 2 frames, so it’s pretty accurate (apart from floating point precision errors).

Here, you can see the FPS grapher using PreRender capturing the spikes pretty well. It’s just the display format making it harder to catch.

The graph expands to the largest value in its history window for each line.

Guys, you must not use task.wait() or .Heartbeat or .PostSimulation or anything like that to get actual FPS. These times are in steps of 1/240 seconds and subject to physics throttling, therefore inaccurate.

You must measure delta time between each frame on .PreRender, using either os.clock() or the dt value from .PreRender. This is an accurate metric for real time FPS.

You may also apply a low pass filter to this FPS value to make it more readable if desired.

What specific use cases would you have for this? Displaying the client’s FPS in game is an obvious example, but are there any other compelling uses?


All of these events are guaranteed to fire once per frame regardless of physics throttling. Where are you getting this information?

Yes, the signals fire once per frame, the delta time you get has a resolution of 1/240 seconds. I should have worded it better sorry.

I use the Stats service personally…
In my opinion I think any similar method that gets the frame time (task.wait, RenderStepped delta, etc.) will work just fine - if not the same. Really useful service, check it out!

local Stats = game:GetService("Stats")
local FPS = 1 / Stats.FrameTime

If you need to accurately measure the current FPS for calculations related to rendering, then just use deltaTime directly. This works for calculations related to the current frame but is unstable for FPS counter display due to each frame taking different lengths of time, so needs further work to make it accurate as an FPS counter. BindToRenderStep and others receive the elapsed time between render steps, including whatever scheduling and waiting occurred between those steps.

If you’re doing maths to calculate the distance an object has moved, you should just use deltaTime directly since this is simple and is based on the actual frame time for that particular frame. So for calculating movement of an object in that particular frame you would use something like e.g.:

    local positionDelta: Vector3 = _renderBall.Position - _ball.Position
    local expectedTravel: number = _ball.AssemblyLinearVelocity.Magnitude * deltaTime
    local gap: number = positionDelta.Magnitude
    local engageThreshold: number = math.max(
        Config.Ball.RenderSmoothing.EngageMin,
        expectedTravel
    )
    if gap >= engageThreshold then
        smoothing = true
        offset = positionDelta
    end

However, if you want a simple counter that displays the averaged FPS (obviously don’t leave the print statement in production, instead you would maybe use the value in a UI display). You can use this, which smooths the FPS:

local frames = 0
local elapsed = 0
local averageFps = 0

RunService:BindToRenderStep("fpsCounter", 9999,
	function(deltaTime)
		frames += 1
		elapsed += deltaTime

		if elapsed >= 0.1 then
			local measuredFps = frames / elapsed
			averageFps = if averageFps == 0
				then measuredFps
				else (measuredFps + averageFps) / 2
			frames = 0
			elapsed = 0
		end
		print(averageFps)
	end
)

Result:

 06:42:30.512   ▶ 120.49464712599567 (x12)  -  Client
  06:42:30.613   ▶ 119.71897719080326 (x12)  -  Client
  06:42:30.713   ▶ 119.67143944840383 (x13)  -  Client
  06:42:30.821   ▶ 120.1102000720737 (x12)  -  Client
  06:42:30.922   ▶ 119.44157837169843 (x13)  -  Client
  06:42:31.031   ▶ 119.53540103053136 (x12)  -  Client
  06:42:31.131   ▶ 119.49355959925816 (x13)  -  Client
  06:42:31.240   ▶ 119.64108941993868 (x13)  -  Client
  06:42:31.347   ▶ 120.61769788628507 (x12)  -  Client
  06:42:31.448   ▶ 119.44096672657193 (x13)  -  Client
  06:42:31.554   ▶ 120.99799709194608 (x13)  -  Client
  06:42:31.663   ▶ 120.14056291624328 (x12)  -  Client
  06:42:31.765   ▶ 118.69208286779357 (x13)  -  Client
  06:42:31.871   ▶ 120.97406459980813 (x12)  -  Client
  06:42:31.975   ▶ 118.0092071912813 (x13)  -  Client
  06:42:32.079   ▶ 121.59174408546329 (x12)  -  Client
  06:42:32.180   ▶ 120.25615155828925 (x12)  -  Client
  06:42:32.280   ▶ 119.95839619990528 (x12)  -  Client
  06:42:32.381   ▶ 119.77851196802871 (x13)  -  Client
  06:42:32.488   ▶ 120.4745848003449 (x12)  -  Client
  06:42:32.588   ▶ 120.10456691038716 (x13)  -  Client
  06:42:32.697   ▶ 119.96354167656477 (x12)  -  Client
  06:42:32.797   ▶ 119.50180148128446 (x13)  -  Client
  06:42:32.905   ▶ 120.16369617617653 (x12)  -  Client
  06:42:33.005   ▶ 119.93389376367118 (x12)  -  Client