RunService.RenderStepped Causing stamina to drain faster on 240 fps

How do I fix RenderStepped causing stamina to drain faster on higher fps? a easy fix is to replace it with a while task.wait() do but then its not optimazed

You should make use of the DeltaTime value provided by RenderStepped. If you do something like

game.RunService.RenderStepped:Connect(function(dt)
	Stamina -= 10 * (dt/60)
end)

it’ll change it based on the frame rate

This only happens in Studio because the server will also run at 240fps, however, in the real game the server will run at 60fps.

task.wait() and RunService run on the frame rate, so if your task.wait() is empty, it means it will run on the next frame.

It didn’t work stamina still drains faster on 240 fps in client
Code:

rs.PreRender:Connect(function(dt)
	if Player:FindFirstChild("Baldi") then
		PlayerisBaldi = true
		MobileButtionsFrame["Flashlight buttion"].Visible = false
	else
		PlayerisBaldi = false
		MobileButtionsFrame["Flashlight buttion"].Visible = true
	end
	
	Stamina = math.clamp(Stamina, 0, MaxStamina)
	
	if Stamina <= 0 then
		game.ReplicatedStorage.PlayerTagEventClient:FireServer("Remove","IsRunning")
		run_Animation("Stop")
		Humanoid.WalkSpeed = 16
		SprintToggled = false
	end

	if SprintToggled == true and script:GetAttribute("CanStaminaDrain") == true and Humanoid.MoveDirection.Magnitude > 0 then
		task.wait(0.1 * (dt/60))
		Stamina = Stamina - 0.2
		local String = tostring(Stamina)
		local Split = string.split(String,".")
		StaminaText.Text = Split[1]
		if Stamina <= 10 then
			ColorTweenfunc(1)
		end
	elseif SprintToggled == false or script:GetAttribute("CanStaminaDrain") == false then
		task.wait(0.2 * (dt/60))
		Stamina = Stamina + 0.1
		local String = tostring(Stamina)
		local Split = string.split(String,".")
		StaminaText.Text = Split[1]
		if Stamina == MaxStamina then
			ColorTweenfunc(2)
		end
	end
	
	StaminaBar.Bar:TweenSize(UDim2.new(
			(MaxStamina / MaxStamina * 0.5) * (Stamina / MaxStamina * 2), 
			StaminaBar.Bar.Size.X.Offset,
			StaminaBar.Bar.Size.Y.Scale, 
			StaminaBar.Bar.Size.Y.Offset
	), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0)
end)

Why are you using it in task.wait()? You should be using it in the Stamina = Stamina - 0.2 lines?
I don’t believe you even need the task.wait()

I tried that it broke the stamina

What was the code that you claim broke the stamina

Stamina = Stamina - 0.2 * (dt/60))

this isn’t In the code i sent because I tested this first then changed it

anyway I’m testing this version in game and its working good the problem is now fixed so yeah thanks for the solution