Hello, I've been trying to get a random player from the game and then change their team. I'm pretty sure how to do the team change but not picking a random player, the only thing I've really done was something to get every player which was fairly easy but otherwise I'm clueless. If you can help me, thank you!
local players = game.Players:GetChildren() local selected = players[math.random(1,#players)]
You can use GetPlayers()
, which returns a table:
local players = game.Players:GetPlayers() print(players[math.random(1, #players)]) -- Prints a random player
The reason why the other two solutions don't work is because you're attempting to use a Service
(which is Players
) as a table. Services are not tables and they should not be used like tables.
local playerNumber = 0 for i, v in pairs(game.Players:GetChildren()) do playerNumber = playerNumber + 1 end local selected = math.random(1, playerNumber) for i, v in pairs(game.Players.GetChildren()) do if i == selected then -- Change the team for v (v is the selected player) end end