-1

# Picking a random player from a server?

I need a script to pick a random person from a server, and make it repeat with different people at a time

1
1waffle1 2908
7 years ago
while wait(1)do
if game.Players.NumPlayers>0 then
local chosen=game.Players:GetPlayers()[math.random(1,game.Players.NumPlayers)]
print(chosen)
end
end


This will print a random player in the game every second. Unfortunately it can't pick different people at a time while only picking a single random person because that is logically impossible.

0

You could try taking everyone, and putting them in a table! Of course, you still need to constantly update the table based on who is in the server! Then select randomly from the table! Once you select the person, remove them from the table so they can't be picked again! Although, I probably wrote the code a tad bit ineffeciently then it should be, I would normally do something along the lines of:

bob = {}

function reupdate()
bob = {}
for _,v in pairs(game.Players:GetPlayers()) do
table.insert(bob,v.Name)
end
end

function pickRandom()
for i,k in pairs(bob) do
s = math.random(1,#bob)
return s
table.remove(bob,s)
end
end