Having problem with a running system

I’m making a running system, but i’m having a problem, if the player starts to run in random directions like this:

he starts to lose alot of stamina, and i dont want that.

i have another problem, if the player starts moving a little and then starts running like this:

he doesn’t lose any stamina

so this is my code:

local UIS = game:GetService("UserInputService")

local char = game:GetService("Players").LocalPlayer.Character
local Hum = char:WaitForChild("Humanoid")

local RS = game:GetService("RunService")

local Stamina = char:GetAttribute("Stamina")
local run = false

local Label = script.Parent

UIS.InputBegan:Connect(function(io, gpe)
	if Stamina <= 4 then
		return
	end
	if UIS:IsKeyDown(Enum.KeyCode.LeftShift) and gpe ~= true then
		run = true
		Hum.WalkSpeed = 24
		while run == true and Hum.MoveDirection.Magnitude > 0 do
			task.wait(.4)
			Label.Text = Stamina
			Stamina -= 1
			if Stamina <= 0 then
				run = false
				Hum.WalkSpeed = 16
				if Stamina < 0 then
					Stamina = math.clamp(Stamina,0,100)
				end
				break
			end
		end
	end
end)

UIS.InputEnded:Connect(function(io,gpe)
	if io.KeyCode == Enum.KeyCode.LeftShift and gpe ~= true then
		Hum.WalkSpeed = 16
		run = false
		while run ~= true do
			task.wait(.2)
			Stamina += 3
			Label.Text = Stamina
			if Stamina >= 100 then
				if Stamina > 100 then
					Stamina = math.clamp(Stamina,0,100)
				end
				break
			end
		end
	end
end)

while true do
	task.wait(.5)
	print(Stamina)
end

anyway, if someone knows to answer my question i’m going to be happy :123: :+1:

Thanks for reading!

Hi.
this is my first time posting in forums but i think i fixed your issue with the stamina. I also cleaned up some aspects of the code.

local UIS = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local Hum = char:WaitForChild("Humanoid")

local Stamina = 100  -- Local variable to track stamina.
local run = false

local Label = player.PlayerGui.ScreenGui.TextLabel
local DepleteRate = 0.05
local RegenRate = 0.09
game:GetService("Players").LocalPlayer.PlayerScripts.EnableMouseLockOption = false -- default shiftlock interferes with the shift bind. I'd recommend you change it to "CTRL".
-- Function to handle stamina depletion while running
local function handleRunning()
	while run and Hum.MoveDirection.Magnitude > 0.1 and Stamina > 0 do
		Stamina = math.max(Stamina - 1, 0)  -- Decrease stamina, ensuring it doesn't go below 0
		Label.Text = Stamina
		Hum.WalkSpeed = 24
		task.wait(DepleteRate)
	end

	-- If stamina runs out or player stops running
	if Stamina <= 0 or not run then
		run = false
		Hum.WalkSpeed = 16
	end
end

-- Function to handle stamina regeneration
local function handleRegeneration()
	while not run and Stamina < 100 do
		Stamina = math.min(Stamina + 1, 100)  -- Increase stamina, ensuring it doesn't exceed 100
		Label.Text = Stamina
		task.wait(RegenRate)
	end
end

-- Detect when the player starts running
UIS.InputBegan:Connect(function(io, gpe)
	if io.KeyCode == Enum.KeyCode.LeftShift and not gpe and Stamina > 0 then
		run = true
		handleRunning()
	end
end)

-- Detect when the player stops running
UIS.InputEnded:Connect(function(io, gpe)
	if io.KeyCode == Enum.KeyCode.LeftShift and not gpe then
		run = false
		Hum.WalkSpeed = 16
		handleRegeneration()
	end
end)

-- Optional: Display stamina in the console
-- game:GetService("RunService").RenderStepped:Connect(function()
--     print("Stamina:", Stamina)
-- end)

Your code got the idea down but it kept calling the stamina function twice. so i made it into its own function and changed the logic of how it detects if the characters running.
I added some extra customizable parameters for ease of access. Replace local Label = player.PlayerGui.ScreenGui.TextLabel with your actual text label though.

hope this helps. if u have questions just ask

Also for the running animation, try cancelling the walk animation when the player starts running.
In the animate script just check if the move direction is over a certain threshold, then play the run animation

2 Likes

it worked thanks, but i still didnt understand why the player not loosing stamina or losing stamina too fast was happening and what you did to fix this, can you explain it to me? anyways, thanks for the help

so, i think that i discovered what was causing the problem, and the script that you made had a problem, if you hold ctrol and stop walking and after sometime you continue walking you’re not going to lose stamina and i think that i fixed that

Here is the script:

local UIS = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local Hum = char:WaitForChild("Humanoid")

local Stamina = 100  -- Local variable to track stamina.
local run = false

local Label = script.Parent
local DepleteRate = 0.05
local RegenRate = 0.09 -- default shiftlock interferes with the shift bind. I'd recommend you change it to "CTRL".
-- Function to handle stamina depletion while running
local function handleRunning()
	while run and Stamina > 0 and run == true do
		if Hum.MoveDirection.Magnitude > 0.1 then
			Stamina = math.max(Stamina - 1, 0)  -- Decrease stamina, ensuring it doesn't go below 0
			print("Foi")
			Label.Text = Stamina
			Hum.WalkSpeed = 24
			task.wait(DepleteRate)
		else
			Stamina = math.min(Stamina + 1, 100) --If you are holding shift but you are not moving you can regen stamina, you can remove this if you want
			Label.Text = Stamina
			task.wait(RegenRate)
		end
	end

	-- If stamina runs out or player stops running
	if Stamina <= 0 or not run then
		run = false
		Hum.WalkSpeed = 16
	end
end

-- Function to handle stamina regeneration
local function handleRegeneration()
	while not run and Stamina < 100 do
		Stamina = math.min(Stamina + 1, 100)  -- Increase stamina, ensuring it doesn't exceed 100
		Label.Text = Stamina
		task.wait(RegenRate)
	end
end

-- Detect when the player starts running
UIS.InputBegan:Connect(function(io, gpe)
	if io.KeyCode == Enum.KeyCode.LeftShift and not gpe and Stamina > 0 and run == false then
		run = true
		handleRunning()
	end
end)

-- Detect when the player stops running
UIS.InputEnded:Connect(function(io, gpe)
	if io.KeyCode == Enum.KeyCode.LeftShift and not gpe then
		run = false
		Hum.WalkSpeed = 16
		handleRegeneration()
	end
end)




-- Optional: Display stamina in the console
 game:GetService("RunService").RenderStepped:Connect(function()
     print("Stamina:", Stamina)
 end)

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