How would I make this?

Hello, I’m making a zombie waves system but Im confused about what to do next. How the zombies spawn, is by 15 parts organized in the line. If there are less then 15 of a type of zombie spawning, I want them to spawn as close to the center as possible. But if there are more than 15 zombies, then I want a full line to spawn, and repeat until all the zombies are spawned. I already organized the table that has the closest ones in it, but now I need to implement the availability and I have no idea how to do that.

If needed here’s the script:

local distances = {}
for i,zombieSpawnPosition in pairs(possibleZombieSpawnPositions:GetChildren()) do
    local distanceValue = Instance.new("NumberValue",zombieSpawnPosition)
	distanceValue.Value = (centerZombieSpawnPosition - zombieSpawnPosition).Magnitude
	distanceValue.Name = "Distance"
	
	local availabilityValue = Instance.new("BoolValue",zombieSpawnPosition)
	availabilityValue.Value = true
	
	table.insert(distances,{zombieSpawnPosition,distanceValue.Value,availabilityValue})
end
for i,zombieSpawnPosition in pairs(possibleZombieSpawnPositions:GetChildren()) do
	table.sort(distances,function(a,b)
		return a[2] < b[2]
	end)
	print(table.unpack(distances))
end
if zombies <= 15 then
	
else
	for index4,spawnLine in pairs() do
		
	end
end

Thanks for the help!

1 Like

Any help possible is appreciated.

Wdym by this? As a sphere or a line?

In the line of 15 possible spawn parts. I made this in the script right here:

for i,zombieSpawnPosition in pairs(possibleZombieSpawnPositions:GetChildren()) do
    local distanceValue = Instance.new("NumberValue",zombieSpawnPosition)
	distanceValue.Value = (centerZombieSpawnPosition - zombieSpawnPosition).Magnitude
	distanceValue.Name = "Distance"
	
	local availabilityValue = Instance.new("BoolValue",zombieSpawnPosition)
	availabilityValue.Value = true
	
	table.insert(distances,{zombieSpawnPosition,distanceValue.Value,availabilityValue})
end

I don’t get this part (prolly cuz im being an idiot), what are you trying to achieve?

When a zombie spawns on a spawn part, its availability will become false, which means that another zombie can’t spawn there until set back to true

So just check if that value is true while positioning the zombies

Yeah but then how do I turn off the availability when they spawn

Well, assuming that you loop over each zombiePosition to spawn the zombies, just set the availabilityValue to false either through the table or by indexing the child of the spawn position

1 Like

Something like this?

for i,zombieSpawnInfo in pairs(distances) do
	local zombie = replicatedStorage:WaitForChild("Zombies"):WaitForChild("NormalZombie"):Clone()
	zombie.HumanoidRootPart.CFrame = zombieSpawnInfo[1][zombieSpawnInfo[#zombieSpawnInfo]]
	zombieSpawnInfo[3].Value = false
end

Honestly I have no idea what this for loop is

for _, zombieSpawnInfo in distances do
	local zombie = replicatedStorage.Zombies.NormalZombie:Clone()
	zombie:PivotTo(CFrame.new(zombieSpawnInfo[1])]
	zombieSpawnInfo[3].Value = false
end
1 Like

I’m sorry but something is not working and I don’t know what it is

while wavesStarted == true do		
	local waveInfo = workspace:WaitForChild("WaveInfo")
	local currentWaveZombies = waveInfo:WaitForChild("Wave"..wave.."Zombies")
	
	updateClientIntermissionText.Value = "Spawning zombies"
	local centerZombieSpawnPosition = possibleZombieSpawnPositions:WaitForChild("CenterZombieSpawnPosition")
	for i,zombieType in pairs(currentWaveZombies:GetChildren()) do
		local zombies = zombieType.Value
		if zombieType.Name == "NormalZombies" then
			for index2 = zombies,1,-1 do
				local distances = {}
				for i,zombieSpawnPosition in pairs(possibleZombieSpawnPositions:GetChildren()) do
					local distanceValue = Instance.new("NumberValue",zombieSpawnPosition)
					distanceValue.Value = (centerZombieSpawnPosition - zombieSpawnPosition).Magnitude
					distanceValue.Name = "Distance"
					
					local availabilityValue = Instance.new("BoolValue",zombieSpawnPosition)
					availabilityValue.Value = true
					
					table.insert(distances,{zombieSpawnPosition,distanceValue.Value,availabilityValue})
				end
				for i,zombieSpawnPosition in pairs(possibleZombieSpawnPositions:GetChildren()) do
					table.sort(distances,function(a,b)
						return a[2] < b[2]
					end)
					print(table.unpack(distances))
				end
				if zombies <= 15 then
					for i,zombieSpawnInfo in pairs(distances) do
						if zombieSpawnInfo[3].Value == true then
							local zombie = replicatedStorage.Zombies.NormalZombie:Clone()
							zombie.HumanoidRootPart.CFrame = zombieSpawnInfo[1].CFrame
							zombieSpawnInfo[3].Value = false
						end
					end
				else
					for index4,spawnLine in pairs() do
						
					end
				end
			end 
		elseif zombies.Name == "SpeedZombies" then
		elseif zombies.Name == "SlowZombies" then
		elseif zombies.Name == "MiniBossZombies" then
		end
	end
	
	for i = waveLength,1,-1 do
		for index2,player in pairs(players) do
			if player then
				character = player.Character
				if character then
					if not character:FindFirstChild("GameTag") then
						table.remove(players,index2)
					end
				end
			else
				table.remove(players,index2)
			end
		end
		updateClientIntermissionText.Value = "Wave "..wave..": "..i.." seconds left"
		
		if #zombiesFolder:GetChildren() == 0 then
			updateClientIntermissionText.Value = "Wave "..wave.." completed"
			break
		end
	end
	if #zombiesFolder:GetChildren() ~= 0 then
		updateClientIntermissionText.Value = "Starting next wave"
		wait(3)
	end
	wave = wave + 1
end

Just ask if you need anything

Does this prioritize the closest spawn part to the center?

What do I put inside the if and for loop at the end?

It is unnecessary to run the table.sort function inside a loop

Yes, as you sort the table to have the closest distances first

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.