Alternative to RunService.Heartbeat due to FPS problem

Hello! So I was creating a stamina system for my game, which required a tutorial because I am not the best at scripting. Anyways, I noticed something strange when testing the system out. Since RunService.Heartbeat is based on frames, changing the fps on Roblox using the new feature significantly impacts the stamina mechanism. I have attached a video below showing the problem. Since my game is somewhat competitive, having a lower fps grants the player significantly more stamina than someone with a higher fps.

So my question is, what alternatives are there for my server script. I wanted to use a while task.wait(0.1) do however it does not look as smooth as heart beat, and I really don’t want to add yet another while true do loop to my game. Any suggestions?

Stamina

Heartbeat fires with the elapsed time since the previous frame. You can multiply this value against your stamina cost per second while running, to drain the stamina at a consistent rate, independent of FPS.

That makes sense, but I am not sure how to get the first value you’re referring to.

For the Heartbeat event, delta-time is fed as the connected function’s first parameter.

RunService.Heartbeat:Connect(function(TimePassedInSeconds)
	local StaminaCost = StaminaDrainPerSecond * TimePassedInSeconds
end)
1 Like

My stamina cost is one though, so won’t it not change anything?
stamina2

Delta-time is a precise value. So if a tenth of a second passed since the last frame, it would be 0.1. So if your stamina cost per second was 1, the resulting stamina loss for that frame after multiplying these two values together, would correctly be 0.1.

1 Like

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