Need help with spawning random amounts of NPC's

I’m attempting at scripting a L4D2 Horde Event, however I am unable to make it so that the game can spawn random amounts of Infected. For example, 6 Infected spawn as a cluster, later on, 9 more spawn. There is no set number of infected to spawn at once. Please, bare with me as I am very new to scripting.

The issue is that I cannot find a way to spawn a somewhat random amount of Infected. I’ve tried using math.random to make it seem like it is spawning a random amount, however, that never worked. Instead, I want a direct approach simply spawning a random amount, not set amount via math.random.

while true do
	if math.random == 2  then
		game.ServerStorage.Zombie:Clone().Parent = game.Workspace.SpawnLocation
		until wait(10)
		if math.random == 1  then
			game.ServerStorage.Zombie:Clone().Parent = game.Workspace.Spawn1
			until wait(15)

(Example of what I tried to do with math.random)

I’ve tried looking up ways to spawn random amounts, though it seems like nobody has posted something like spawning random amounts of NPCs. The DevHub did give me a clue on what I should’ve done, but it wasn’t fit for what I wanted.

Also, it would be favorable if you could include which values do which, It would help me a lot.

local amt = math.random(minAmt,maxAmt)
for i = 1, amt do
	--spawn zombie in area

math.random uses commas, not a dash.
Also, make sure to set a variable for the math.random so you can reference it later. :+1:


Thank you so much, It helps a lot.

1 Like