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

How do I select a random player?

Asked by 8 years ago

So I am currently learning random player selection, but I got a problem, It only print out player[#player], which is not the intention of this script. Anyone who know how to fix this problem?

player = game.Players:GetPlayers()
rand = math.random(1, #player)

for i, v in pairs (player) do

    print(#player)
    print(player[rand])

end 

Output when I start server[3 Players]:

On player 1's screen:

1
Player1

On player 2's screen:

2
Player2
2
Player2

On player 3's screen:


3 Player3 3 Player3 3 Player3

1 answer

Log in to vote
1
Answered by 8 years ago

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
    -- stuff
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:

-- I personally prefer to use locals unless it's for a specific reason.
local Players = game.Players:GetPlayers()

for i = 1,#Players do

    -- We're now indexing "Players" for whatever 
    -- math.random(1-#Players) returns.

    local RandomPlayer = Players[math.random(#Players)]

    -- Should be a random player every time.
    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:

-- Get players service
local Players = game:GetService'Players'

-- When a new player is added the the game, execute this function ...
Players.PlayerAdded:connect(function(Player)

    -- Get a table of all players
    local Playing = Players:GetPlayers()

    -- Pick a random player in that table
    local RandomPlayer = Playing[math.random(#Playing)]

    -- Print it
    print(RandomPlayer)

end)

-- This will print a random player every time a new player joins the game.

In a loop:

-- Again, get players service
local Players = game:GetService'Players'

while wait(5) do

    -- You probably get the idea
    local Playing = Players:GetPlayers()
    local RandomPlayer = Players[math.random(#Playing)]
    print(RandomPlayer)

end

-- This will print a random player every 5 seconds

If you have any questions, just let me know.

0
No questions, this works perfectly fine now. Thanks for the help on such a short notice! Hyperclux 65 — 8y
Ad

Answer this question