Script isn't working and I don't know why

Godot Version

4.3

Question

I’m new to Godot and for a couple days I’ve been trying to figure out why my script suddenly stopped working, it’s connected to my base white circle node(I have no idea what it’s called) and it worked fine before, I tried to change the script but even stuff that I know work don’t and I’m not even sure if the problem has something to do with the script so I didn’t put it in Programming Help.
Here’s the script:

extends Node

var enemy_health = 100

func keyattack(event):

if event.is_action_pressed("take_damage"):
	enemy_health -= 10
	$EnemyHP.text = str(enemy_health)
	
	
	print(enemy_health)
	
	if enemy_health <= 0:
		enemy_health = 0
		$EnemyHP.text = "Dead"
		$EnemyHP.modulate = Color.RED
	
	elif enemy_health == 100:
		$EnemyHP.modulate = Color.WEB_GREEN

So the goal for the script is to lower a number in a text in a Label called EnemyHP it starts at 100 and once it gets to 0 it turns red, but it’s not working and I don’t know why.
I need help.

I think it’s your function name “keyattack” if it probably was _input before. The _input function gets called by the Godot engine if there is any input (key pressed, released, mouse motion, …).
You can clamp your health btw:

enemy_health = clamp(enemy_health - damage, 0, enemy_health)

It just makes sure the first parameter stays between the second and third parameter and returns the clamped result (if larger or smaller than the max/min it returns the max or min)

1 Like

The body of keyattack looks fine.

Do you remember what changes did you make right after the last time it worked?

Or check if keyattack actually gets called or which signal it is connected to.

Thank you, you guys are saints

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