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)
2 comments
I'm pretty sure FindFirstChild doesn't work on finding properties.
It does