Sprint stamina drain affected by FPS

Hello guys, I am very new, so I apologize in advance if I am annoying for asking this, however, I am having trouble fixing this, if fixable. Basically, the script works very well, I do have some stuff to modify, but that’s not the concern here.

The issue is that the stamina drain increases as the game’s FPS goes up. The drain is like 10x smaller if I run at 30 FPS, compared to when I run at 175 FPS. What can I do about this issue?

Consists of a “Handler” local script and inside of it, two more local scripts for the sprint states, “Running” and “NotRunning”.

Handler:

local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

local Player = Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")

local StaminaGui = script.Parent
local Holder = StaminaGui:WaitForChild("Holder")
local Bar = Holder:WaitForChild("Bar")

local Stamina = script.Stamina

local TI = TweenInfo.new(0.01, Enum.EasingStyle.Quint)

UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.LeftShift and Humanoid.MoveDirection.Magnitude > 0 then
		if Stamina.Value >= 1 then
			Humanoid.WalkSpeed = 16
			script.Running.Enabled = true
			script.NotRunning.Enabled = false
		end
	end
end)

UserInputService.InputEnded:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		Humanoid.WalkSpeed = 8
		script.Running.Enabled = false
		script.NotRunning.Enabled = true
	end
end)

while task.wait() do
	if Stamina.Value < 100 then
		TweenService:Create(Bar, TI, {Size = UDim2.new(Stamina.Value / 100, 0, 1, 0)}):Play()
	end
end

Running:

local Stamina = script.Parent.Stamina

while task.wait() do
	if Stamina.Value > 0 then
		repeat
			task.wait()
			Stamina.Value = Stamina.Value - 0.25
		until Stamina.Value <= 0
	end
end

NotRunning:

local Stamina = script.Parent.Stamina

while task.wait() do
	if Stamina.Value < 100 then
		repeat
			task.wait()
			Stamina.Value = Stamina.Value + 0.05
		until Stamina.Value >= 100
	end
end

Try using .Changed event for the Value or use GetPropertyChangedSignal(“Value”)

1 Like
local Stamina = script.Parent.Stamina

Stamina.Changed:Connect(function(newVal)
    if newVal < 100 then
        coroutine.wrap(function()
            while Stamina.Value < 100 do
                task.wait()
                Stamina.Value = math.min(100, Stamina.Value + 0.05)
            end
        end)()
    end
end)
1 Like

If I understood correctly, I should replace the NotRunning script with this. I just tried, but I am not sure if I should also be modifying something else. I think I did it wrong.

Correction to my previous reply, I was able to get it working. Thanks to the both of you, @StylishEyepatch and @BobbieTrooper!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.