Stamina not working

basically, in studio as you can see this SHOULD be the normal stamina amount:


but the problem is that IN-GAME the stamina is ALOT different:

i have seen others with similar problems but their solutions did not help me, i also dont want to post the entire script to avoid people yoinking it, so ill post the math that changes the stamina:
LOCAL SCRIPT: (obviously)

runService.RenderStepped:Connect(function()

	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 -= staminaCost2
			
			FullSprintAnim:Stop()
		else
			hum.WalkSpeed = walkSpeed
			
			JogStoppedFOVTween:Play()
			LightJogAnim:Stop()
		end
	else
		if currentStamina < maxStamina then 
			task.wait(1)
			currentStamina += staminaRegen
		elseif currentStamina > maxStamina then
			currentStamina = maxStamina
		end
	end
	updateGui(currentStamina, maxStamina)
end)

your running this in a renderstepped. Rendersteps run ever frame. the reason its going down faster ingame is because your getting more FPS ingame than in studio. if you try something like this:

runService.RenderStepped:Connect(function(FPS)
currentStamina -= (staminaCost / FPS)
end)

1 Like

When I tried it on studio it worked so its the solution.
Unless it doesn’t work for anybody else.

that makes sense cause i use an fps unlocker, and when my friend tried (who didnt have his on) the stamina was normal for him. HOWEVER this is what i did:

runService.RenderStepped:Connect(function(FPS)

	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 -= (staminaCost2 / FPS)
			
			FullSprintAnim:Stop()
		else
			hum.WalkSpeed = walkSpeed
			
			JogStoppedFOVTween:Play()
			LightJogAnim:Stop()
		end
	else
		if currentStamina < maxStamina then 
			task.wait(1)
			currentStamina += (staminaRegen / FPS)
		elseif currentStamina > maxStamina then
			currentStamina = maxStamina
		end
	end
	updateGui(currentStamina, maxStamina)
end)

and the stamina went down almost instantly. now i know one solution is to cap FPS to 60, but im just wondering if there is another way of doing this without capping FPS

so ill give you the solution for now

If you don’t want to cap the FPS then you can always set the StaminaCosts to extremely low values.

1 Like

does this mean players on lower fps will have more stamina, cause players on higher had less

No, so how this is working is its dividing your current FPS with a value. so in theory if you make the staminaCost 1, it should take away 1 from that value every 60 frames, if theyre using a FPS unlocker every 120 Frames and so on, but the time the stamina takes to drain should be the same

thats weird, just tried it and its almost identical

ok so here is what i did:

runService.RenderStepped:Connect(function(FPS)

	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)

however, the problem is still there and stamina depletes slower (low fps) or faster (high fps)

With render stepped, and any other yielding function you could obtain the time the “step” took as a return value. You could then use this to calculate how much stamina consumed.

The quoted code below could lead you in the right direction.

im not trying to find how much is consumed. i already know the problem but do not know how to fix it: the problem is the stamina depends on the current amount frames, so high frames = less stam, low = high

I know… and determining the delta time between function runs should allow you to determine how much to move the bar based on the fps.

how would i implement this into the script?

local renderStep = runService.RenderStepped -- Based on frame rate
local timer: number = 0 -- If you don't use LuaU, just type: local timer = 0

while timer < 5 do -- Five seconds
local delta = renderStep:Wait()
timer = timer + delta
print(delta)
print(timer)
end

full script(stamina 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 = .015
local staminaCost = .04
local staminaCost2 = .02
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)
--
local renderStep = runService.RenderStepped -- Based on frame rate
local timer: number = 0 -- If you don't use LuaU, just type: local timer = 0

while timer < 5 do -- Five seconds
	local delta = renderStep:Wait()
	timer = timer + delta
	print(delta)
	print(timer)
end
--
runService.RenderStepped:Connect(function(FPS)

	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)
-------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)
--]]