Help with stamina system

I’ve been trying to make a stamina system that starts regenerating when the player stops moving. Something that is stopping me from doing this is that the script is not letting the sprinting stop even after releasing the sprint key. Below is a video and the code responsible for this.

UIS.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed and input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then
		while UIS:IsKeyDown(Enum.KeyCode.LeftShift) or UIS:IsKeyDown(Enum.KeyCode.RightShift) do
			task.wait()
			Sprint = true
			print("sprint enabled")
			Stamina -= 1
			Stamina = math.clamp(Stamina,0,MaxStamina)
			
		end
	end
end)

UIS.InputEnded:Connect(function(input, gameProcessed)
	if gameProcessed and input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then
		Running = Running
		Sprint = false
		print("sprint disabled")
		while Running ~= true do
			task.wait()
			Stamina += 1
		end
	end
end)

while game:GetService("RunService").RenderStepped:Wait() do
	if Sprint == true then
		Humanoid.WalkSpeed = 20
	else
		Humanoid.WalkSpeed = 16
	end
end

3 Likes

What does Running = Running do?
Maybe that’s the problem with your script.

2 Likes

gameProcessed
Remove this from your if statements.

that’s just to make sure that part of the code knows the variable updated. i am sure that it is separate from the reason.

that worked! thank you so much!

1 Like

while you’re here, I’ve noticed that this fix makes sprinting enable again when shift is released. Do you know why that is the case?

As in your walkspeed is still 20?

yes. exactly. it just changes back to sprinting as soon as i release shift. prints both disabled and enabled and doesn’t change speed.

Add a slight delay before Sprint = false, task.wait() should suffice.

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