How to fix Stamina issue?

basically, i have a stamina script and if the players FPS is High, then stamina will go down fast, but if the players FPS is Low, then stamina will go down slower. I know its because im using RenderStepped to add/remove stamina, but how could i make it so stamina is the same with all FPS?

LOCAL SCRIPT:

local players = game:GetService("Players")
local Player = players.LocalPlayer
local runService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local character = players.LocalPlayer.Character or Player.CharacterAdded:Wait()
local hum = character:WaitForChild("Humanoid")
local userInputService = game:GetService("UserInputService")
local Camera = workspace.CurrentCamera
local FOV = Camera.FieldOfView
local debounce = false
local maxStamina = script:WaitForChild("maxStamina").Value
local staminaRegen = .1
local staminaCost = .2
local staminaCost2 = .15
local sprintSpeed = character:WaitForChild("SprintSpeedValue").Value
local jogSpeed = character:WaitForChild("JogSpeedValue").Value
local walkSpeed = character:WaitForChild("WalkSpeedValue").Value
local Toggle = false
local Climbing = Enum.HumanoidStateType.Climbing
local Moving = false
local Sprinting = false
local Jogging = false
local currentStaminaValue = script:WaitForChild("currentStamina").Value
local currentStamina = maxStamina

task.wait()
local FullSprintAnim = hum.Animator:LoadAnimation(script.Animation) or script:WaitForChild("Animation")
local WalkAnim = hum.Animator:LoadAnimation(script.Walk) or script:WaitForChild("Walk")
local IdleAnim = hum.Animator:LoadAnimation(script.Idle) or script:WaitForChild("Idle")
local LightJogAnim = hum.Animator:LoadAnimation(script.LightJog) or script:WaitForChild("LightJog")

local anim = character:WaitForChild("Animate")
hum.WalkSpeed = walkSpeed










local StaminaBackground = players.LocalPlayer.PlayerGui.Stamina.Background
local StaminaBar = players.LocalPlayer.PlayerGui.Stamina.Bar
local Icon = StaminaBackground:WaitForChild("Icon")



StaminaBar.Transparency = StaminaBar.Transparency
--TWEENS
local tweenInfo = TweenInfo.new(
	1, -- Time
	Enum.EasingStyle.Quad, -- EasingStyle
	Enum.EasingDirection.In, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)

local tween = TweenService:Create(StaminaBar, tweenInfo, {Transparency = 1})

StaminaBackground.Transparency = StaminaBackground.Transparency

local tweenInfo = TweenInfo.new(
	1, -- Time
	Enum.EasingStyle.Quad, -- EasingStyle
	Enum.EasingDirection.In, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)

local tween2 = TweenService:Create(StaminaBackground, tweenInfo, {Transparency = 1})

local tweenInfoText = TweenInfo.new(
	1, -- Time
	Enum.EasingStyle.Quad, -- EasingStyle
	Enum.EasingDirection.In, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)

local tweenInfoIcon = TweenInfo.new(
	1, -- Time
	Enum.EasingStyle.Quad, -- EasingStyle
	Enum.EasingDirection.In, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)

local tweenIcon = TweenService:Create(Icon, tweenInfo, {ImageTransparency = 1})

local tweenInfoIcon2 = TweenInfo.new(
	1, -- Time
	Enum.EasingStyle.Quad, -- EasingStyle
	Enum.EasingDirection.In, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)

local tweenIcon2 = TweenService:Create(Icon, tweenInfo, {ImageTransparency = 0})

local tweenInfoSprint = TweenInfo.new(
	.5, -- Time
	Enum.EasingStyle.Quad, -- EasingStyle
	Enum.EasingDirection.InOut, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)

local SprintFOVTween = TweenService:Create(Camera, tweenInfoSprint, {FieldOfView = 85})

local tweenInfoSprintStop = TweenInfo.new(
	.25, -- Time
	Enum.EasingStyle.Quad, -- EasingStyle
	Enum.EasingDirection.InOut, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)
local SprintStoppedFOVTween = TweenService:Create(Camera, tweenInfoSprintStop, {FieldOfView = 70})


local tweenInfoJog = TweenInfo.new(
	.75, -- Time
	Enum.EasingStyle.Quad, -- EasingStyle
	Enum.EasingDirection.InOut, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)

local JogFOVTween = TweenService:Create(Camera, tweenInfoSprint, {FieldOfView = 79})

local tweenInfoJogStop = TweenInfo.new(
	.5, -- Time
	Enum.EasingStyle.Quad, -- EasingStyle
	Enum.EasingDirection.InOut, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)
local JogStoppedFOVTween = TweenService:Create(Camera, tweenInfoSprintStop, {FieldOfView = 70})

local SprintTweenInfo = TweenInfo.new(
	1, -- Time
	Enum.EasingStyle.Quad, -- EasingStyle
	Enum.EasingDirection.InOut, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)
local SprintTween = TweenService:Create(hum, SprintTweenInfo, {WalkSpeed = 22})

local SprintTweenInfo = TweenInfo.new(
	1, -- Time
	Enum.EasingStyle.Quad, -- EasingStyle
	Enum.EasingDirection.InOut, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)
local SprintStopTween = TweenService:Create(hum, SprintTweenInfo, {WalkSpeed = walkSpeed})
--



