At the end of round 10, i want to give players left the win (table)

Hey so I am making a game with 10 rounds and at the 10th round I want the players left to get a win

I have already made the table :
local players = game.Players:GetPlayers(); -- Table of players in server

I have the script that removes players from the table when they die :


local function playerAdded(player)
	local conn -- disconnect connections or fraud

	local function characterAdded(character)
		if not character.Parent then
			character.AncestryChanged:Wait()
		end

		local function onDied()
			conn:Disconnect()
			table.remove(players, table.find(players, player))
		end

		local humanoid = character:FindFirstChildWhichIsA("Humanoid")
		if humanoid then
			humanoid.Died:Connect(onDied)
		end
	end

	local character = player.Character

	if character then
		characterAdded(character)
	end

	conn = player.CharacterAdded:Connect(characterAdded)
end
local Players = game:GetService("Players")
for _, player in ipairs(Players:GetPlayers()) do
	coroutine.wrap(playerAdded)(player)
end

All I need is help on the script that I will place at the end of round 10 that will give players left in the table the WIN (leaderstats already made). Additionally if you could help me with a script that after giving the win, removes the rest of the players in the table and then my script will loop.

ROUND 10 SCRIPT IF NEEDED =


		Circles = game.Workspace.Circles:GetChildren() 

		Chosen_Colour = Circles[math.random(1,#Circles)]

		game.ReplicatedStorage.Colour.Value = Chosen_Colour.Name 

		task.wait(2)
																						-- ROUND 9,10
for number,Circle_Colours in pairs(Circles) do
	for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do

		Circle_Part.Transparency = 1 
		Circle_Part.CanCollide = false
	end
end

for number, Circle_Part in pairs(Chosen_Colour:GetChildren()) do
	Circle_Part.Transparency = 0 
	Circle_Part.CanCollide = true
end

wait(3)

for number,Circle_Colours in pairs(Circles) do 
	for number,Circle_Part in pairs(Circle_Colours:GetChildren()) do
		print("h")
		Circle_Part.Transparency = 0
		Circle_Part.CanCollide = true

		
	end
end

wait(2)
print("Round 9 and 10 completed")


local StarterBlock = game.Workspace.StarterBlock


			StarterBlock.Transparency = 0
StarterBlock.CanCollide = true





local Players = game:GetService("Players")

for _, Player in pairs(Players:GetChildren()) do
	local leaderstats = Player.leaderstats
	local Wins = leaderstats.Wins
	Wins.Value += 1
end

game.ReplicatedStorage.Colour.Value = "Game ended."


wait(3.5)

game.ReplicatedStorage.Colour.Value = " "

You can for i,v in ipairs in the table, find the players in the table and give them the win

1 Like

Hey thanks for the reply, could you maybe tell me how to

so you would want to

for i,v in ipairs(playerstable) do
local plr = game.Players:FindFirstChild(v)
-- give win logic
end
1 Like

Great! And have you got any idea on how to clear the table / remove everyone after the win logic?

yourtable = {} -- clears a table lol

It sets the table to an empty table, so basically it removes everything in that table

So I recommend you put it after the ipairs

1 Like
local PlayersService = game:GetService("Players")
local Players = PlayersService:GetPlayers()
local PlrAddConn, PlrRemoveConn, CharAddConn, CharRemoveConn, DiedConn = nil, nil, nil, nil, nil

PlrConn = PlayersService.PlayerAdded:Connect(function(Player)
	if PlrRemoveConn then
		PlrRemoveConn:Disconnect()
	end
	CharAddConn = Player.CharacterAdded:Connect(function(Character)
		if DiedConn then
			DiedConn:Disconnect()
		end
		if CharRemoveConn then
			CharRemoveConn:Disconnect()
		end
		local Humanoid = Character:WaitForChild("Humanoid")
		DiedConn = Humanoid.Died:Connect(function()
			if table.find(Players, Player) then
				table.remove(Players, table.find(Players, Player))
			end
		end)
	end)
	local CharRemoveConn = Player.CharacterRemoving:Connect(function(Character)
		if CharAddConn then
			CharAddConn:Disconnect()
		end
	end)
end)

PlrRemoveConn = PlayersService.PlayerRemoving:Connect(function(Player)
	if PlrConn then
		PlrConn:Disconnect()
	end
end)
1 Like
for _, player in ipairs(Players) do --change "Players" to name of table alive players are stored inside
	local leaderstats = player:WaitForChild("leaderstats")
	if leaderstats then
		local Wins = leaderstats:WaitForChild("Wins")
		if Wins then
			Wins.Value += 1
		end
	end
end

You likely wouldn’t need to fetch the player from game.Players/PlayersService as “v” in your implementation will already be a reference to the player object.

1 Like
local tables = {"a", "b", "c"}
local tables = {} --overrides previous table with an empty table (emptying it)
table.clear(tables) --alternatively this function from the table library will achieve the same

You can use either of these methods to empty a table (be it an array or dictionary) which you choose is entirely up to you.

1 Like

I’m not saying it must be v, he can change it anyway

1 Like

In hindsight I should’ve replied to the original poster (this is information you likely already know).

1 Like

Hey so this removes players when they die. Should this be placed at the start of the script or every round?

Does this work as my table for all the players in the server?

local Players = game.Players:GetPlayers(); -- Table of players in server

Got an error, the name of my table is “Players”

As I mentioned above,

for i,v in pairs(Players) do
local plr = game.Players:FindFirstChild(v)
-- give win logic
end
Players = {}

Try using pairs then if error persists. As ipairs dosent accept anything other than arrays

That’s because you’re attempting to pass the PlayerService itself as an argument to the ipairs iterator function as opposed to an array (which is what is required).

local Players = game:GetService("Players")
local PlayerList = Players:GetPlayers()

for _, player in ipairs(PlayerList) do --change "PlayerList" to name of table alive players are stored inside
	local leaderstats = player:WaitForChild("leaderstats")
	if leaderstats then
		local Wins = leaderstats:WaitForChild("Wins")
		if Wins then
			Wins.Value += 1
		end
	end
end

Make sure you don’t override any existing variables with the top two variables.

Does this work?

local PlayersInServer = game.Players:GetPlayers(); -- Table of players in server

Yes, that would also create an array of current players.

Alright and so for the “removing of dead players” script, I don’t want to mess the script up but where will I need to make the changes if the table of players is named “PlayersInServer”.

Additionally where will I need to place this script?

local function onDied()
	conn:Disconnect()
	if table.find(PlayersInServer, player) then
		table.remove(PlayersInServer, table.find(PlayersInServer , player))
	end
end