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

Choosing Random Player, Help!?

Asked by
Jurdy 0
10 years ago

Tried making a random player chooser but doesn't work.. please help me fix this, or you could write a new script explaining the actions

players = game.Players:GetChildren()

randoms = players[math.random(1,#players)]
chosen = randoms.Name
print(chosen)

Please help :(

0
Try running it only when you know you have multiple players. GoldenPhysics 474 — 10y

3 answers

Log in to vote
1
Answered by 10 years ago

I just tested out this script, and now I know whats wrong. Your script is running when there is no players on the game. Here is a script that will give you the random player, after the player has joined.

game.Players.ChildAdded:connect(function()
    players = game.Players:GetChildren() --Gets a table of all the players
    randomPlayer = players[math.random(1, #players)] --Gets a random player from the players table
    print(randomPlayer.Name) --Prints the name of the random player
end)

I hope this helps, and if you have any questions, just comment.

Ad
Log in to vote
0
Answered by 10 years ago

-- You can take out the while wait() do loop and still have my script run correctly. :D Hope this helps you!

while wait() do



local plr=game.Players:GetPlayers()[math.random(1,game.Players.NumPlayers)] 
print(plr.Name)



wait(10)
end
Log in to vote
0
Answered by
Jurdy 0
10 years ago

It only works in solo though, doesn't work in - game :(

Answer this question