Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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

Log in to vote
0
Answered by
RubenKan 3615 Moderation Voter Administrator Community Moderator
6 years ago

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.
Ad

Answer this question