Frame changed signal being called before scene is ready

Godot Version

4.3 (Steam)

Question

I currently am working on a mechanic for my top-down game where if you activate a movement where you spin around with your sword, letting go of the spin button will cancel the animation. It was working perfectly fine using a frame changed signal from the animatedsprite2d and when it was called it would check if the sword button is being pressed. But for whatever reason, this signal randomly started being called before the scene was even ready, resulting in it having a nil value for the animatedsprite2d, crashing on load. Here is the signal:

func _on_player_sprite_frame_changed() -> void:
	if "spin" in sprite.animation:
		if not Input.is_action_pressed("sword"):
			animate("walk",current_direction)
			animator.stop()

Basically it throws up an error on the scene loading where it says “Invalid access to property or key ‘animation’ on a base object of type ‘Nil’.”, and the scene is still grey, which means it hasn’t loaded. my animate() function is basically something I made that plays both an animation from animatedsprite2d and animationplayer at one time. I have this so that it will play the walking animation and then stop on the first frame on the walking animation when the sword button is not pressed. It detects if the sprite is playing either “spin_up”, “spin_down”, or “spin_right” (left plays right but flips h) by seeing if “spin” is in the sprite animation.

can just add a checking before the if "spin" in sprite.animation
something like:

func _on_player_sprite_frame_changed() -> void:
	if sprite:
		if "spin" in sprite.animation:
			if not Input.is_action_pressed("sword"):
				animate("walk",current_direction)
				animator.stop()

Thanks, if you can’t tell I’m a noob lol. Although it would be nice if I could figure out why the signal is being called before the scene is ready, this seems like kind of a temporary solution.

you sure it’s the signal is called upon scene creation? i tested it on my place, the frame_changed signal never fired unless you autoplay the sprite
image
image
even if you auto play it, the sprite reference is never null.
tell me how do you reference the sprite