I have a script here that detects weather or not a player is standing on a part or not. However, I wish for it to run at 30FPS. I heard and read that Heartbeat depends on the FPS, so is there a way for heartbeat at 30fps.
Another question, if Heartbeat depends on FPS, does heartbeat on client and serverscript fire at different rates? If so, the rate at which heartbeat fires on serverscripts depends on what?
Is
From the documentation:
The Heartbeat event fires every frame, after the physics simulation has completed. The deltaTime argument indicates the time that has elapsed since the previous frame.
On the server you usually have 60? (not sure) ticks per second, so Heartbeat fires 60 times a second, every 0.0166 seconds (1/60)
this is the solution i’ve come up with, you essentially do nothing on hearbeat until you hit your target framerate.
it becomes more accurate as your fps increases (more accurate on 120 fps than 60)
local RunService = game:GetService("RunService")
local lastExecution = tick()
local frames = 0
local t = tick()
local MAX_FPS = 0
local targetFramerate = 30 --change to framerate
RunService.Heartbeat:Connect(function()
frames += 1
local now = tick()
local elapsed = now-t
if elapsed >= 1 then
t = now
frames = 0
end
--do nothing until target framerate
if now - lastExecution < 1 / targetFramerate then
return
end
lastExecution = now
--code that runs under target fps here
end)
local RunService = game:GetService("RunService")
local targetDelta = 1 / 30
local accumulatedTime = 0
RunService.Heartbeat:Connect(function(deltaTime: number)
accumulatedTime += deltaTime
if accumulatedTime >= targetDelta then
--do stuff here
accumulatedTime = 0
end
end)