My Enemy makeing me have max health all the time after fighting the fist enemy

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By cvans11

i have used the same code for all 4 enemys is this the problem? because there is a queue free at the end here is the code:

extends KinematicBody2D

var curHp : int = 5
var maxHp : int = 5

var movespeed : int = 150
export var xpToGive :int =30

var damage : int = 1
var attackRate : float = 1.0
var attackDist : int = 80
var chaseDist : int = 400

onready var timer = get_node("Timer")
onready var target = get_node("/root/MainScene/player")

func _ready ():
	
	timer.wait_time = attackRate
	timer.start()

func _physics_process(delta):
	
	var dist  = position.distance_to(target.position)
	
	if dist > attackDist and dist < chaseDist:
		
		var vel = (target.position - position).normalized()
		move_and_slide(vel * movespeed) 

func _on_Timer_timeout():
	 
	if position.distance_to(target.position) <= attackDist:
		target.take_damage(damage)
		
func take_damage (dmgToTake):
	curHp -= dmgToTake
	
	if curHp <= 0:
		die()
	
func die ():
	
	target.give_xp(xpToGive)
	queue_free() 

And the player code is:
extends KinematicBody2D

var curHp : int = 10
var maxHp : int = 10
var moveSpeed : int = 250
var damage : int = 1

var totem : int = 0

var curlevel : int = 0 
var curXp : int = 0 
var XpToNextLevel : int = 50
var XpToLevelIncreaseRate : float = 1.2 

var interactDist : int = 70

var vel : Vector2 = Vector2()
var facingDir : Vector2 = Vector2()

onready var raycast = get_node("RayCast2D")
onready var anim = get_node("AnimatedSprite")
onready var ui = get_node("/root/MainScene/CanvasLayer/Ui")

func _ready ():
	ui.update_level_text(curlevel)
	ui.update_health_bar(curHp, maxHp)
	ui.update_xp_bar(curXp, XpToNextLevel)
	ui.update_Totem_Label(totem)
	
	

func _physics_process(_delta):
	
	vel = Vector2()
	
	if Input.is_action_pressed("move_up"):
		vel.y -= 1
		facingDir = Vector2(0, -1)
	
	if Input.is_action_pressed("move_down"):
		vel.y += 1
		facingDir = Vector2(0, 1)
	
	if Input.is_action_pressed("move_left"):
		vel.x -= 1
		facingDir = Vector2(-1, 0)
	
	if Input.is_action_pressed("move_right"):
		vel.x += 1
		facingDir = Vector2(1, 0)
		
	vel = vel.normalized()
	
	move_and_slide(vel* moveSpeed)
	
	manage_animations()
	
func manage_animations ():
	
	if vel.x > 0:
		play_animation("Move right")
	elif vel.x < 0:
		play_animation("Move left")
	elif vel.y < 0:
		play_animation("Move up")  
	elif vel.y > 0:
		play_animation("Move down")
	elif facingDir.x == 1 :
		play_animation("Idle right")
	elif facingDir.x == -1 :
		play_animation("Idle left")
	elif facingDir.y == -1 :
		play_animation("Idle up")
	elif facingDir.y == 1 :
		play_animation("Idle down")
		
func _process(delta):
	
	if Input.is_action_just_pressed("interact"):
		try_interact()
		
func try_interact ():
	
	raycast.cast_to = facingDir * interactDist 
	
	if raycast.is_colliding():
		if raycast.get_collider() is KinematicBody2D:
			raycast.get_collider().take_damage(damage)
		elif raycast.get_collider().has_method("on_interact"):
			raycast.get_collider().on_interact(self)
			
func play_animation (anim_name):
	if anim.animation != anim_name: 
		anim.play(anim_name)

func give_xp (amount):
	
	curHp += amount
	
	ui.update_xp_bar(curXp, XpToNextLevel)

	
	if curXp >= XpToNextLevel:
		level_up()
		
func level_up ():
	var overflowXp = curXp - XpToNextLevel
	
	XpToNextLevel *= XpToLevelIncreaseRate
	curXp = overflowXp
	curlevel += 1
	
	ui.update_xp_bar(curXp, XpToNextLevel)
	ui.update_level_text(curlevel)
	
func give_totem (amount):
	
	totem += amount
	ui.update_Totem_Label(totem)

func take_damage (dmgToTake):
	
	curHp -= dmgToTake
	
	ui.update_health_bar(curHp, maxHp)
	
	if curHp <= 0:
		die()
		
func die ():
	
	get_tree().reload_current_scene()

Edited to fix code formatting…

jgodfrey | 2023-03-07 00:18

:bust_in_silhouette: Reply From: zhyrin

In give_xp() you increase the current health, not xp.