I have a loop that waits for a heartbeat to run. But if my FPS is at like 120 fps, the loop will run faster than intended. I don’t want to use wait(1 / 60)
for something like this, so is there any way I can make a loop run at 60 Hz without it speeding up if the framerate increases?
What does the seconds have to do with the framerate?
Not sure what you mean. What I’m saying is I have a loop that runs every physics frame
while true do
local dt = game:GetService("RunService").Heartbeat:Wait()
end
This would wait a less amount of time if the framerate is higher, so how would I limit how low it would wait?
The framerate dosen’t affect the wait time. The wait time is in seconds. It has nothing to do with the framerate.
I don’t think you understand.
The rate at which the heartbeat event fires at is based of the framerate. I have tested this.
I understand your question, and how Heartbeat, with fps unlockers, fires wayyy faster. You should try to implement a thing where you keep track of the delta time from heartbeat and add it to your own counter stored in a variable, then compare it with the dt you want for the loop to pass again
local dtSoFar = 0
while true do
addingDt = game:GetService("RunService").Heartbeat:Wait()
dtSoFar = dtSoFar + addingDt
if dtSoFar >= 60hzDt then
do code here
dtSoFar = 0
end
end
Calculate 60 hertz dt by printing out the dt on an empty baseplate and getting the dt that appears the most, might not work but this is the one I think should work.
He’s taking about the wait
function specifically, and it is true that framerate doesn’t have an effect on it; Hearbeat:Wait()
, however, is based the client’s / server’s framerate.
You don’t have full control of the frequency of the Heartbeat event and you can’t guarantee that the machine handling the event will run at 60 Hz. You also can’t use the wait
function, which has a minimum wait time of 0.03333
(1 / 30 seconds), so a loop’s frequency with wait will be approximately 30 Hz.
This may be a bit helpful, but you can get the frequency using this. You can check if the player’s frequency is above 60 Hz so you wouldn’t rely on Heartbeat until it drops:
local frequency = RunService.Hearbeat:Wait() ^ -1
Can’t you just divide 1 by 60 to get 60 hertz?
Omg, disregard my post, I realized that my idea would be redundant, oopsie daisy…
Would using Stepped
or RenderStepped
fix my issue here? Or no?
Stepped, RenderStepped and Heartbeat fires every frame, thus all of those are dependent on the machine’s frame rate. The machine’s frame rate is dependent on a number of factors which is simply out of your control, so, you can’t really force the events to run at a frequency of 60 Hz unless the client is really good specifications-wise – the most you can get is 30 Hz using wait, since that function isn’t dependent on the machine’s framerate