[Beginner Scripting] Creating a Table of players

I am very new to scripting and have made a countdown, when this countdown reaches zero I want to have a list of the current players in the game and if their index number is even they are placed on Red Team and if it’s odd they are placed on Blue Team.
Currently I really have no idea how to do this, I have tried getting all the players and doing some weird function but that never seemed to work.

local InGame = game.Workspace.MainSystem.InGame.Value
local status = game.workspace.MainSystem.GuiStat

while true do
	while InGame == false and status.Value > 0 do
		wait(1)
		status.Value = status.Value - 1
		end
		if status.Value == 0 then
			InGame = true
			status.Value = 10
			
		end
		while InGame == true and status.Value > 0 do
		wait(1)
		status.Value = status.Value - 1
		end
		end

Here is the script for the countdown and when I turn ‘InGame’ to True I want to sort the teams. Thanks for your help!
I am new and if you need more information please let me know!

1 Like

No need to worry. I’m certain that this is what you’re looking for:

local players = game.Players:GetPlayers() --returns a table of all the players in-game

local function teamUp()

    for i, v in pairs(players) do --loop through table

        if i % 2 == 0 then --if even number

            --red team

        else 

            --blue team

        end

    end

end

You can also use ipairs as @XxELECTROFUSIONxX suggested since this array is not a dictionary (key-value pairs), but rather a table (index-value pairs).

Hope that helps. DM/PM me if you have any questions!

4 Likes

To get all the players in a table you would do

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

Thanks! I was really close to what I had previously.

1 Like

Didn’t know you could do it like that. I used to do

if i/2 == math.floor(i/2) then
1 Like

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