--Moving
hum.Running:Connect(function(speed)
	if speed > 2 then
		Moving = true
		IdleAnim:Stop()
		WalkAnim:Play()
	else
		IdleAnim:Play()
		WalkAnim:Stop()
		Moving = false
	end
end)
--



-- Update Stamina GUI
function updateGui(current, max)

	--print("Current:",current,"Max:",max,"c/m:",current/max,"Background Size:", StaminaBackground.AbsoluteSize.X)
--]]
	if currentStamina >= maxStamina then
		tween:Play()
		tween2:Play()
		tweenIcon:Play()
	else
		StaminaBar.Transparency = 0
		StaminaBackground.Transparency = 0.6
		
		tweenIcon2:Play()
		tween:Cancel()
		tween2:Cancel()

	end


	players.LocalPlayer.PlayerGui.Stamina.Bar.Size = UDim2.new(0,current/max*StaminaBackground.AbsoluteSize.X, 0, 35)
end
--hum.Running:Connect(function(speed)
	userInputService.InputBegan:Connect(function(input)
		--if speed > 0 then return end
	if input.KeyCode == Enum.KeyCode.LeftShift and debounce == false  and Moving and Jogging == false then
		
		if currentStamina >= staminaCost then
			Sprinting = true
				FullSprintAnim:Play()
				hum.WalkSpeed = sprintSpeed
				SprintFOVTween:Play()
			if hum.WalkSpeed ~= sprintSpeed then
					Sprinting = false
					FullSprintAnim:Stop()
				end
				debounce = true
				task.wait(.7)
				debounce = false
			end
--
		elseif input.KeyCode == Enum.KeyCode.X and debounce == false and Moving and Sprinting == false then
		if currentStamina >= staminaCost2 then
			Jogging = true
				LightJogAnim:Play()
				hum.WalkSpeed = jogSpeed
				JogFOVTween:Play()
			if hum.WalkSpeed ~= jogSpeed then
				Jogging = false
					LightJogAnim:Stop()
				end
				debounce = true
				task.wait(.5)
				debounce = false
			end
			--]]
		end
	end)

-- Sprint Key Released

	userInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Sprinting = false
		SprintStoppedFOVTween:Play()

			FullSprintAnim:Stop()
			hum.WalkSpeed = walkSpeed
			--
	elseif input.KeyCode == Enum.KeyCode.X then
		Jogging = false
			JogStoppedFOVTween:Play()
			LightJogAnim:Stop()
			hum.WalkSpeed = walkSpeed
			--]]
		end
	end)
--end)

--
runService.RenderStepped:Connect(function(FPS)

	if Sprinting then
		LightJogAnim:Stop()
		if currentStamina >= staminaCost and Moving then
			currentStamina -= staminaCost
		else
			hum.WalkSpeed = walkSpeed
			FullSprintAnim:Stop()
			SprintStoppedFOVTween:Play()
		end 
	elseif Jogging then
		if currentStamina >= staminaCost2 and Moving then
			currentStamina -= staminaCost

			FullSprintAnim:Stop()
		else
			hum.WalkSpeed = walkSpeed

			JogStoppedFOVTween:Play()
			LightJogAnim:Stop()
		end
	else
		if currentStamina < maxStamina then 
			task.wait(1)
			currentStamina += staminaCost
		elseif currentStamina > maxStamina then
			currentStamina = maxStamina
		end
	end
	updateGui(currentStamina, maxStamina)
end)
-------HEALTH

local OGSaturation = game.Lighting.ColorCorrection.Saturation
local HeartBeat = workspace["Slow Heartbeat"]
hum.HealthChanged:Connect(function(health)
	
	game.Lighting.ColorCorrection.Saturation = health / 100 - 1

	
end)

hum.Died:Connect(function()
	game.Lighting.ColorCorrection.Saturation = 0
end)
--]]

I get that delta time would probably fix it, but i just do not know how i should implement it into my script if that makes sense

use a local script and a remote event, if you do it on the client it won’t be faster or slower

thats one way, but i want to keep it client sided

oh so it’s already a local script?

well then your only choice is to change the system and not use runservice

not true cause there is a solution using delta time and others but i dont know how to implement it

This is RunService part v

RunService.RenderStepped:Connect(function(FPS)
    -- FPS is DeltaTime
	if Sprinting then
		LightJogAnim:Stop()
		if currentStamina >= staminaCost and Moving then
			currentStamina -= (staminaCost * FPS)
		else
			hum.WalkSpeed = walkSpeed
			FullSprintAnim:Stop()
			SprintStoppedFOVTween:Play()
		end 
	elseif Jogging then
		if currentStamina >= staminaCost2 and Moving then
			currentStamina -= (staminaCost * FPS)

			FullSprintAnim:Stop()
		else
			hum.WalkSpeed = walkSpeed

			JogStoppedFOVTween:Play()
			LightJogAnim:Stop()
		end
	else
		if currentStamina < maxStamina then 
			task.wait(1)
			currentStamina += (staminaCost * FPS)
		elseif currentStamina > maxStamina then
			currentStamina = maxStamina
		end
	end
	updateGui(currentStamina, maxStamina)
end)

It will reduce stamina by staminaCost Per Second (No matter how lag you are)

2 Likes