Inaccurate FPS counter using delta time

local RunService = game:GetService("RunService")
 
local FPS = nil
 
RunService.Heartbeat:Connect(function(deltaTime)
	FPS = math.floor(1/deltaTime)
end)

while wait(1) do
	script.Parent.Text = FPS
end

Problem:

When FPS capped at 120 in Studio, the FPS counter shows 125 or any number that isn’t my real frame rate. However, when using different methods, the FPS indicator is accurate?
Is this due to the in accuracy of ROBLOX?

image

image

Also tried using RenderStepped to no avail, but when using Stepped, the FPS counter is accurate just 1 fps below.

1 Like

Roblox does calculate the FPS for you already which can be retrieved with workspace:GetRealPhysicsFPS().

Counting the amount of updates every loop is a more reliable way to get the current rendering FPS, since the difference in the deltaTime values of the Heartbeat event can be quite big.

Example:

local RunService = game:GetService("RunService")
 
local updates = 0
 
RunService.Heartbeat:Connect(function()
	updates += 1
end)

while (true) do
	local deltaTime = wait(1)
	local FPS = math.floor((updates/deltaTime) + 0.5)
	updates = 0
	
	script.Parent.Text = FPS
end
3 Likes

Completely wrong, that calculates the physics FPS.

Have you tried averaging it over a few cycles, and possibly using os.clock or tick to calculate the time difference since the last cycle manually?

Using tick() to calculate the time elapsed since the last frame?

local RunService = game:GetService("RunService")
 
local FPS = nil
 
RunService.Heartbeat:Connect(function(tick)
	FPS = math.floor(1/tick)
end)

while wait(1) do
	print(FPS)
end

Now this is a rude thing to say to someone who’s trying to help and also providing the sufficient solution. The value you get from :GetRealPhysicsFPS() equals to how many times per second the viewport including physics currently get updated, and since Roblox caps this value to ~60, then that is also what you’ll get or less if you don’t unlock this cap.

Monitor refresh rate, which I think you’re looking for, is something else. It doesn’t matter how fast your monitor is, if the game cannot keep up with it - which in this case is capped to 60 fps.

6 Likes

Went in studio and messed around a bit but couldn’t get an accurate counter of my FPS. So I browsed the forum to see if anyone came up with something similar and sure enough they did. Le Script. I just wrote it like this because I wanted to lol.

local FPSLabel = script.Parent
local RunService = game:GetService("RunService")
local RenderStepped = RunService.RenderStepped

local sec = nil
local FPS = {}
local function fre()
	local fr = tick()
	for index = #FPS,1,-1 do
		FPS[index + 1] = (FPS[index] >= fr - 1) and FPS[index] or nil
	end
	FPS[1] = fr
	local label = script.Parent
	local fps = (tick() - sec >= 1 and #FPS) or (#FPS / (tick() - sec))
	fps = math.floor(fps)
	label.Text = ""..fps.." FPS"
end

sec = tick()

RenderStepped:Connect(fre)

The post

Get client FPS trough a script

2 Likes