How to make Health Work?

Godot 4.3

Hello there! I am working on a player, and I am working on a health bar, though its not working. Can someone help me out for a way to make this work?

CODE:
extends CharacterBody2D

var health = MAX_HEALTH
var SPEED = 450.0
const JUMP_VELOCITY = -600.0
const MAX_HEALTH = 12

func _ready():
add_to_group(“Player”)

func damageone() → void:
health -= 1
if health <= 0:
get_tree().reload_current_scene()

func _physics_process(delta: float) → void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta

# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
	velocity.y = JUMP_VELOCITY
if Input.is_action_just_pressed("run"):
	SPEED = 670
if Input.is_action_just_released("run"):
	SPEED = 450

# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction := Input.get_axis("left", "right")
if direction:
	velocity.x = direction * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, 22)

move_and_slide()

func _on_hitbox_area_area_entered(area: Area2D) → void:
if area.is_in_group(“One Hurt”):
damageone()
if health < 0:
get_tree().reload_current_scene()

Can you explain what you expect to happen versus what really happens? Any errors?

Make sure to format your code pastes

1 Like

Well I’m trying to make my health go down by one when interacting with an enemies hitbox on your hitbox. Instead it just touches you a couple times and randomly resets the level, instead of the value being 0.

Are you sure it’s randomly reseting the level? Have you printed out how much health remains after each hit?

no I haven’t

Ok so I’ve come up with a new system by giving the health (progress bar) on the player it’s own script. But it still didn’t work, how could I fix this?

CODE:

extends ProgressBar


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	max_value = 12
	min_value = 0

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float) -> void:
	if value == 0:
		get_tree().reload_current_scene()

func _on_hitbox_area_area_entered(area: Area2D) -> void:
		if area.is_in_group("One hurt"):
			value -= 1

print when the value should change, see what values the print gives you.

it gave me nothing

Then something along this chain must be missing

  1. trying to get to print(value)
  2. the colliding area isn’t in group (capitalization is important) “One hurt”, you could add another print before the if to check
  3. the signal is not connected (potentially a green mark by the func name)

ok, so.
1 - give your variables proper types.
this could be a reference problem.

var health : int = 12
const MAX_HEALTH : int = 12

2 -

yes, that’s what the code does.

3 - you need either add a delay or to get rid of the object causing damage.

func damageone() → void:
	health -= 1
	await get_tree().create_timer(0.05).timeout

4 - you have redundant code:

in damageone

	if health <= 0:
		get_tree().reload_current_scene()

in _on_hitbox_area_area_entered


if area.is_in_group(“One Hurt”):
	damageone()#already does if health reload scene
	if health < 0:
		get_tree().reload_current_scene()