Script stops running on the second time it runs?

So basically I have been making a little game.
And I run into a bug. There is a round system.

And basically every time it gets to the second round it stops working. Anyone know why?

-- variables

local maxAmountAdd = 10 -- the amount to add when a round ends
local groupAmountAdd = 5


local maxNPCS = 100 -- the total we can spawn
local spawned = 0 -- the amount spawned

local NPCSLeft = 0 -- how many are left


local groupAmount = 10 -- the amount to spawn at a time

local currentNight = game:GetService('ServerStorage'):FindFirstChild('CurrentNight')

-- time
local GroupTime = 1 -- time between spawning groups
local intermission = 5

-- servies
local RStorage = game:GetService('ReplicatedStorage')
local lighting = game:GetService('Lighting')

-- events
local CNEvent = RStorage:FindFirstChild('ChangeNight')
local NPCLEvent = RStorage:FindFirstChild('NL')



local function NPCDIED(npc) -- when the NPC dies
	wait(5)
	NPCSLeft -= 1
	NPCLEvent:FireAllClients('Enemies Left:'..NPCSLeft)
end


local function spawnNPC() -- spawn enemy
	spawned += 1
	task.spawn(NPCDIED,nil)
end



local function spawnGroup() -- spawn enemy group
	print('Spawning group')
	for t = 1, groupAmount, 1 do
		if spawned <= maxNPCS then
			spawnNPC()
			print('Spawned NPC')
		else
			print('Max amount added')
		end
	end
	print('Done spawning')
end



local function endRound() -- end round
	maxNPCS += maxAmountAdd
	groupAmount += groupAmountAdd
end




local function startRound() -- start a round
	lighting.ClockTime = 0
	currentNight.Value += 1
	CNEvent:FireAllClients(currentNight.Value)
	NPCSLeft = maxNPCS
	print('Round started')
	NPCLEvent:FireAllClients('Enemies Left:'..NPCSLeft)
	repeat
		if spawned <= maxNPCS then
			spawnGroup()
		end
		wait(GroupTime)
	until NPCSLeft <= 0
	print('Round over')
	endRound()
end

local function Startintermission()
	lighting.ClockTime = 14
	for t = intermission, 0, -1  do
		print(t)
		NPCLEvent:FireAllClients('Round starts in '..t)
		wait(1)
	end
end



wait(3)

while true do
	Startintermission()
	wait(1)
	startRound()
	wait(1)
end

It only spawns one set of npcs. Anyone know why?

Figured it out.
I never reset a certain value!

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