Im not great at adding animations

4.2.1

how should I add my animations to this script for my 2d game?

extends CharacterBody2D

@export var speed: float = 100.0

# Hearts UI node
@onready var hearts_ui = $HeartsUI

# Health
var heart: int = 3

func death():
	if heart <= 0:
		get_tree().reload_current_scene()
		
	death()
		
func take_damage():
	heart -= 1
	if heart < 0:
		heart = 0  # safety so it doesn't go negative
		

func _physics_process(delta: float) -> void:
	var input_direction = Vector2.ZERO

	if Input.is_action_pressed("ui_right"):
		input_direction.x += 1
	if Input.is_action_pressed("ui_left"):
		input_direction.x -= 1
	if Input.is_action_pressed("ui_down"):
		input_direction.y += 1
	if Input.is_action_pressed("ui_up"):
		input_direction.y -= 1
	if input_direction.length() > 0:
		input_direction = input_direction.normalized()
		
	velocity = input_direction * speed
	move_and_slide()

I am only guessing here because you have not given us much to go on. I presume your CharacterBody2D has a sprite child at the moment. Change it to an animated sprite. Then in the animations tab in the editor, import your sprite sheet or your individual sprites. Call the animations names like jump, walk, run, fight, die etc etc. When you sprite is walking play the animation ‘walking’, when idle play the animation ‘idle’, when jumping play the animation ‘jumping’.

animated_sprite.play("walking")

You may be doing something more complex, if so, tell us more details about what type of animation you mean and we can help further.

Animated Sprite in the docs

I tried to add that to the input part when it moves but it did’t work.

What’s the code you tried? I would place the animation logic after the input logic is all done so the animation will rely on the final value of input