Okay so, you pretty much have the right idea, but we could change a few things.
pairs
This is kinda off topic, but you should probably use a numeric for loop. In the for loop you're demonstrating, we're not really concerned about keys and values, just the number of cycles the loop will run. So, let's just change that to:
for i = 1, #player do
end
Back on topic
Now, if i understand you correctly, you wan't to make the loop choose a random player each cycle it makes. To do this, we'd have to put the random number, inside the loop (so it re-calculates the random algorithm every time the loop runs). Like this:
local Players = game.Players:GetPlayers()
for i = 1,#Players do
local RandomPlayer = Players[math.random(#Players)]
print(RandomPlayer)
end
Note
This code only runs once, pretty much instantly. So if you want this to work with multiple players, you'd need to either put all this code in an infinite loop, or with an event (since i'm pretty sure this code would finish before the game would have time to recognize any more players), like this:
In an event:
local Players = game:GetService'Players'
Players.PlayerAdded:connect(function(Player)
local Playing = Players:GetPlayers()
local RandomPlayer = Playing[math.random(#Playing)]
print(RandomPlayer)
end)
In a loop:
local Players = game:GetService'Players'
while wait(5) do
local Playing = Players:GetPlayers()
local RandomPlayer = Players[math.random(#Playing)]
print(RandomPlayer)
end
If you have any questions, just let me know.
1 comment
No questions, this works perfectly fine now. Thanks for the help on such a short notice!