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

Make a math.random but garbing the players?

Asked by 5 years ago

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!

0
Trying to grab the player so I can chose one of the players as murder and one as sheriff. Nosh4309 8 — 5y
0
GARBING u mean grabing BashGuy10 384 — 5y

3 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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
0
Thank you do you know how I could make it so it wont be same players? Nosh4309 8 — 5y
0
Edited WillieTehWierdo200 966 — 5y
Ad
Log in to vote
1
Answered by 5 years ago

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

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. WillieTehWierdo200 966 — 5y
0
I see, thanks! JudgeDuckie 25 — 5y
Log in to vote
0
Answered by 5 years ago

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

0
GetPlayers() Azarth 3141 — 5y
0
Doesn't matter greatneil80 2647 — 5y
0
You're teaching someone to use the wrong method vs the right method, so it does matter. Azarth 3141 — 5y

Answer this question