Using RunService with an FPS unlocker

I’m currently making a game where I had the max movement speed of a ship controlled by the players frame rate with RunService:RenderStepped, but that allowed for me to unlock my fps and move at insane speeds. I tried using while wait() do, but that felt too slow. How would I make a wait or function always run 60 times per second such that it isn’t depending on the frame rate of the user?

3 Likes

The easiest way to do that is to scale the distance the ship goes by the amount of time that passed in the meanwhile, through the deltaTime argument.
So instead of:

RunService.Stepped:Connect(function()
	ship.Position += speed * 6
end)

You would do:

RunService.Stepped:Connect(function(dt)
	local scale = dt*60
	ship.Position += speed * 6 * scale
end)
15 Likes

Time to plug: don’t use RenderStepped if you aren’t updating small things like the Camera. Code in RenderStepped actually blocks frames from being rendered. If you need to rely on RenderStepped, use BindToRenderStep instead. If not, use either Stepped or Heartbeat. Stepped will run before physics simulation and Heartbeat will run after. Stepped would probably be the most appropriate for your case.

6 Likes

thanks for this info, but for future readers make sure instead of this:

RunService.RenderStepped:Connect(function(dt)
	local scale = dt*60
	ship.Position = ship.Position + speed * 6 * scale
end)

you use:

RunService.Stepped:Connect(function(time, dt)
	local scale = dt*60
	ship.Position = ship.Position + speed * 6 * scale
end)
8 Likes