How do I make a while do and/or renderstepped function run at a maximum of 60FpS/Hz?

In my game, some of the animations rely on CFrame lerps in the Renderstepped function below. As well as this, the automatic fire system uses a while do function. The problem is, both of these functions run faster with an FpS unlocker. How can I lock these functions to 60 FpS?

RenderStep function

Run.RenderStepped:Connect(function(deltaTime)
	
	if dead.Value == false and gun.weapon then
		
		local mouseDelta = game:GetService("UserInputService"):GetMouseDelta()
		
		mouse.TargetFilter = cam
		
		DT = deltaTime

		if switching then
			gun.switchCF = gun.switchCF:Lerp(gun.module.switchcf, .1)
		elseif switching == false then
			gun.switchCF = gun.switchCF:Lerp(CFrame.new(), .1)
		end

		if gun.weapon then

			gun.weapon:SetPrimaryPartCFrame(cam.CFrame 

				* CFrame.new(gun.module.regularcf.Position.X,-1.7,gun.module.regularcf.Position.Z)
				* gun.switchCF
				* gunbobcf
				* aimcf
				* sprintcf
				* recoilcf

			)

		end

		-- [[ States ]] --
		if (gun.state == gun.states.idle) then
			gun.mainCF = gun.mainCF:lerp(CFrame.new(gun.module.regularcf.Position.X,-1.7,gun.module.regularcf.Position.Z),.3)
			
			if not gun.animationTrack.idle.IsPlaying then
				gun.animationTrack.idle:Play()
			end
			
		end
		
		if running then
			gunbobcf = gunbobcf:Lerp(CFrame.new(
				0.14 * math.sin(tick() * 6), -- Intensity left/right * speed
				0.07 * math.sin(tick() * 12), -- Intensity up/down * speed
				-0.005 * math.sin(tick() * 1) -- Intensity back/forward * speed
				
				),	0.1)
			
			--[[
		elseif running and aiming then
			gunbobcf = gunbobcf:Lerp(CFrame.new(
				0.04 * math.sin(tick() * 1), -- intensity left/right * math.sin(tick() * speed
				0.02 * math.sin(tick() * 2), -- intensity up/down * math.sin(tick() * speed
				-0.025 * math.sin(tick() * 1)
				), 0.1)
				
				--]]
			
		elseif sprinting then
			gunbobcf = gunbobcf:Lerp(CFrame.new(
				0.14 * math.sin(tick() * 6), -- intensity left/right * math.sin(tick() * speed
				0.07 * math.sin(tick() * 6), -- intensity up/down * math.sin(tick() * speed
				-0.3 * math.sin(tick() * 6)
				), 0.1)
		else
			gunbobcf = gunbobcf:Lerp(CFrame.new(),0.1)
		end
		
		
		if sprinting then
		--	print('is sprihnting')
			local tween = Tween:Create(cam, aimTween, {FieldOfView = 55/1.4}) --fov+zoom
			tween:Play()
			gun.mainCF = gun.mainCF:Lerp(CFrame.new(gun.module.regularcf.Position.X,-1.7,gun.module.regularcf.Position.Z) * CFrame.Angles(-.75,1,0.25),.1)
			
			if char.Humanoid.WalkSpeed > .1 then
				gunbobcf = gunbobcf:Lerp(CFrame.new(
					0.14 * math.sin(tick() * 1), -- intensity left/right * math.sin(tick() * speed
					0.07 * math.sin(tick() * 2), -- intensity up/down * math.sin(tick() * speed
					-0.7 * math.sin(tick() * 8)
					), 0.1)
			end
		
		else
			local tween = Tween:Create(cam, aimTween, {FieldOfView = 55}) --fov
			tween:Play()
			gun.mainCF = gun.mainCF:Lerp(CFrame.new(),.1)
		end
		
		
		--[[
		
		if aiming then
			local tween = Tween:Create(cam, aimTween, {FieldOfView = 70/gun.module.Zoom}) --fov+zoom
			tween:Play()
			aimcf = aimcf:Lerp(gun.module.aimcf, 0.1) -- goal cframe (lower number, lower aimspeed)
		else
			local tween = Tween:Create(cam, aimTween, {FieldOfView = 70}) --fov
			tween:Play()
			aimcf = aimcf:Lerp(CFrame.new(), 0.1)
		end
		
		
		if crouching then
			char.Humanoid.CameraOffset = Vector3.new(0,-4,0)
		
		else
			char.Humanoid.CameraOffset = Vector3.new(0,0,0)
		end
		
		--]]	
		
		gun.springs.inputSway:shove(Vector3.new(mouseDelta.x / 200 , mouseDelta.y / 200)) --not sure if this needs deltaTime filtering
		
		local sway = gun.springs.inputSway:update(deltaTime)
		gun.weapon.Primary.CFrame = gun.weapon.Primary.CFrame * CFrame.Angles(0,-sway.x,-sway.y)
		
		recoilcf = recoilcf:Lerp(CFrame.new(), 0.1)		
	end

end)

While do function

mouse.Button1Down:Connect(function()
	mouseldown = true
	sprinting = false

	
	Events.Sprint:FireServer(gun.module.GunWalkSpeed)	
	
	delay(0, function()
        if gun.module.ShootType == 2 and reloading == false and gun.module.Ammo > 0 then

			if gun.module.Ammo > 0 or gun.module.Ammo == 1 then

				while mouseldown and not sprinting do

					if shooting or sprinting or switching then

						Events.Sprint:FireServer(gun.module.GunWalkSpeed)
						break

					end

					if gun.module.Ammo > 0 or gun.module.Ammo == 1 then
						if reloading == false then

							if gun.module.WeaponType == "Gun" then

								gun.module.Ammo = gun.module.Ammo - 1
								PlrGui.GunInfo.Frame.Ammo.Text = gun.module.Ammo
								PlrGui.GunInfo.Frame.Reserve.Text = gun.module.Reserve

							ShootRecoil()
							newBullet()
							shooting = false
						end
					end
				end
			end
1 Like

You can keep track of a time variable at the top of your script and then add DeltaTime to it.
So like this:

local totalTime = 0

Run.RenderStepped:Connect(function(deltaTime)
	totalTime += deltaTime
	if (totalTime < 1 / 60) then
		return
	end
	totalTime = 0
	
	-- Your code
end)

deltaTime is basically the amount of seconds between this and the previous frame. So if you have 60 fps, deltaTime is basically always around 0,0167 (which is 1 / 60) and if you have higher FPS, deltaTime becomes lower. So you just add the deltaTime to the totalTime until totalTime reaches 1/60th of a frame. I hope this helps you!

Edit: If you want a different framerate limit, just adjust the 1 / 60 to 1 / 30 or whatever framerate you want.

5 Likes

Thanks for this, how would I adjust it to fit a while do loop? :sweat_smile:

Edit 1: i just realised that if it’s running at a lower FPS, my gun model flickers a lot because it’s returning every time that it checks to see if Deltatime is equal to 1/60.

1 Like

Just place anything that you don’t want affected by framerate before the return, and anything you want limited to a framerate after it. Good luck! :slight_smile:

3 Likes