Just a quick little information, you can just do local dt = task.wait(); local fps = 1/dt to get the current FPS at that time, or even connect function(dt) local fps = 1/dt end to RunService.PreRender to update it each frame.
It’s an okay feature for beginners in local scripts, but it’s definitely a time-saver if you’re talking about it being server-side since we don’t have to setup events for querying a player’s current FPS.
This does not count the number of frames in a second, you are only extrapolating from the time between frames. If 10 frames occur over 1 second, but 1 of the frames only takes 16ms to render, then this calculation will report the FPS as 60 for that frame. The only way to accurately know what the framerate is, is to actually measure and count how many frames occurred within a second with a counter.
This is a pretty trivial thing to implement on your own, not in any way hard.
local RS = game:GetService("RunService")
local frames = {}
RS.Heartbeat:Connect(function()
local now = tick()
local toremove
for i, v in frames do
if (now - v) > 1 then toremove = i
else break end
end
if toremove then table.move(frames, toremove + 1, #frames + toremove, 1) end
table.insert(frames, now)
end)
return function()
return #frames
end
There’s also this approach, which I personally use for my FPS measurements. It’s a little simpler, however the FPS value only updates once per second.
local FramesPerSecond = 0 -- The actual value to display
local FrameCount = 0
local LastSecond = 0
RunService.PreRender:Connect(function()
local currentTime = workspace.DistributedGameTime
if currentTime - LastSecond >= 1 then
FramesPerSecond = FrameCount
LastSecond = currentTime
FrameCount = 0
end
FrameCount += 1
end)
I think a “frames per second counter” is a slightly different thing than “frames per second”. An FPS counter counts the number of frames in a second, while a “frames per second” is the ratio of frames per second. It can be written as 1/delta_between_2_frames. PreRender gives us the delta between 2 frames, so it’s pretty accurate (apart from floating point precision errors).
Here, you can see the FPS grapher using PreRender capturing the spikes pretty well. It’s just the display format making it harder to catch.
The graph expands to the largest value in its history window for each line.
Guys, you must not use task.wait() or .Heartbeat or .PostSimulation or anything like that to get actual FPS. These times are in steps of 1/240 seconds and subject to physics throttling, therefore inaccurate.
You must measure delta time between each frame on .PreRender, using either os.clock() or the dt value from .PreRender. This is an accurate metric for real time FPS.
You may also apply a low pass filter to this FPS value to make it more readable if desired.
I use the Stats service personally…
In my opinion I think any similar method that gets the frame time (task.wait, RenderStepped delta, etc.) will work just fine - if not the same. Really useful service, check it out!
local Stats = game:GetService("Stats")
local FPS = 1 / Stats.FrameTime
If you need to accurately measure the current FPS for calculations related to rendering, then just use deltaTime directly. This works for calculations related to the current frame but is unstable for FPS counter display due to each frame taking different lengths of time, so needs further work to make it accurate as an FPS counter. BindToRenderStep and others receive the elapsed time between render steps, including whatever scheduling and waiting occurred between those steps.
If you’re doing maths to calculate the distance an object has moved, you should just use deltaTime directly since this is simple and is based on the actual frame time for that particular frame. So for calculating movement of an object in that particular frame you would use something like e.g.:
local positionDelta: Vector3 = _renderBall.Position - _ball.Position
local expectedTravel: number = _ball.AssemblyLinearVelocity.Magnitude * deltaTime
local gap: number = positionDelta.Magnitude
local engageThreshold: number = math.max(
Config.Ball.RenderSmoothing.EngageMin,
expectedTravel
)
if gap >= engageThreshold then
smoothing = true
offset = positionDelta
end
However, if you want a simple counter that displays the averaged FPS (obviously don’t leave the print statement in production, instead you would maybe use the value in a UI display). You can use this, which smooths the FPS:
local frames = 0
local elapsed = 0
local averageFps = 0
RunService:BindToRenderStep("fpsCounter", 9999,
function(deltaTime)
frames += 1
elapsed += deltaTime
if elapsed >= 0.1 then
local measuredFps = frames / elapsed
averageFps = if averageFps == 0
then measuredFps
else (measuredFps + averageFps) / 2
frames = 0
elapsed = 0
end
print(averageFps)
end
)