Round system error

local roundtext = game.ReplicatedStorage.Values.StatusText
local roundnumber = game.ReplicatedStorage.Values.StatusNumber
local multimaps = game.ServerStorage.MultiMaps:GetChildren()
local singlemaps = game.ServerStorage.SingleMaps:GetChildren()

playing = {}

function checkPlayers(playing)
	for i, v in pairs(playing) do
		if game.Workspace:FindFirstChild(v) == nil then
			table.remove(playing, i)
			print(playing)
		else
			game.Workspace:WaitForChild(v).Humanoid.Died:Connect(function()
				table.remove(playing, i)
				print(playing)
			end)
		end
	end
end


while wait() do

	if #game.Players:GetPlayers() >= 2 then
		local countdown  = 30
		for i = countdown, 0, -1 do
			wait(1)
			roundtext.Value = "Intermission"
			roundnumber.Value = i
		end

		local chosen = multimaps[math.random(1, #multimaps)]:Clone()
		Spawns = chosen:FindFirstChild('Spawns'):GetChildren()
		chosen.Parent = workspace

		local countdown = 3
		for i = countdown, 0, -1 do
			wait(1)
			roundtext.Value = chosen.Name.." has been chosen!"
			roundnumber.Value = i
		end

		for _, Player in ipairs(game.Players:GetPlayers())do
			if Player.Character and Player.Character:WaitForChild('Humanoid') then
				RandomSpawn = Spawns[math.random(1, #Spawns)]
				Player.Character.HumanoidRootPart.CFrame = RandomSpawn.CFrame
			end
		end
		
		playing = {}
		
		for i, v in pairs(game.Players:GetPlayers()) do
			table.insert(playing, v.Name)
			print(playing)
		end

		countdown = 5
		for i = countdown, 1, -1 do
			wait(1)
			roundtext.Value = "Be the last one standing!"
			roundnumber.Value = i
			checkPlayers(playing)
		end

		local countdown = 90
		for i = countdown, 0, -1 do
			wait(1)
			roundtext.Value = chosen.Name
			roundnumber.Value = i

			checkPlayers(playing)

			if #playing == 1 then
				chosen:Destroy()
				for i, v in pairs(playing) do
					roundtext.Value = "Winners: "..table.concat(playing, ', ')
					game.Players:WaitForChild(v).leaderstats.Minicoins.Value += 1
					local gamepass = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(game.Players:WaitForChild(v).UserId, 13839244)

					if gamepass then
						game.Players:WaitForChild(v).leaderstats.Minicoins.Value += 1
					end
				end
				wait(3)
			end
			
			if #playing == 0 then
				chosen:Destroy()
				for i, v in pairs(playing) do
					roundtext.Value = "Winners: Nobody!"
				end
				wait(1)
				break
			end
			
		end

		chosen:Destroy()
		for i, v in pairs(playing) do
			roundtext.Value = "Winners: "..table.concat(playing, ', ')
			game.Players:WaitForChild(v).leaderstats.Minicoins.Value += 1
			local gamepass = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(game.Players:WaitForChild(v).UserId, 13839244)

			if gamepass then
				game.Players:WaitForChild(v).leaderstats.Minicoins.Value += 1
			end
		end
		wait(3)

	else ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
		local countdown  = 30
		for i = countdown, 0, -1 do
			wait(1)
			roundtext.Value = "Intermission"
			roundnumber.Value = i
			checkPlayers(playing)
			if #game.Players:GetPlayers() > 1 then
				break
			end
		end
		
		if #game.Players:GetPlayers() == 1 then
			playing = {}

			local chosen = singlemaps[math.random(1, #singlemaps)]:Clone()
			Spawns = chosen:FindFirstChild('Spawns'):GetChildren()
			chosen.Parent = workspace

			local countdown = 3
			for i = countdown, 0, -1 do
				roundtext.Value = chosen.Name.." has been chosen!"
				roundnumber.Value = i
				wait(1)
				checkPlayers(playing)
			end

			for _, Player in ipairs(game.Players:GetPlayers())do
				if Player.Character and Player.Character:WaitForChild('Humanoid') then
					RandomSpawn = Spawns[math.random(1, #Spawns)]
					Player.Character.HumanoidRootPart.CFrame = RandomSpawn.CFrame
				end
			end

			for i, v in pairs(game.Players:GetPlayers()) do
				table.insert(playing, v.Name)
			end

			countdown = 5
			for i = countdown, 0, -1 do
				checkPlayers(playing)
				roundtext.Value = "Survive for 60 seconds!"
				roundnumber.Value = i
				wait(1)
			end

			local countdown = 60
			for i = countdown, 0, -1 do
				wait(1)
				roundtext.Value = chosen.Name
				roundnumber.Value = i

				checkPlayers(playing)

				if #playing == 0 then
					chosen:Destroy()
					roundtext.Value = "Winners: Nobody!"
					wait(1)
					break
				end
			end
			
			chosen:Destroy()
			for i, v in pairs(playing) do
				roundtext.Value = "Winners: "..table.concat(playing, ', ')
				game.Players:WaitForChild(v).leaderstats.Minicoins.Value += 1
				local gamepass = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(game.Players:WaitForChild(v).UserId, 13839244)
				
				if gamepass then
					game.Players:WaitForChild(v).leaderstats.Minicoins.Value += 1
				end
			end
			wait(3)
		end
	end
end

Issues im having:

when one or two players die, it will sometimes end the match even when there are more than one players present
only the first player to join the server will be able to win; if someone else wins, it will just end the round without assigning a winner

i would really appreciate any help i get because ive been at this issue since november 2020

Could you describe your issue in more detail, I don’t think it’s very easy for others to understand what you’re trying to work through without swimming deep into your code.

From what I can see, you have a table that has the players that are playing the game mode. That gets updated with the players every so often.

I can’t really see an issue with your code right now.

1 Like

basically, when the script checks for a player’s death in the function, sometimes it ends the game before there’s one player left
and im assuming its removing others from the table or just straight up not adding them into the table
my other issue is that the script only allows the first player to join a server to win a round

Make sure your number of players is updating every time another player joins and if players disconnect.

This is going to be a memory leak/build up if you leave it like this. Every time you call it and the player exists, there will be a new roblox connection added which means it’s going to delete twice, thrice, and so on. You only need to do this once, so do it when you add players. That may be your issue.

It’s going to remove the index, but that means if the index has already been removed, then a player that is still in the game risks being removed as well. You need to search for the player’s name in the table, and then delete the index. That way it will take into account any already deleted players.

1 Like

i’m still confused
what should i do instead?
that checking for nil thing was to make sure they get removed when the player leaves
if i dont have that part there, the game will break because it cant find anything with FindFirstChild()

What I’d suggest is to rewrite the script. There are too many things wrong with it. For example, it doesn’t check if the player count is over 1 after the Intermission and other stuff.

1 Like

I really recommend rewriting the script and using a module instead. This will keep it organised and can easily be required by a script. :ok_hand:

1 Like