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

How to select random player?

Asked by 5 years ago

Hi there! I'm trying to create a game where one player is selected to kill the other players anonymously. How would you do this? I've created a script already but it hasn't worked.

function randomizer()
    local players = game.Players:GetChildren()
    local rPlayer = game.Players:GetPlayers()
    [math.random(1, #players)]
    wait(5)
    rPlayer.TeamColor = BrickColor.new("Really red")
end

randomizer()

The error message reads

15:25:18.309 - ServerScriptService.Selecter:4: bad argument #2 to 'random' (interval is empty)

What does this mean? Is something wrong with the script?

1 answer

Log in to vote
0
Answered by
valchip 789 Moderation Voter
5 years ago
Edited 5 years ago
function randomizer()
    local players = game:GetService(:Players"):GetPlayers()
    print(players[math.random(1, #players)])
end

while wait(1) do
    randomizer()
end

Here is another way, you can obviously change the print to whatever you want! Here is an example what you can do!

function randomizer()
    local players = game:GetService(:Players"):GetPlayers()
    local i = (players[math.random(1, #players)])
    --now the random player is located as i, do whatever you want with it!
    i:Kick("hello!") --example
end

while wait(1) do
    randomizer()
end
0
Thanks! Is there just a way to keep it from running all the time, like if someone is alreay on the "Really red" brickColor team? Thunderblade234 4 — 5y
0
That is entirely up to you, and should be implemented into the basis of your game. Before each round begins you'll need to loop through all the players and choose one randomly to be the murderer. T0XN 276 — 5y
0
You should use the Random library instead of math.random, no need to seed and not biased. 3dsboy08 1 — 5y
Ad

Answer this question