How to make an accurate FPS counter!

First, insert a ScreenGUI:
image

Second, insert a TextLabel, and customize it:
image

Third, insert a LocalScript into the TextLabel:
image

Fourth, copy and paste this code into it:

local RunService = game:GetService("RunService")

local start = tick()
local updaterate = 0.5

local average_amount = 5 --Higher the average, the more accurate the fps will be!

local fps_table = {}

RunService.RenderStepped:Connect(function(frametime)
	if tick() >= start+((updaterate)/average_amount) then
		local fps = 1/frametime
		table.insert(fps_table,fps)
	end
	if tick() >= start+updaterate then
		start = tick()
		local current = 0
		local maxn = table.maxn(fps_table)
		for i=1,maxn do
			current = current+fps_table[i]
		end
		local fps = math.floor(current/maxn)
		script.Parent.Text = fps
		fps_table = {}
	end
end)

Finally, test your game and see the results:

Note: I had lag spikes from minimizing and maximizing the window, this is normal

Play around with these variables:

local updaterate = 0.5

local average_amount = 5 --Higher the average, the more accurate the fps will be!

This script basically takes a certain update rate, and divides it by the amount of fps to be checked. It adds all indexes inside the table it made, and divides it using table.maxn() . It then uses math.floor() to get a single integer and sets the text to be the fps.

5 Likes

You have to explain the code to make it a #resources:community-tutorials

2 Likes

Forgot to do that, just fixed it! :+1: