The problem is that when i start a server with 2 players it will countdown start the round but says everyone died when no one has died yet and ends the round because of that
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Teams = game:GetService("Teams")
local Manager = require(ServerScriptService.PlayerData.Manager)
local RoundSystem = ReplicatedStorage.RoundSystem
local InRound = RoundSystem.InRound
local Status = RoundSystem.Status
local PlayingTeam = Teams.Playing
local SpectatorsTeam = Teams.Spectators
local IntermissionTime = 10
local RoundTime = 15
InRound.Changed:Connect(function()
if InRound.Value == true then
for i, player in pairs(Players:GetPlayers()) do
local character = player.Character or player.CharacterAdded:Wait()
local hrp = character.HumanoidRootPart
hrp.CFrame = workspace.TestSpawn.CFrame
player.Team = PlayingTeam
character.Humanoid.Died:Connect(function()
player.Team = SpectatorsTeam
end)
end
else
for i, player in pairs(Players:GetPlayers()) do
local character = player.Character or player.CharacterAdded:Wait()
local hrp = character.HumanoidRootPart
hrp.CFrame = workspace.Lobby.Spawns.SpawnLocation.CFrame
player.Team = SpectatorsTeam
end
end
end)
local function Round()
while true do
local requiredPlayers = 2
repeat
task.wait(1)
Status.Value = "Atleast "..requiredPlayers.." players needed to start a round"
until #Players:GetPlayers() >= requiredPlayers
InRound.Value = false
for i = IntermissionTime, 0, -1 do
Status.Value = "Game will start in "..i.." seconds"
task.wait(1)
end
InRound.Value = true
for i = RoundTime, 0, -1 do
Status.Value = "Game will end in "..i.." seconds"
local playing = {}
for i, player in pairs(Players:GetPlayers()) do
if player.Team == PlayingTeam.Name then
table.insert(playing, player.Name)
end
end
if #playing == 0 then
Status.Value = "Everyone has died D:"
task.wait(3)
break
end
if #playing == 1 then
Status.Value = playing[1].." has won!"
for i, player in pairs(Players:GetPlayers()) do
if playing[1] == player.Name then
local profile = Manager.Profiles[player]
if not profile then return end
profile.Data.Wins += 1
player.leaderstats.Wins.Value = profile.Data.Wins
Manager.adjustCoins(player, 10)
end
end
task.wait(3)
break
end
task.wait(1)
end
task.wait(3)
end
end
task.spawn(Round)
What prints when you add these two print statements?
for i, player in pairs(Players:GetPlayers()) do
print(player, player.Team)
if player.Team == PlayingTeam.Name then
table.insert(playing, player.Name)
print(player, "Inserted into Playing Table")
end
end
You are saying to insert players only if they are on the Teams.Playing with this line:
if player.Team == PlayingTeam.Name then...
Earlier you are trying to assign players to Teams.Playing by changing a BoolValue:
InRound.Value = true
My guess is that the BoolValue is not picked up fast enough so the script executes before the InRound.Changed:Connect(function() has a chance to change their team.
You could try replacing InRound.Value = true with a call to the function instead.
You would need to change the InRound.Change to be a named function like so:
local function InRound()
if InRound.Value == true then
for i, player in pairs(Players:GetPlayers()) do
local character = player.Character or player.CharacterAdded:Wait()
local hrp = character.HumanoidRootPart
hrp.CFrame = workspace.TestSpawn.CFrame
player.Team = PlayingTeam
character.Humanoid.Died:Connect(function()
player.Team = SpectatorsTeam
end)
end
else
for i, player in pairs(Players:GetPlayers()) do
local character = player.Character or player.CharacterAdded:Wait()
local hrp = character.HumanoidRootPart
hrp.CFrame = workspace.Lobby.Spawns.SpawnLocation.CFrame
player.Team = SpectatorsTeam
end
end
end
Now you can call it from within your Round() function instead of relying on a BoolValue.
local function Round()
while true do
local requiredPlayers = 2
repeat
task.wait(1)
Status.Value = "Atleast "..requiredPlayers.." players needed to start a round"
until #Players:GetPlayers() >= requiredPlayers
--InRound.Value = false
for i = IntermissionTime, 0, -1 do
Status.Value = "Game will start in "..i.." seconds"
task.wait(1)
end
--InRound.Value = true
InRound()
for i = RoundTime, 0, -1 do
Status.Value = "Game will end in "..i.." seconds"
local playing = {}
for i, player in pairs(Players:GetPlayers()) do
if player.Team == PlayingTeam.Name then
table.insert(playing, player.Name)
end
end
if #playing == 0 then
Status.Value = "Everyone has died D:"
task.wait(3)
break
end
if #playing == 1 then
Status.Value = playing[1].." has won!"
for i, player in pairs(Players:GetPlayers()) do
if playing[1] == player.Name then
local profile = Manager.Profiles[player]
if not profile then return end
profile.Data.Wins += 1
player.leaderstats.Wins.Value = profile.Data.Wins
Manager.adjustCoins(player, 10)
end
end
task.wait(3)
break
end
task.wait(1)
end
task.wait(3)
end
end
Try adding a 2-3 second wait() in front of the RoundTime for loop:
for i = RoundTime, 0, -1 do
Status.Value = "Game will end in "..i.." seconds"
local playing = {}
Just like @mc7oof said, this looks like a data synchronization issue. The server can hypothetically run the PlayingTeam loop before assigning players to the playing team. Also, I am pretty sure the said loop needs to be unnested since, logically speaking, it only needs to run once per round:
for i = IntermissionTime, 0, -1 do
Status.Value = "Game will start in "..i.." seconds"
task.wait(1)
end
InRound.Value = true
wait(2)
local playing = {}
for i, player in pairs(Players:GetPlayers()) do
if player.Team == PlayingTeam then
table.insert(playing, player.Name)
print(player, " Inserted into Playing Table")
end
end
for i = RoundTime, 0, -1 do
Status.Value = "Game will end in "..i.." seconds"