Spawn Manager Bug

Godot Version

Godot version 4.5

Question

I’m making a game for a game Jam and this is my wave/spawn manager for the enemies. Although it works there is a bug were the enemy gets instantiated twice at the start of a new wave and I can’t figure out why. Help is greatly appreciated.

*I did remove some of the not relevant code


@onready var spawn_timer: Timer = $spawnTimer
var currentWave: int = 1
var enemiesthisWave: int = 1
var spawnQuickness: float = 5
var waveComplete: bool = false


func _process(delta: float) -> void:
	spawn_timer.wait_time = spawnQuickness
	

func _on_spawn_timer_timeout() -> void:
	waveManager()
	print("timer out")


func waveManager():
	if waveComplete == true:
		startnextWave()
	else:
		pass
	checkwaveComplete()


func checkwaveComplete():
	var noEnemies: bool = get_tree().get_nodes_in_group("enemy").is_empty()
	if noEnemies && enemiesthisWave <= 0:
		waveComplete = true
		waveManager()
	if enemiesthisWave >=0: #&& isnotEnemies:
		waveComplete = false
		spawnEnemy()


func spawnEnemy():
	
	if enemiesthisWave > 0 && waveComplete == false:
		var enemy = enemyScene.instantiate()
		enemy.position = enemySpawns.pick_random()
		add_child(enemy)
		print("spawned enemy")
		enemiesthisWave -= 1
	else:
		waveManager()


func startnextWave():
	currentWave += 1
	print("Wave"+str(currentWave))
	enemiesthisWave = currentWave + 1
	spawnQuickness = 10 - currentWave
	if spawnQuickness < 0:
		spawnQuickness = 1
	print(spawnQuickness)
	waveComplete = false
	#waveManager()
	spawn_timer.start()

My guess is it’s a race condition, and you’re spawning an enemy at the end of the previous wave and it appears at the beginning of the new wave.