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

Picking a random player from a server?

Asked by 10 years ago

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

2 answers

Log in to vote
1
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
10 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.

Ad
Log in to vote
0
Answered by 10 years ago

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

game.Players.PlayerAdded:connect(function(p)
reupdate()
end)

game.Players.PlayerRemoving:connect(function(s)
reupdate()
end)




Answer this question