Code to choose a random player from a team if the team has more than 1 player isnt working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? To have the game select a random player from a team that is converted into an array of players

  2. What is the issue? My code is not choosing a player, with no error in the output

  3. What solutions have you tried so far? I have searched the DevForum for relevant posts that have similar ideas but none have provided a clear response to my issue. I have also looked at some youtube tutorials.

Here is my code that is not working:

--GLOBAL VARIABLES
local indHasPresident = false
local demHasPresident = false
local repHasPresident = false
local plrTableForInd
local plrTableForDem
local plrTableForRep

plrTableForInd = game.Teams.Independant:GetPlayers()
if #plrTableForInd > 0 and indHasPresident == false then
	local indPresident = plrTableForInd[math.random(1, #plrTableForInd)]
	print(indPresident)
end
3 Likes

You can use a for parameter to go through every player, and check their team. Then store that information, like how many players each team has.

Then use an if statement to check if that team has more than one player, then store those teams in a team, to then pick one randomly.

Don’t check players in teams, check all the players’ teams, put simply.

Example Code:

local avaibleTeams = {}

local team1 = 0
local team2 = 0

game.Players.PlayerAdded:Connect(function(player)
    player:GetPropertyChangedSignal("Team"):Connect(function()
        if player.Team == "Team1" then
            team1 += 1
        elseif player.Team == "Team2" then
            team2 += 1
        end
    end)
end)

if team1 > 0 then
    table.insert(avaibleTeams, {"Team1",team1})
end

if team2 > 0 then
    table.insert(avaibleTeams, {"Team2",team2})
end

local randomTeam = avaibleTeams[math.random(1,#avaibleTeams)]

print(randomTeam[1],randomTeam[2]) --Team, amount of players.
1 Like

I think you misunderstood what I said, I don’t want to choose a random team, but rather a random person from each team.

That’s simple then.

You can get all players in all teams, then use simple math to get a random player from each.

local teams = game:GetService("Teams")

function getPlayerPerTeam()
    for _,team in pairs(teams:GetChildren())
        local player = team:GetPlayers()[math.random(1,#team:GetPlayers())]
        print(team.Name..": "..player.Name..".")
    end
end

--Call this function whenever you need to.
getPlayerPerTeam()

And if you want to then store those players’ names, you can use table.insert to add those names into a table.

1 Like

The error “ServerScriptService.choosePres:5: invalid argument #2 to ‘random’ (interval is empty)” is occuring when I run this script. I did also have to add “do” to the end of line 4.

local teams = game:GetService("Teams")

function getPlayerPerTeam()
	for _,team in pairs(teams:GetChildren()) do
		local player = team:GetPlayers()[math.random(1,#team:GetPlayers())]
		print(team.Name..": "..player.Name..".")
end
end

--Call this function whenever you need to.
getPlayerPerTeam()

“The interval is empty”

That’s because there’s no players in that team.

And I did forget to add do.

You can add a parameter to check if there are any players in that team, to prevent that error from happening.

local teams = game:GetService("Teams")

function getPlayerPerTeam()
    for _,team in pairs(teams:GetChildren()) --Gets all teams.
        if #team:GetPlayers() > 0 then --If a team has 1 or more players.
            local player = team:GetPlayers()[math.random(1,#team:GetPlayers())] --Gets a random player from that team.
            print(team.Name..": "..player.Name..".")
        else --If the team has 0 players
            print("There are no players in team "..team.Name..".")
        end
    end
end

--Call this function whenever you need to.
getPlayerPerTeam()
if #plrTableForInd > 0 and indHasPresident == false then
	local indPresident = plrTableForInd[math.random(1, #plrTableForInd)]
	print(indPresident)
end

#plrTableForInd > 0 should be #plrTableForInd > 1 since you want it to be more than 1 player. This should work if there are more than 1 player in the team.

I think I wrote something wrong, but it is still not working. I am doing a team test with 2 clients in game on the ind team, however it still does not print a chosen player.

image

--GLOBAL VARIABLES
local indHasPresident = false
local demHasPresident = false
local repHasPresident = false
local plrTableForInd
local plrTableForDem
local plrTableForRep

plrTableForInd = game.Teams.Independant:GetPlayers()
if #plrTableForInd > 1 and indHasPresident == false then
	local indPresident = plrTableForInd[math.random(1, #plrTableForInd)]
	print(indPresident)
end

You should try printing the table and see what results are printed to make sure the players are inside the table.

1 Like

I think the issue may be is that the code is running when the game starts, but I join the team later on.

Then do the checking when the time is right, if you want it when they join just do player added and task.wait

The script runs before the players even join. So the game finds no players in the team, just before those 2 players join and nothing happens.

This is your code, but it runs every second.

--GLOBAL VARIABLES
local indHasPresident = false
local demHasPresident = false
local repHasPresident = false
local plrTableForInd
local plrTableForDem
local plrTableForRep

plrTableForInd = game.Teams.Independant:GetPlayers()

while task.wait(1) do
if #plrTableForInd > 1 and indHasPresident == false then
	local indPresident = plrTableForInd[math.random(1, #plrTableForInd)]
	print(indPresident)
end
end
1 Like

“I think the issue may be is that the code is running when the game starts, but I join the team later on.”

That’s exactly what’s happening.

1 Like

I think an issue with your code is that it’s possible for the same player to be picked twice.

You can mitigate it like this:

local list_of_players = players:GetPlayers()

for i = 1, 10 do
     local random_n = math.random(1, #list_of_players)
     print(list_of_players[random_n]) -- do something here?
     table.remove(list_of_players, random_n)
end

But what he wants is only one random player to be picked.

sorry, I saw you using a for loop and assumed.

This still works, by the way. You just need to run getPlayerPerTeam() every time.