Hi everyone! I’m having trouble finding a way to have it so when all the players except 1 leaves or dies and they were in the round then the game would end. I appreciate any help, thank you.
Here’s my script:
--Change only the value, NOT the name of the variable:
local IntermissionTime = 10 --Intermission time in seconds
local GameTime = 45 --Game time in seconds
local playersneeded = 2 --Players needed to start a round
local lobbySpawn = game.Workspace.Lobby.spawn
local maps = game.ReplicatedStorage.RoundSystem.Maps
local status = game.ReplicatedStorage.RoundSystem.status
local gameon = game.ReplicatedStorage.RoundSystem.Gameon
while true do
wait(0.00001)
if gameon.Value == false then
if #game.Players:GetPlayers() < playersneeded then
status.Value = "Waiting for more players..."
else
for i = IntermissionTime,0,-1 do
print(i)
status.Value = "Intermission: "..i
wait(1)
end
local rand = math.random(1,#maps:GetChildren())
local map = maps["Map"..rand]
local clone = map:Clone()
clone.Parent = game.Workspace
status.Value = "Spread out! The game will start soon..."
for i,v in pairs(game.Players:GetChildren()) do
local char = v.Character
char.HumanoidRootPart.CFrame = clone.spawn.CFrame
local ff = Instance.new("ForceField")
ff.Parent = char
v.Ingame.Value = true
for pos,tool in pairs(game.ReplicatedStorage.RoundSystem.Tools["Map"..rand.."Tools"]:GetChildren()) do
local toolclone = tool:Clone()
toolclone.Parent = v.Backpack
end
end
wait(3)
game.ReplicatedStorage.RoundSystem.Gameon.Value = true
status.Value = "Go!"
for i,v in pairs(game.Players:GetChildren()) do
local char = v.Character
char.ForceField:Destroy()
end
wait(1)
for i = GameTime,0,-1 do
status.Value = "Game: "..i
wait(1)
end
end
end
end
I fixed some potential errors and cleaned up your code a little bit. That code you have for setting up a map, if you are using this in many scripts, I would recommend using a module script.
local _players = game:GetService("Players")
local _replicatedStorage = game:GetService("ReplicatedStorage")
local _roundSystemFolder = _replicatedStorage:WaitForChild("RoundSystem")
local _rsMaps = _roundSystemFolder:WaitForChild("Maps")
local _rsGameOn = _roundSystemFolder:WaitForChild("Gameon")
local _rsstatus = _roundSystemFolder:WaitForChild("status")
local _gameConfigs = {
["intermissionTime"] = 10,
["gameTime"] = 45,
["playersNeeded"] = 2,
}
function _updateStatus(msg)
_rsstatus.Value = msg
end
_players.PlayerRemoving:Connect(function(player)
if (_rsGameOn.Value == false) then
if (#_players:GetPlayers() < _gameConfigs["playersNeeded"]) then
_updateStatus("Waiting for more players...")
elseif (#_players:GetPlayers() >= _gameConfigs["playersNeeded"]) then
for countDown = _gameConfigs["intermissionTime"], 0, -1 do
_updateStatus("Intermission: " .. countDown)
task.wait(1)
end
local mapId = math.random(1, #_rsMaps:GetChildren())
local map = _rsMaps["Map" .. mapId]
local clonedMap = map:Clone()
clonedMap.Parent = game.Workspace -- Should probably have a folder
_updateStatus("Spread out! The game will start soon...")
for _, plr in pairs(game.Players:GetChildren()) do
local playerCharacter = plr.Character
playerCharacter.HumanoidRootPart.CFrame = clonedMap.spawn.CFrame
local ff = Instance.new("ForceField")
ff.Parent = playerCharacter
plr.Ingame.Value = true
for pos, tool in pairs(game.ReplicatedStorage.RoundSystem.Tools["Map" .. mapId .."Tools"]:GetChildren()) do
local toolclone = tool:Clone()
toolclone.Parent = plr.Backpack
end
task.delay(3.1, function()
ff:Destroy()
end)
end
task.wait(3)
_rsGameOn.Value = true
_updateStatus("Go!")
task.wait(1)
for countDown = _gameConfigs["gameTime"], 0, -1 do
_updateStatus("Game: " .. countDown)
task.wait(1)
end
end
end
end)
I would take a second to figure out how this all works, if you have any questions, I’m open to answer them. Also, if you are having any issues with getting players who are still in the game, I would recommend putting all the players into a table at the start of a game and removing them when they get out, this way you can get all the players still actively in the game.
1 Like
Hi! Thanks so much for your answer, but I think you may have made a mistake. The script won’t do anything unless a player leaves. I’ll try to fix it myself, but help would be amazing. Thanks, again.
My bad, I thought you were trying to make script that will end the game and start it again after the game already started. This should work:
local _players = game:GetService("Players")
local _replicatedStorage = game:GetService("ReplicatedStorage")
local _roundSystemFolder = _replicatedStorage:WaitForChild("RoundSystem")
local _rsMaps = _roundSystemFolder:WaitForChild("Maps")
local _rsGameOn = _roundSystemFolder:WaitForChild("Gameon")
local _rsstatus = _roundSystemFolder:WaitForChild("status")
local _gameConfigs = {
["intermissionTime"] = 10,
["gameTime"] = 45,
["playersNeeded"] = 2,
}
function _updateStatus(msg)
_rsstatus.Value = msg
end
function _startRound()
for countDown = _gameConfigs["intermissionTime"], 0, -1 do
_updateStatus("Intermission: " .. countDown)
task.wait(1)
end
local mapId = math.random(1, #_rsMaps:GetChildren())
local map = _rsMaps["Map" .. mapId]
local clonedMap = map:Clone()
clonedMap.Parent = game.Workspace -- Should probably have a folder
_updateStatus("Spread out! The game will start soon...")
for _, plr in pairs(game.Players:GetChildren()) do
local playerCharacter = plr.Character
playerCharacter.HumanoidRootPart.CFrame = clonedMap.spawn.CFrame
local ff = Instance.new("ForceField")
ff.Parent = playerCharacter
plr.Ingame.Value = true
for pos, tool in pairs(game.ReplicatedStorage.RoundSystem.Tools["Map" .. mapId .."Tools"]:GetChildren()) do
local toolclone = tool:Clone()
toolclone.Parent = plr.Backpack
end
task.delay(3.1, function()
ff:Destroy()
end)
end
task.wait(3)
_rsGameOn.Value = true
_updateStatus("Go!")
task.wait(1)
for countDown = _gameConfigs["gameTime"], 0, -1 do
_updateStatus("Game: " .. countDown)
task.wait(1)
end
end
function _runGame()
_startRound()
_players.PlayerRemoving:Connect(function(player)
if (_rsGameOn.Value == false) then
if (#_players:GetPlayers() < _gameConfigs["playersNeeded"]) then
_updateStatus("Waiting for more players...")
elseif (#_players:GetPlayers() >= _gameConfigs["playersNeeded"]) then
_startRound()
end
end
end)
end
_runGame()
2 Likes
Works perfect! Thanks so much for your help! 
1 Like