Create a stamina bar and forbid the player for running until it refills to a certain point

Godot Version

4

Question

Hello, I’m practicing a little in Godot and I want to make a stamina bar. The thing is, when the stamina reaches 0, I want to forbid the player from sprinting until it refills to a certain point (say, 20. The range goes from 0 to 100). This is the code I have now in the _physics_process(delta)

if Input.is_action_pressed("ui_sprint"):
		if stamina >= 0:
			speed = 100
			stamina -= 2
			stamina_updated.emit(stamina, max_stamina)
	elif Input.is_action_just_released("ui_sprint"):
		speed = 50

And in _process(delta)

var updated_stamina = min(stamina + regen_stamina * delta, max_stamina)
	if updated_stamina != stamina:
		stamina = updated_stamina
		stamina_updated.emit(stamina, max_stamina)

What should I do to achieve what I want? If you know of any resources to learn from I would appreciate it. Thank you very much!

I actually have a similar system. What I did was have a variable called “can_sprint”, and if the player pressed sprint while “can_sprint” was false, they wouldn’t be able to. Meanwhile, I’d constantly check the value of my progress bar (i.e. stamina bar). If its value is >20, for example, then “can_sprint” is true, and the player is able to sprint. However, if it’s <20, then the variable is set to false.
Hope this helps!

You just need the function updated_stamina to determine if the current stamina is in a certain zone.