How to make heartbeat stay at 30FPS

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

while true do
wait()
end

better than heartbeat? whats the difference

2 Likes

yes

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)

probably not.

3 Likes

I think you can use delta time, but im not sure. Look into delta time in the runService documentation and you should find something.

2 Likes
local FPS = 1/30
while true do
task.wait(FPS)
end

2 Likes

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)
1 Like
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)
1 Like

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