I’m currently working on a round based gamewith a main script that chooses a random map and teleports players to it, that all work fine but im having issues with the part of the script that is supposed to stop the game if there are 0 players but it currently already stops the game when one player resets. I hope that someone can help me! Here is the round script:
local lobby = game.Workspace.Lobby
local Maps = game.ReplicatedStorage.Maps:GetChildren()
local Status = game.ReplicatedStorage.Status
local Players = game:GetService("Players")
local Playing = {}
--Checks how many players
local function enoughPlayers()
return #Players:GetPlayers() >= 2
end
--Main round loop
while true do
for i = 10, 0, -1 do
Status.Value = "Intermission: "..i
task.wait(1)
end
--Chooses random map
local chosenMap = Maps[math.random(1, #Maps)]
local clonedMap = chosenMap:Clone()
clonedMap.Parent = game.Workspace
Status.Value = "Map: "..clonedMap.MapName.Value
task.wait(3)
--Teleport players to map
for i, Player in pairs(game.Players:GetPlayers()) do
local character = Player.Character
table.insert(Playing, Player)
print(Playing)
if character then
character:MoveTo(clonedMap.TeleportPoint.Position)
end
end
--Game countdown
for i = 30, 0, -1 do
Status.Value = "Game: "..i
for i, Player in pairs(game.Players:GetPlayers()) do
local character = Player.Character
if character.Humanoid.Health <= 0 then
local Index = table.find(Playing, Player)
table.remove(Playing, Index)
print(Playing)
end
end
--If no more players end the game
if #Playing == 0 then
Status.Value = "Game over!"
task.wait(3)
break
end
task.wait(1)
end
--Teleports players to lobby
for i, Player in pairs(game.Players:GetPlayers()) do
local character = Player.Character
if character then
character:MoveTo(lobby.TeleportPoint.Position)
end
end
-- destroys map and cycle repeats
clonedMap:Destroy()
end
You’re currently only checking to see how many players are within the Playing array (which means the players that are playing the current round), but you aren’t checking to see how many players are still joined to the “main” game (the actual game which exists outside of the rounds)
I’ve just realised that you were, in fact, breaking just the for loop rather than the core loop itself… The way I had the script zoomed in made it seem as though the if statement was running within the scope of the core loop
I’m glad that BonesIsUseless was able to find a solution to your problem
Essentially, you shouldn’t break the core loop after all players have died within the round (as a matter of fact, the core loop shouldn’t be broken unless the server is empty). This is something best done using coroutines, I’ll write an example for you
@BonesIsUseless The core loop should only break once the server is empty, not because the round has ended
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")
local lobby = Workspace:WaitForChild("Lobby")
local maps = ReplicatedStorage:WaitForChild("Maps"):GetChildren()
local status = ReplicatedStorage:WaitForChild("Status")
local connections = {}
local numberOfPlayers = 0
local thread = nil
local function disconnectConnections()
for _, connection in connections do
connection:Disconnect()
end
connections = {}
end
local function coreLoop()
while true do
for i = 10, 1, -1 do
status.Value = "Intermission: "..i
task.wait(1)
end
local playersInRound = {}
local randomMap = maps[math.random(1, #maps)]:Clone()
randomMap.Parent = Workspace
status.Value = "Map: "..randomMap.MapName.Value
task.wait(3)
for _, player in Players:GetPlayers() do
table.insert(playersInRound, player)
if player.Character then
player.Character:MoveTo(randomMap.TeleportPoint.Position)
local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
if humanoid then
table.insert(connections, humanoid.Died:Connect(function()
table.remove(playersInRound, player)
end))
else
table.remove(playersInRound, player)
end
end
end
for i = 30, 1, -1 do
status.Value = "Game: "..i
task.wait(1)
if #playersInRound == 0 then break end -- All players have died, so exit out of the for loop
end
for _, player in Players:GetPlayers() do
if player.Character then
player.Character:MoveTo(lobby.TeleportPoint.Position)
end
end
task.spawn(disconnectConnections)
randomMap:Destroy()
end
end
local function onPlayerAdded(player: Player)
numberOfPlayers += 1
if numberOfPlayers >= 2 and thread == nil then
thread = task.spawn(coreLoop)
end
end
local function onPlayerRemoving(player: Player)
numberOfPlayers -= 1
if numberOfPlayers == 0 and thread then
task.cancel(thread)
thread = nil
disconnectConnections()
end
end
Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)