Need help with round system

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)
2 Likes

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
1 Like

image

i noticed it doesnt print player inserted into playing table why is this?

1 Like

Because they are still on Teams.Spectators.

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
1 Like

No errors but now its saying Everyone died without starting the round at all

i think its failing to get the amount of players but i dont know how else to do it

1 Like

Try removing .Name from the if player.Team == PlayingTeam.Name then:

for i, player in pairs(Players:GetPlayers()) do
	if player.Team == PlayingTeam then -- remove .Name
		table.insert(playing, player.Name)
	end
end

Round System.rbxm (12.1 KB)

here is the file it still isnt working i would appreciate if anyone takes time to try and see why its broken

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"
1 Like

Thank you so much it was the adding a wait

@Ok_HandLol oh i forgot i thought i did it already lol

No problem. Can you also mark my reply as a solution or at least give it a like. I would really appreciate that, thanks :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.