Making FPS displayer

Just feeling like checking how your Roblox performs?

by Mearshev

Author Avatar

#FPS Displayer


#Making of the UI


#Making of the script

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

while true do
	
	local fps = math.floor(1 / RunService.RenderStepped:Wait())
	
	textLabel.Text = fps .." FPS"
	
	wait(1)
end

#Part 1: Variables

-- line 1
local textLabel = script.Parent

This first line defines the textLabel, which is the script's parent

-- line 2
local RunService = game:GetService("RunService")

The 2nd line defines and gets the service of RunService.

#Part 2, Section 1: The Loop

while true do

end

This is a loop which you probably might know and it loops whatever script is inside it.

#Part 2, Section 2: The Script

local fps = math.floor(1 / RunService.RenderStepped:Wait())
    
textLabel.Text = fps .." FPS"
    
wait(1)

The fps variable gets the FPS of the player, let's break this down to small chunks.

#math.floor() function

The math.floor() function rounds off any positive numbers

example:

local veryLongDecimal = 3.141592653589

print(math.floor(veryLongDecimal))

-- Should output: 3

#RunService.RenderStepped:Wait()

What is RunService?

RunService is a service provided by Roblox that allows developers to interact with the runtime environment of the game. It provides functionality related to controlling the execution of scripts, timing, and synchronization with the game's frame rate.

What is RenderStepped?

RenderStepped is an event provided by the RunService that occurs every time a new frame is rendered in the game. This event fires immediately after each frame is drawn on the player's screen.

Then the :Wait() function, this waits for the RenderStepped event to occur, and when the event occurs, it returns the time (in seconds) since the previous frame was rendered.

When this runs, it should output approximately 0.016 seconds, which happens 1/60th of a second, this is the answer of 1 ÷ 60:

image|100x100

So it takes approximately 0.016 seconds to render each frame, after it gets the output, it divides the output by 1, so math.floor(1 / 0.016....) ≈ 60

#The Result

After all that, the script should work as intended:

image|100x100

((I'm sorry if the explanation is bad, I'm new to this thing, I hope this helps!))

View in-game to comment, award, and more!