My zombie spawning function wont work

try this:

   while wait() do
            if (maxnumber.Value <= 1) then
                return
            else
                local spawnLocation = ZombiesSpawns[math.random(1, #ZombiesSpawns)]
                local Zombie = Zombies[math.random(1, #Zombies)]
                local TpTo = nil
                if spawnLocation:IsA("Part") or spawnLocation:IsA("SpawnLocation") then 
                        TpTo = spawnLocation.Position
                 end

            local new = Zombie:Clone()
            new.Parent = ZombieCloningFolder
            wait(.3)
            new:MoveTo(TpTo)        

            maxnumber.Value = maxnumber.Value - 1local spawnLocation = ZombiesSpawns[math.random(1, #ZombiesSpawns)]
            local Zombie = Zombies[math.random(1, #Zombies)]
            local TpTo = CFrame.new(spawnLocation.Position)

            local new = Zombie:Clone()
            new.Parent = ZombieCloningFolder
            new:SetPrimaryPartCFrame(TpTo.CFrame)        

            maxnumber.Value = maxnumber.Value - 1
        end
    end
4 Likes

So is this what my code should look like?

local ZombieSpawnPointsFolder = Map:WaitForChild("ZombieSpawnPoints")
local ZombiesSpawns = ZombieSpawnPointsFolder:GetChildren()

local Difficultys = ReplicatedStorage:WaitForChild("Difficultys"):GetChildren()

local Difficulty = Difficultys[math.random(1, #Difficultys)]

local maxnumber = ReplicatedStorage:WaitForChild("MaxNumber")
local NewWaveEvent = ReplicatedStorage:WaitForChild("NewWave")
local WaveSystem = ReplicatedStorage:WaitForChild("WaveSystem")
local WaveNumber = WaveSystem:WaitForChild("TotalNumberOfWaves")

function NewWave() -- This is the function that errors --

    if Difficulty.Value == 1 then
        maxnumber.Value = 70
        print("Zombies spawning: "..maxnumber.Value)
    elseif Difficulty.Value == 2 then
        maxnumber.Value = 80
        print("Zombies spawning: "..maxnumber.Value)
    elseif Difficulty.Value == 3 then
        maxnumber.Value = 90
        print("Zombies spawning: "..maxnumber.Value)
    elseif Difficulty.Value == 4 then
        maxnumber.Value = 100
        print("Zombies spawning: "..maxnumber.Value)
    elseif Difficulty.Value == 5 then
        maxnumber.Value = 110
        print("Zombies spawning: "..maxnumber.Value)
	end
	
	while wait() do
		if (maxnumber.Value <= 1) then
			return
		else
			local spawnLocation = ZombiesSpawns[math.random(1, #ZombiesSpawns)]
			local Zombie = Zombies[math.random(1, #Zombies)]
			local TpTo = nil
			if spawnLocation:IsA("Part") or spawnLocation:IsA("SpawnLocation") then 
				TpTo = spawnLocation.Position
			end
			local TpTo = CFrame.new(spawnLocation.Position)

			local new = Zombie:Clone()
			new.Parent = ZombieCloningFolder
			wait(.3)
			new:MoveTo(TpTo)        

			maxnumber.Value = maxnumber.Value - 1
			local spawnLocation = ZombiesSpawns[math.random(1, #ZombiesSpawns)]
			local Zombie = Zombies[math.random(1, #Zombies)]
			local TpTo = CFrame.new(spawnLocation.Position)

			local new = Zombie:Clone()
			new.Parent = ZombieCloningFolder
			new:SetPrimaryPartCFrame(TpTo.CFrame)        

			maxnumber.Value = maxnumber.Value - 1
		end
	end
	
end
2 Likes

well my bad, the code iā€™ve given you in it I forgot to remove this part.

So if i remove that it should be right?

2 Likes

Yeah! also make sure the thing you are cloning is a model! or else it will not work.

Yea the model clones, but it still only clones 2 models, would there be a way to clone more?

2 Likes

Why is

local spawnLocation = ZombiesSpawns[math.random(1, #ZombiesSpawns)]
			local Zombie = Zombies[math.random(1, #Zombies)]
			local TpTo = CFrame.new(spawnLocation.Position)

			local new = Zombie:Clone()
			new.Parent = ZombieCloningFolder
			new:SetPrimaryPartCFrame(TpTo.CFrame)        

			maxnumber.Value = maxnumber.Value - 1

being repeated twice in almost the same way, wouldnā€™t it be simpler to do this?

while maxnumber.Value > 1 do
	local spawnLocation = ZombiesSpawns[math.random(1, #ZombiesSpawns)]
	local Zombie = Zombies[math.random(1, #Zombies)]
	local TpTo = nil
	if spawnLocation:IsA("Part") or spawnLocation:IsA("SpawnLocation") then 
		TpTo = spawnLocation.CFrame
		local new = Zombie:Clone()
		new.Parent = ZombieCloningFolder
		wait(.3)
		new:SetPrimaryPartCFrame(TpTo)        

		maxnumber.Value -= 1
	end
	wait()
end

And in terms of it not spawning more than once, maybe what I didā€™ll help so it only decrements if the spawn is not a script

1 Like

Thank you all for all the help! My game would have gotten no where without your help!

3 Likes

Anytime! If you hae anymore issues donā€™t be afraid to make another post!

2 Likes