Hi I’m making a wave based zombie game and I’m having trouble with my script. I’m trying to make it so when all players die or the time runs out the round restarts. However if the players kill all the zombies in time they will move into the next wave here is what I have:
Script:
local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")
local gv = RS.Values.gv
local DiedValue = RS.Values.DV
local wave = RS.Values.Wave
local ZombiesRemaining = RS.Values.ZombiesRemaning
local TimeToWait = RS.Values.TimeToWait.Value
local IGT = RS.Values.InGameTime
local Restart = RS.Event.Restart
local KillAllZombies = RS.Values.KillAllZombies
local GIP1 = RS.Values.gameInProgress1
local DeathEvent = RS.Event.DeathEvent
local NoSpawner = RS.Event.ZombieSpawning
local YesSpawner = RS.Event.SpawnZombies
Restart:Fire()
local playersInRound = {}
local playersDisconnected = {}
local playerDies = {}
local lobbyTime = 20
local isOverrided = false
--local inRound = false
local inRound = RS.Event.inRound
local value = RS.Values.MainGame
Players.PlayerRemoving:Connect(function(Player)
table.insert(playersDisconnected, Player)
if table.find(playersInRound, Player) then
table.remove(playersInRound, table.find(playersInRound, Player))
if #playersInRound == 0 then
print("Everyone has died!")
--inRound = false
inRound:Fire()
end
end
end)
local function doRound()
--inRound = true
playersInRound = {}
playersDisconnected = {}
for _, Player in ipairs(Players:GetPlayers()) do
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local HRP = Character:WaitForChild("HumanoidRootPart")
--HRP.CFrame = CFrame.new(0, 0, 0) --good spawn location
if GIP1.Value == 1 then
table.insert(playersInRound, Player)
Humanoid.Died:Connect(function()
table.insert(playersDisconnected, Player.Name)
table.remove(playersInRound, table.find(playersInRound, Player))
if #playersInRound == 0 then
print("Everyone has died!")
--inRound = false
inRound:Fire()
end
end)
end
end
end
function Loop()
wave.Value = 0
for i = lobbyTime, 0, -1 do
gv.Value = i
wait(1)
end
GIP1.Value = 1
--inRound = true
doRound()
local function MCoroutine()
while true do
task.wait(1)
value.Value -= 1
IGT.Value = value.Value
print("Timer:", value.Value)
if value.Value <= 0 then
Restart:Fire()
DiedValue.Value = "Game Over"
DeathEvent:Fire()
NoSpawner:Fire()
print("Time Ran Out")
task.wait(3)
break
end
end
end
local MyCoroutine = coroutine.create(MCoroutine)
coroutine.resume(MyCoroutine)
local function ChangingWave()
wave.Value = wave.Value + 1
YesSpawner:Fire()
value.Value = 30
ZombiesRemaining.Value = 10 + wave.Value
if TimeToWait ~= 0.5 then -- If it hasn't reached 0.5 yet
TimeToWait = TimeToWait - 0.1 -- Decrease the current TimeToWait by 0.1
print("Time For zombies to Spawn: "..TimeToWait)
else
TimeToWait = 0.5
end
end
local function waitForConditions()
while true do
task.wait(1)
if ZombiesRemaining.Value <= 0 then
ChangingWave() -- Used to have a break and if statement
end
end
end
local function EveryoneDied()
while true do
task.wait(1)
--if inRound == false then
inRound.Event:Connect(function()
Restart:Fire()
DiedValue.Value = "Game Over"
DeathEvent:Fire()
NoSpawner:Fire()
task.wait(3)
print("InRound = false, For some reason fix it")
return true
end)
end
end
local function CallAllFunctions()
waitForConditions()
EveryoneDied()
end
CallAllFunctions()
end
Restart.Event:Connect(function()
Loop()
end)
task.spawn(function()
while task.wait() do
if not inRound then
doRound()
end
end
end)