Making a random team script if theirs enough players

Trying to make a script which will put 1 player into a team randomly if theirs enough players in the game,

this is the current script im using:
image

But it comes back with this error:


(highlighted grey line is where the error is in the script)

Any help appreciated

u get players twice

local Players = game:GetService("Players")

remove the :GetPlayers() at line 61

1 Like

the variable Players is a table. In line 65 you then try to call the :GetPlayers() method on a table which does not have it.

instead try

local Players = game:GetService("Players")

while true do
    ---checks if there are more than 4 players
    if #Players:GetPlayers() >= 4 then
1 Like

That works in solving that error but now theirs something new;
image

Line 67:
image

1 Like
local chosenKnight = Players[math.random(1,#Players:GetPlayers())]

Changes the error to this;
image
(I changed the min number of players to 2 as testing with 3)
but if I keep it at 4 players while testing it with 3 players it freezes for a few seconds and comes back with this;


Perhaps you should consider that your while true do loop never yields.

I tried it without the minimum level player count and it comes back with the first error above,

Here’s an example script.

Thanks but for my game their is only supposed to be 1 player on the extra team while the rest are part of the normal team

In that case pick one random player to be on that team and distribute the other players to the other team.

If anyone stumbles across this with a similar issue this is how I fixed it:

I see what you were trying to achieve now, you wanted players to be distributed across three teams, two of which are solo teams (restricted to one player).

local players = game:GetService("Players")
local teams = game:GetService("Teams")
local team1 = teams.Team1 --Solo team.
local team2 = teams.Team2 --Solo team.
local team3 = teams.Team3 -- Default team.

local function startRound() --Example start round function.
	for _, player in ipairs(players:GetPlayers()) do
		player.Team = team3
	end
	
	local randomPlayer1 = team3:GetPlayers()[math.random(#team3:GetPlayers())]
	randomPlayer1.Team = team1
	
	local randomPlayer2  = team3:GetPlayers()[math.random(#team3:GetPlayers())]
	randomPlayer2.Team = team2
end