NPC Spawner is spawning only 1 instead of looping

My spawner is spawning only 1 zombie instead of looping it

Here is the script:

local spawner = script.Parent

while true do
	local zombie = game.ReplicatedStorage.NPCs:FindFirstChild("Zombie [Enemy]"):Clone()
	zombie.Parent = game.Workspace
	zombie.HumanoidRootPart.CFrame = spawner.CFrame + Vector3.new(0,2,0)
	wait(5)
end

You might not be waiting long enough, because it adds a zombie before waiting 5 seconds, and you’re adding a Vector3 onto a CFrame which you cant do under normal conditions, add a CFrame onto it instead.

EDIT: Nevermind, I’ve been proved wrong.

You can actually add a Vector3 onto a CFrame. I tried adding a CFrame to do this exact thing for my tower defense game a few hours ago and it errored.

(Taken from the DevHub)

I’ve waited enough and still spawning only 1 zombie, and if I change to wait 1 second, it spawn 2 zombies and I’ve changed the CFrame to: zombie.HumanoidRootPart.CFrame = CFrame.new(spawner.Position + Vector3.new(0,2,0))

For debugging, add a print function at the very top of the loop. Let me know if it continues to print every 5 seconds.

It only print once when I wait

There are no errors appearing in the console?

No, there’s no errors appearing.

Not sure if or why this would make a difference, but might as well give it a try. Try utilizing ServerStorage rather than ReplicatedStorage. Let me know if the results are different.

Your code seems to be fine. Do any errors or warnings appear in the output?

No, the results changing to ServerStorage are the same

There are no errors appearing on output.

Are you sure there aren’t? There is no other explanation as to why this is happening, unless they are actually being cloned and you aren’t noticing?

That’s odd. Maybe something here will help:

  • Ensure Zombie [Enemy] has its .Archivable property set to true
  • Ensure the spawner/spawner script aren’t ever destroyed
  • Use task.wait(5) instead of wait(5)

Oh, I’ve spotted the problem, I forgot to Anchor the part

:grin: Happens. Glad you were able to figure it out.

Nice! I was midway typing this while I saw this so im still going to post it.

It may be you using “while true do” what you could do is the following:

local spawner = script.Parent

while task.wait(5) do
	local zombie = game.ReplicatedStorage.NPCs:FindFirstChild("Zombie [Enemy]"):Clone()
	zombie.Parent = game.Workspace
	zombie.HumanoidRootPart.CFrame = spawner.CFrame + Vector3.new(0,2,0)
end

This Runs the script every 5 seconds and also cuts the script down by 1 line.

I also read somewhere (Im not sure if this is true or not) that it is generaly bad practice to use “While true do” and insted use “White task.wait() do” btw.

Just wanna point out that the opposite is true