Getting a random player?

How can I get a random player in a serverscript becouse this doesnt work for me.

local players = game:GetService("Players"):GetPlayers()
local pickedPlayer = players[math. random(1, #players)]

If that code runs before any player joins, it will not get a random player.

4 Likes

There’s no space between math. and random.

local players = game:GetService("Players"):GetPlayers()
local pickedPlayer = players[math.random(1, #players)]

As mentioned above, if this is run at the very start of script execution, since there are no players yet, it’ll error or return nil.

^^

For this you should probably do:

while players < 2 do
    task.wait(.1)

or something like that

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.