Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Selecting a random player to join a team?

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?

2 comments

0

I'm pretty sure FindFirstChild doesn't work on finding properties.

0

It does

4 answers

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

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)
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

1 comment

0

Where do I put the script?

This discussion is preserved as it was. New answers, comments, and votes are closed.