You can write your topic however you want, but you need to answer these questions:
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
What is the issue? My code is not choosing a player, with no error in the output
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
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.
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.
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()
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.
--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
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
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