So a while ago I made a game with a round system, it worked nice but the rounds didn’t end when no one or 1 player was alive, I made this modification to the script in hopes of ending the game when there are 1 or less players alive, it didn’t work, and I got no errors.
Server script inside of ServerScriptService.
local replicatedStorage = game:GetService("ReplicatedStorage")
local Status = replicatedStorage:WaitForChild("Status")
local InGameBool = replicatedStorage:WaitForChild("InGame")
local MapsFolder = replicatedStorage:WaitForChild("Maps")
local Maps = MapsFolder:GetChildren()
InGameBool.Changed:Connect(function()
if InGameBool.Value == true then
-- Choose Map
local ChosenMap = Maps[math.random(1,#Maps)]
local ClonedMap = ChosenMap:Clone()
ClonedMap.Parent = game.Workspace
wait(2)
for _, player in pairs(game.Players:GetChildren()) do
local char = player.Character or player.CharacterAdded:Wait()
char.HumanoidRootPart.CFrame = CFrame.new(ClonedMap.Spawn.Position)
end
else
for _, player in pairs(game.Players:GetChildren()) do
local char = player.Character or player.CharacterAdded:Wait()
char.HumanoidRootPart.CFrame = CFrame.new(game.Workspace.Lobby.SpawnLocation.Position) -- Teleports
end
for i,v in pairs (MapsFolder:GetChildren()) do
if game.Workspace:FindFirstChild(v.Name) then
local obj = game.Workspace[v.Name]
obj:Destroy()
end
end
end
end)
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
player.Team = game.Teams.Lobby
end)
end)
local function changeTimer1()
local players = game:GetService("Players"):GetChildren()
InGameBool.Value = false
for i = 30,0,-1 do
wait(1)
Status.Value = "Intermission:"..i.." Seconds Before New Match!"
if i == 0 then
for i,v in pairs(players) do
v.Team = game.Teams.Playing
end
end
end
end
local function changeTimer2()
local players = game:GetService("Players"):GetChildren()
InGameBool.Value = true
for i = 80,0,-1 do
wait(1)
Status.Value = "In Game:"..i.." Seconds Left Before Game Ends!"
if i == 0 then
for i,v in pairs(players) do
v.Team = game.Teams.Lobby
end
end
end
end
while wait() do
changeTimer1()
local playersOnTeam = game:GetService("Teams")["Playing"]:GetPlayers()
local numberPlayersOnTeam = #playersOnTeam
if numberPlayersOnTeam ~= 0 then
changeTimer2()
elseif numberPlayersOnTeam == 0 then
changeTimer2:Disconnect()
changeTimer1()
end
end
I don’t know what I was thinking when I made those if statements to check if there are one or less players on the “Playing” team, but I fixed that and now something is happening, but the thing that is happening is an error:
Updated script:
local replicatedStorage = game:GetService("ReplicatedStorage")
local Status = replicatedStorage:WaitForChild("Status")
local InGameBool = replicatedStorage:WaitForChild("InGame")
local MapsFolder = replicatedStorage:WaitForChild("Maps")
local Maps = MapsFolder:GetChildren()
InGameBool.Changed:Connect(function()
if InGameBool.Value == true then
local ChosenMap = Maps[math.random(1,#Maps)]
local ClonedMap = ChosenMap:Clone()
ClonedMap.Parent = game.Workspace
wait(2)
for _, player in pairs(game.Players:GetChildren()) do
local char = player.Character or player.CharacterAdded:Wait()
char.HumanoidRootPart.CFrame = CFrame.new(ClonedMap.Spawn.Position)
end
else
for _, player in pairs(game.Players:GetChildren()) do
local char = player.Character or player.CharacterAdded:Wait()
char.HumanoidRootPart.CFrame = CFrame.new(game.Workspace.Lobby.SpawnLocation.Position) -
end
for i,v in pairs (MapsFolder:GetChildren()) do
if game.Workspace:FindFirstChild(v.Name) then
local obj = game.Workspace[v.Name]
obj:Destroy()
end
end
end
end)
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
player.Team = game.Teams.Lobby
end)
end)
local function changeTimer1()
local players = game:GetService("Players"):GetChildren()
InGameBool.Value = false
for i = 30,0,-1 do
wait(1)
Status.Value = "Intermission:"..i.." Seconds Before New Match!"
if i == 0 then
for i,v in pairs(players) do
v.Team = game.Teams.Playing
end
end
end
end
local function changeTimer2()
local players = game:GetService("Players"):GetChildren()
InGameBool.Value = true
for i = 80,0,-1 do
wait(1)
Status.Value = "In Game:"..i.." Seconds Left Before Game Ends!"
if i == 0 then
for i,v in pairs(players) do
v.Team = game.Teams.Lobby
end
end
end
end
while wait() do
changeTimer1()
local playersOnTeam = game:GetService("Teams")["Playing"]:GetPlayers()
local numberPlayersOnTeam = #playersOnTeam
if numberPlayersOnTeam > 1 then
print("not hi 👀👀")
changeTimer2()
elseif numberPlayersOnTeam <= 1 then
print("hi")
changeTimer2:Disconnect()
changeTimer1()
end
end
That worked, but I need to go into further testing to make sure every thing works as expected, here is the new code I made:
local replicatedStorage = game:GetService("ReplicatedStorage")
local Status = replicatedStorage:WaitForChild("Status")
local InGameBool = replicatedStorage:WaitForChild("InGame")
local MapsFolder = replicatedStorage:WaitForChild("Maps")
local Maps = MapsFolder:GetChildren()
local EndRound = false
InGameBool.Changed:Connect(function()
if InGameBool.Value == true then
local ChosenMap = Maps[math.random(1,#Maps)]
local ClonedMap = ChosenMap:Clone()
ClonedMap.Parent = game.Workspace
wait(2)
for _, player in pairs(game.Players:GetChildren()) do
local char = player.Character or player.CharacterAdded:Wait()
char.HumanoidRootPart.CFrame = CFrame.new(ClonedMap.Spawn.Position)
end
else
for _, player in pairs(game.Players:GetChildren()) do
local char = player.Character or player.CharacterAdded:Wait()
char.HumanoidRootPart.CFrame = CFrame.new(game.Workspace.Lobby.SpawnLocation.Position)
end
for i,v in pairs (MapsFolder:GetChildren()) do
if game.Workspace:FindFirstChild(v.Name) then
local obj = game.Workspace[v.Name]
obj:Destroy()
end
end
end
end)
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
player.Team = game.Teams.Lobby
end)
end)
local function changeTimer1()
local players = game:GetService("Players"):GetChildren()
InGameBool.Value = false
for i = 10,0,-1 do
wait(1)
Status.Value = "Intermission:"..i.." Seconds Before New Match!"
if i == 0 then
for i,v in pairs(players) do
v.Team = game.Teams.Playing
end
end
end
end
local function changeTimer2()
local players = game:GetService("Players"):GetChildren()
InGameBool.Value = true
for i = 15,0,-1 do
wait(1)
Status.Value = "In Game:"..i.." Seconds Left Before Game Ends!"
if i == 0 then
for i,v in pairs(players) do
v.Team = game.Teams.Lobby
end
end
if EndRound == true then
break
end
end
end
while wait() do
changeTimer1()
local playersOnTeam = game:GetService("Teams")["Playing"]:GetPlayers()
local numberPlayersOnTeam = #playersOnTeam
if numberPlayersOnTeam > 1 then
print("not hi 👀👀")
changeTimer2()
elseif numberPlayersOnTeam <= 1 then
print("hi")
EndRound = true
changeTimer1()
end
end
If you find anything that is a bad practice or is an error in this script please tell me.
So after a bit of testing I figured out that it didn’t work, the current problem is that when the round starts, the script can’t find the “Spawn” part of the selected map, so it returns an error. The original script never had this issue, which is weird because I didn’t change the script around the section that teleports the player to the map.
Updated script:
local replicatedStorage = game:GetService("ReplicatedStorage")
local Status = replicatedStorage:WaitForChild("Status")
local InGameBool = replicatedStorage:WaitForChild("InGame")
local MapsFolder = replicatedStorage:WaitForChild("Maps")
local Maps = MapsFolder:GetChildren()
local EndRound = false
InGameBool.Changed:Connect(function()
if InGameBool.Value == true then
local ChosenMap = Maps[math.random(1,#Maps)]
local ClonedMap = ChosenMap:Clone()
ClonedMap.Parent = game.Workspace
wait(2)
for _, player in pairs(game.Players:GetChildren()) do
local char = player.Character or player.CharacterAdded:Wait()
char.HumanoidRootPart.CFrame = CFrame.new(ClonedMap.Spawn.Position) --This is the line with the error.
end
else
for _, player in pairs(game.Players:GetChildren()) do
local char = player.Character or player.CharacterAdded:Wait()
char.HumanoidRootPart.CFrame = CFrame.new(game.Workspace.Lobby.SpawnLocation.Position)
end
for i,v in pairs (MapsFolder:GetChildren()) do
if game.Workspace:FindFirstChild(v.Name) then
local obj = game.Workspace[v.Name]
obj:Destroy()
end
end
end
end)
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
player.Team = game.Teams.Lobby
end)
end)
local function changeTimer1()
local players = game:GetService("Players"):GetChildren()
InGameBool.Value = false
for i = 10,0,-1 do
wait(1)
Status.Value = "Intermission:"..i.." Seconds Before New Match!"
if i == 0 then
for i,v in pairs(players) do
v.Team = game.Teams.Playing
end
end
end
end
local function changeTimer2()
local players = game:GetService("Players"):GetChildren()
InGameBool.Value = true
for i = 15,0,-1 do
wait(1)
Status.Value = "In Game:"..i.." Seconds Left Before Game Ends!"
if i == 0 then
for i,v in pairs(players) do
v.Team = game.Teams.Lobby
end
end
if EndRound == true then
break
end
end
end
while wait() do
changeTimer1()
local playersOnTeamiPlayingi = game:GetService("Teams")["Playing"]:GetPlayers()
local playersOnTeamiLobbyi = game:GetService("Teams")["Lobby"]:GetPlayers()
local numberPlayersOnTeam = #playersOnTeamiPlayingi
if playersOnTeamiPlayingi > 1 then
print("not hi 👀👀")
changeTimer2()
elseif playersOnTeamiPlayingi <= 1 then
print("hi")
EndRound = true
changeTimer1()
end
end
EDIT: I will be going to bed right now, so if I don’t respond for the next 12ish hours, that’s why.