How do i fix an progress bar script with an error Invalid set index 'text' (on base: 'null instance') with value of type 'String'

Godot Version

4.2

Question

I have been following a step by step tutorial on making a 2D turn-based RPG combat scene. everything is working so far, except for the function i'm working on.

I have the same exact script, letter for letter, but the tutorial guy’s is working while mine is not. I’m trying to update the progress bar for the health of the character. Here’s the script.
func _update_health_bar():
health_bar.value = cur_hp
health_text.text = str(cur_hp, " / ", max_hp)
When I launch the test, it plays fine but when I press a button that will activate this function, the error: Invalid set index ‘text’ (on base: ‘null instance’) with value of type ‘String’. appears.
I have tried alot but cannot seem to find an answer.
I would be greatful for some input on why it’s not working properly.

The variable health_text is null. I guess it’s supposed to be a reference to a label node, but something is wrong.

1 Like

Thanks, but i need something more concrete.

How do you declare health_text? Do you have something like
@onready var health_text = $Label ?

You can drag&drop the Label node into the code while holding control to automatically create a reference variable to the node.

If you posted the link of the video it might easier to figure out :slight_smile:

Kinda i have this.
@onready var health_text : Label = get_node(“HealthBar/HealthText”)
the video is from a paid course that i can’t share.

Oh ok. Looks like you dont really need to use get_node(), you could just do:
@onready var health_text : Label = $HealthBar/HealthText

But either way, make sure the node path is correct. Again, you can drag&drop the label node into the code to get the correct path.

This worked great thank you so much.
i changed the @onready to the one you sent and the new code for the health text is
$HealthBar/Label.text = str(cur_hp, " / ", max_hp)