How can I improve this script?

Hey guys! This is the script I use to count fps but I want to know how I could improve this script!

local milliseconds = 16
local fps = 0
local number = 0
repeat
fps += 1
number += milliseconds
until number >= 1000
print(fps) -> 60

Since it has no waits at all it causes some lag, If I were to put a task.wait() inside it’s delayed and I want to fire this code every frame.

2 Likes

To get something every frame you can use RunService.

Here is a quick description of it and its uses.

By connecting to one of the RunService events (ex. heartbeat) you can also receive the amount of seconds it took for that heartbeat. This will give you an approximation every heartbeat for what the current fps is.

Your current code will more or less halt the thread for X time. If your code takes exactly 1 second to execute that is pure luck, and will be different depending on the hardware that is running it.

Your current script does not actually show you the FPS, instead it shows you what integer * 16 is greater than or equal to or larger than 1000, which will be the same every time.

Instead you could do this:

local fps = 0
local number = 0
repeat
    fps += 1
    number += task.wait() -- This returns the time it took in seconds (decimals)
until number >= 1
print(fps) -- Prints the amount of loops able to be completed within 1s

However, I would still not recommend this as it is not linked to the frame rendering, which RunService is.

2 Likes

Workspace | Documentation - Roblox Creator Hub This may help.

2 Likes

you could also just do:

local runService = game:GetService("RunService")
local CurrentFPS = 1/runService.RenderStepped:Wait()

RenderStepped:Wait() returns a number equal to how long it’s been between the current frame and the last frame. Divide 1 by this number and you should get the FPS

For the server’s FPS, you can do this:

local FPS = 1/task.wait()

Same concept as using RenderStepped:Wait(), since task.wait() returns a number equal to how long it’s been between the current frame and the last frame

1 Like

But doesn’t that code returns milliseconds?

Nevermind it returns fps thank you!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.