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

How can I make a script choose a random player?

So I am trying to make a script choose a random player but it is not working! I put

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

18:38:06.232 ServerScriptService.Script:3: invalid argument #2 to 'random' (interval is empty)  -  Server  -  Script:3

1 answer

You cant get a random number with an input of 1. If you have one player you can choose to wait or pick that player like i did below.

local players = game.Players:GetPlayers()
local player
if #players == 1 then
	player = players[1]
	print(player)
else 
	player = players[math.random(#players)]
	print(player)
end

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