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

How can i make a Script to choose a random player?

I want to make a script like on the darkness game where it chooses a random player and puts them on a certain team how can i do this?

1 comment

0

so how you do this

1 answer

✓ Accepted answer

You can also do this in one line.

local randomPlayer = game.Players:GetPlayers()[math.random(1,#game.Players:GetPlayers())]

Broken down:

game.Players:GetPlayers() returns a table of all the players in the game

The [] brackets get something from a table, e.g.

local table = {"apple","banana"}
print(table[2])
> banana

math.random(arg1,arg2) returns a random number between arg1 and arg2

#game.Players:GetPlayers() gets the number of players in the game (# gets the length of a table)

Using the player we've got, we can put them into a team.

local randomPlayer = game.Players:GetPlayers()[math.random(1,#game.Players:GetPlayers())]
randomPlayer.TeamColor = BrickColor.new("Teams TeamColor here")

3 comments

1

wow thank alot!

0

invalid argument #2 to 'random' (interval is empty)

0

This was posted 6 YEARS AGO

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