Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Make a math.random but garbing the players?

So this is a really simple script but I just cant figure it out can someone help me out.

local Rplayers = game.Players:GetChildren()
local Mplayers = math.random(1,#Rplayers)
local Aplayers = Rplayers[1]
local Bplayers = Rplayers[2]

Thank you for helping me!

2 comments

0

Trying to grab the player so I can chose one of the players as murder and one as sheriff.

0

GARBING u mean grabing

3 answers

✓ Accepted answer

To get two random players

local players = game.Players:GetPlayers()
local randomPlayer1 = players[math.random(#players)]
local randomPlayer2 = players[math.random(#players)]

while #players > 1 and randomPlayer1 == randomPlayer2 do
    players = game.Players:GetPlayers()
    randomPlayer2 = players[math.random(#players)]
end

2 comments

0

Thank you do you know how I could make it so it wont be same players?

0

Edited

Here Duckie will elaborate some more for you. So in the roblox lua world to get players you use game.Players:GetPlayers()

local players = game:GetService("Players"):GetPlayers()

Now to get random players! First we get our first player and when we get out second player we make sure the first and second are not the same.

local players = game:GetService("Players"):GetPlayers()

local playerone = players[math.random(1, #players)]
local playertwo = players[math.random(1, #players)]--you don't need the 1 but it helps

if playertwo == playerone then
repeat
local playertwo = players[math.random(1, #players)]--you don't need the 1 but it helps
until playertwo ~= playerone
end

There the end :D

2 comments

0

You have to be careful when running a loop to allow the loop to yield (wait) or ensure that there is a reasonable break condition. If there is only one player in the game when line 01 is run, your loop will never end, and the script will crash.

0

I see, thanks!

To get a random player do this:

local rand = math.random(1,#game.Players:GetChildren())
local chosen = game.Players:GetChildren()[rand]

If this helped, clicked accept

3 comments

0

GetPlayers()

0

Doesn't matter

0

You're teaching someone to use the wrong method vs the right method, so it does matter.

This discussion is preserved as it was. New answers, comments, and votes are closed.