Ending game when too many zombies or clock runs out

Do you even have a function called “playIntermission()”? It may be because you have to call it once for the function to fully work

The loop would stop until the CurrentZombies is greater than the MaxZombieLimit, and it would break the loop continuing onward onto its thread (Or it would destroy all the spawns)

1 Like

I can see the zombies load in the Workspace. How made a folder called ZombiesFolder. How do I make them load in there?

Go to where is the ur zombies location and add them in a new folder called “Zombies_Folder” add your zombies inside that folder

I do. On ServerScriptService I have this

local lobbyLocation = game.Workspace.Lobby.Position + Vector3.new(0,3,0)
local gameLocation = game.Workspace.Main.Position + Vector3.new(0,3,0)

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local timeEvent = ReplicatedStorage:WaitForChild('TimeEvent')

local function playGame()
	local timeAmount = 10
	local timerText = 'Remaining Time: '
	while timeAmount > 0 do
		timeEvent:FireAllClients(timeAmount, timerText)
		wait(1)
		timeAmount -= 1
	end
end

local function playIntermission()
	local intermission = 5
	local timerText = 'Intermission: '
	while intermission > 0 do
		timeEvent:FireAllClients(intermission, timerText)
		wait(1)
		intermission -=1
	end
end

local function resetPlayers()
	for _, plr in pairs(game.Players:GetChildren()) do
		plr.Character.HumanoidRootPart.CFrame = CFrame.new(lobbyLocation)
	end
end


local function teleportPlayers()
	for _, plr in pairs(game.Players:GetChildren()) do
		plr.Character.HumanoidRootPart.CFrame = CFrame.new(gameLocation)
	end
end

while true do
	resetPlayers()
	playIntermission()
	teleportPlayers()
	playGame()
end

And I have this on starterGUI, screenGUI, Text label, local script

local label = script.Parent

local ReplicatedStorage = game:GetService('ReplicatedStorage')

local timeEvent = ReplicatedStorage:WaitForChild('TimeEvent')

timeEvent.OnClientEvent:Connect(function(timeAmount, timerText)

label.Text = timerText..timeAmount

end)

The zombie was added in ReplicatedStorage. I made a folder called “Zombies_Forlder” and dropped the zombie in there. It looks like this

ReplicatedStorage
Zombies_Folder
Zombie

I get this
Script:24: attempt to index nil with ‘GetChildren’

Do this

local Max_Zombies = 0 --put an amount here (If the zombies amount reach this value the game will end)

local Zombies = --location of the zombies folder only (They all must be in one folder and that folder must contain every single zombie inside it)
local plr = game.Players.LocalPlayer
wait(0.5)
for i, v in pairs(Zombies:GetChildren()) do
if  v == Max_Zombies  then
print("Every zombie is dead game ended")
        --End game.
   end
end

Only the location of the folder

I get attempt to index nil with ‘GetChildren’ -

local Max_Zombies = 0 --put an amount here (If the zombies amount reach this value the game will end)

local Zombies = ReplicatedStorage --location of the zombies folder only (They all must be in one folder and that folder must contain every single zombie inside it)
	local plr = game.Players.LocalPlayer
wait(0.5)
for i, v in pairs(Zombies:GetChildren()) do
	if  v == Max_Zombies  then
		print("Every zombie is dead game ended")
		--End game.
	end
end

I will explain everything I have done. I made a folder in Workspace (The underscores are spaces)
workspace
___Baseplate
_____Part1
_____Part2
_____Part3
___spawns
_____Script
_____Part

The script is this

local spawns = script.Parent
local spawn_time = 5
local MaxZombieLimit = 5
local CurrentZombies = 0

local TimeRemaining = 30 --Relies every 5 seconds, so 30 * 5 = 150 seconds I believe

local part1 = workspace.Part1
local part2 = workspace.Part2
local part3 = workspace.Part3


local Part1Enabled = false
local Part2Enabled = false
local Part3Enabled = false

local PartsTouched = 0


part1.Touched:Connect(function(Hit)
	if Part1Enabled == false then
		Part1Enabled = true
		PartsTouched += 1
	end
end)

part2.Touched:Connect(function(Hit)
	if Part2Enabled == false then
		Part2Enabled = true
		PartsTouched += 1
	end
end)

part3.Touched:Connect(function(Hit)
	if Part3Enabled == false then
		Part3Enabled = true
		PartsTouched += 1
	end
end)

while true do

	wait(spawn_time)
	TimeRemaining -= 1
	for _, spwn in pairs(spawns:GetChildren()) do

		if spwn:IsA("BasePart") then
			local zombieCopy = game.ReplicatedStorage['Zombie']:Clone()
			zombieCopy.Parent = game.Workspace
			zombieCopy.HumanoidRootPart.CFrame = CFrame.new(spwn.Position + Vector3.new(0,3,0))	
			CurrentZombies += 1
		end	

	end
	if CurrentZombies > MaxZombieLimit then
		playIntermission()
	end

end

spawns:Destroy()

On ReplicatedStorage I have (keep in mind the underscores are spaces)
ReplicatedStorage
__RemoteFunction called “TimeEvent”
__Zombie

on ServerscriptService this is the script

local lobbyLocation = game.Workspace.Lobby.Position + Vector3.new(0,3,0)
local gameLocation = game.Workspace.Main.Position + Vector3.new(0,3,0)

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local timeEvent = ReplicatedStorage:WaitForChild('TimeEvent')

local function playGame()
	local timeAmount = 100
	local timerText = 'Remaining Time: '
	while timeAmount > 0 do
		timeEvent:FireAllClients(timeAmount, timerText)
		wait(1)
		timeAmount -= 1
	end
end

local function playIntermission()
	local intermission = 5
	local timerText = 'Intermission: '
	while intermission > 0 do
		timeEvent:FireAllClients(intermission, timerText)
		wait(1)
		intermission -=1
	end
end

local function resetPlayers()
	for _, plr in pairs(game.Players:GetChildren()) do
		plr.Character.HumanoidRootPart.CFrame = CFrame.new(lobbyLocation)
	end
end


local function teleportPlayers()
	for _, plr in pairs(game.Players:GetChildren()) do
		plr.Character.HumanoidRootPart.CFrame = CFrame.new(gameLocation)
	end
end



while true do
	resetPlayers()
	playIntermission()
	teleportPlayers()
	playGame()
end

Then I have
StarterGui
__ScreenGui
____TextLabel
_______LocalScript

This is what is in Local Script

local label = script.Parent

local ReplicatedStorage = game:GetService('ReplicatedStorage')

local timeEvent = ReplicatedStorage:WaitForChild('TimeEvent')

timeEvent.OnClientEvent:Connect(function(timeAmount, timerText)

label.Text = timerText..timeAmount

end)

All I want is when the zombie count reaches 5 it will take you to intermission to show that the game ended. Unless there is an easier way for the game to end when the countdown reaches zero or when the zombie count reaches 5.

I also have a Game over screen that pops out when you die
StarterGui
__ScreenGui
__LocalScript

game.Players.LocalPlayer.Character:WaitForChild("Humanoid").Died:Connect(function()

script.Parent.GameOver.Visible = true

end)

How do I reference this screen when the zombie count is reached or the timer runs out?