Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Selecting a random player to join a team?

Asked by 10 years ago
local numPlayers = 0

for i, v in pairs(game.Players:GetPlayers()) do
if v:FindFirstChild("TeamColor") == BrickColor.new("White") then
numPlayers = numPlayers + 1
elseif v:FindFirstChild("TeamColor") ~= BrickColor.new("White") then
    numPlayers = numPlayers - 1
end
end

if numPlayers == 0 then

local player = game.Players:GetPlayers()

local pickedPl = math.random(1, #player)
pickedPl.TeamColor = BrickColor.new("White")

end

So basically if Players on the team are equal to 0 it will pick a random player to join that team How wrong have I gone with this though?

0
I'm pretty sure FindFirstChild doesn't work on finding properties. MrNicNac 855 — 10y
0
It does Geobloxia 251 — 4y

4 answers

Log in to vote
0
Answered by 10 years ago

There doesn't appear to be anything wrong with this. However, I don't fully understand what you're trying to do with this and I don't know how to comment on this site...

However, to make it more efficient, I'd use for i, v in next.

local numPlayers = 0

for i, v in next, game.Players:GetPlayers() do
if v:FindFirstChild("TeamColor") == BrickColor.new("White") then
numPlayers = numPlayers + 1
elseif v:FindFirstChild("TeamColor") ~= BrickColor.new("White") then
    numPlayers = numPlayers - 1
end
end

if numPlayers == 0 then

local player = game.Players:GetPlayers()

local pickedPl = math.random(1, #player)
pickedPl.TeamColor = BrickColor.new("White")

end

Ad
Log in to vote
0
Answered by 10 years ago

Like truefire2, I do not understand what exactly you are trying to do here.

I assume you are trying to pick a random player, and switch his team, so try this:

TeamName = "Put the team name here"
players = {}
selected = ""

function GetPlayers()
    for _,v in pairs(game.Players:GetChildren()) do
        table.insert(players,v)
    end
end

GetPlayers()

function SelectPlayer()
    local num = math.random(1,#players)
    selected = players[num]
    print('Selected player was: ' ..selected)
end

SelectPlayer()

function TeamPlayer(player)
    local play = game.Players:FindFirstChild(player)
    local team = game.Teams:FindFirstChild(TeamName)

    if play~= nil then
        play.TeamColor = team.TeamColor
    end
end

TeamPlayer(selected)

Lets break it up, and I will explain it.

Our variables we will be working with:

TeamName = "Put the team name here"
players = {}
selected = ""

This section inserts every player into the table "players," so that later on, we can randomize from that table.

function GetPlayers()
    for _,v in pairs(game.Players:GetChildren()) do
        table.insert(players,v)
    end
end

After that, we use the function "GetPlayers()"

Next, we need to select a player. We do that by getting how many names are in the table, and starting from 1, to that number, randomize a number in between them.

After that, we change our "selected" variable to the name of the randomly selected.

function SelectPlayer()
    local num = math.random(1,#players)
    selected = players[num]
    print('Selected player was: ' ..selected)
end

Now it is just time to change the team of the player.

function TeamPlayer(player)
    local play = game.Players:FindFirstChild(player)
    local team = game.Teams:FindFirstChild(TeamName)

    if play~= nil then
        play.TeamColor = team.TeamColor
    end
end

TeamPlayer(selected)
Log in to vote
0
Answered by
jtefurd 50
7 years ago
local players = game:GetService("Players");
local teamNumDif = 0;

for _, player in pairs(players:GetPlayers()) do
    teamNumDif = (player.TeamColor = BrickColor.Palette("White")) and teamNumDif + 1 or teamNumDif - 1;
end
if (teamNumDif == 0) then
    players[math.random(players.NumPlayers)].TeamColor = BrickColor.Palette("White");
end
0
Where do I put the script? DrisTheGoat -9 — 5y
Log in to vote
0
Answered by 4 years ago

that is indeed correct

Answer this question