Ad
Log in to vote
0

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

Asked by
NexeusX 137
7 years ago

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?

0
so how you do this themasterrocket67 -5 — 3y

1 answer

Log in to vote
1
Answered by 7 years ago

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")
1
wow thank alot! NexeusX 137 — 7y
0
invalid argument #2 to 'random' (interval is empty) 2_MMZ 1057 — 7mo
0
This was posted 6 YEARS AGO theepicboy6789 56 — 6mo
Ad

Answer this question