--// Fps Counter
local RunService = game:GetService("RunService")
local FPS = 0
local function updateFPS()
FPS += 1
end
RunService.RenderStepped:Connect(updateFPS)
while wait(1) do
print(FPS)
FPS = 0
end
--[[ Logic:
Reseting the variable 'FPS' back to 0 once it prints it because we
don't want it to gain a number every render stepped event. A Player
won't notice the 'FPS' suddenly going down since render stepped fires every frame
and a frame fires every second and that's why there is a wait of 1 second on the while loop.
]]
Alright, so here’s what I want reviewed.
The Logic of the script that is commented and the overall script.
I believe with this in mind, the script could just become this:
local RunService = game:GetService("RunService")
while true do
wait(1)
local dt = RunService.RenderStepped:Wait()
local fps = math.floor(1 / dt)
print(fps)
end
This way it minimizes the amount of implicit state.
I find heartbeat the best solution, since it fires every frame after physics simulation.
Looking at your script, well delta time can be sometimes different resulting in in-accurate FPS. So the best solution I find is just divide by 1.
Here’s so if you would like to review my script.
local RunService = game:GetService("RunService")
local frames = 0
RunService.Heartbeat:Connect(function()
frames += 1
end)
while wait(1) do
script.Parent.Text = math.floor(frames / 1)
end
However, delta time would be a little more accurate since it’s the time elapsed since the last frame and you can just divide the delta time by 1.
With this in mind, now knowing you want the average FPS, you can display it like so:
local RunService = game:GetService("RunService")
-- How many seconds should pass before the FPS counter is updated. Set to 0
-- to have the counter update on every frame.
local MINIMUM_DISPLAY_DELAY_IN_SECONDS = 1
local frame = 0
local timeElapsed = 0
RunService.Heartbeat:Connect(function(step)
frame += 1
timeElapsed += step
if timeElapsed >= MINIMUM_DISPLAY_DELAY_IN_SECONDS then
local fps = frame / timeElapsed
script.Parent.Text = math.floor(fps)
frame = 0
timeElapsed = 0
end
end)