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

How to grab all players in game and choose one random one?

I've tried this a couple different ways and can not seem to get it to work. The code I have is short, but let me explain. What I am trying to write is a script that will grab all the players in the game, and choose one randomly. Everytime I try to print or run to test my script it throws an error saying ' unable to index local i, a number value' am i using the random number wrong? Let me know if you guys can help, thanks!



for i,v in pairs(game.Players:GetChildren()) do
	local ranI = math.random(1,i)
	local ChosenPlayer = i[ranI]
	
end

1 answer

Try this.

local Players = game.Players:GetPlayers() -- Get a table that contains every Player.
local ChosePlayer = Players[math.random(#Players)] -- Get a random value from the table.

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