New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Make a math.random but garbing the players?

Asked by 5 months 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 3 — 5mo
0
GARBING u mean grabing BashGuy10 373 — 5mo

3 answers

Log in to vote
0
Answered by 5 months ago
Edited 5 months 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 3 — 5mo
0
Edited WillieTehWierdo200 897 — 5mo
Ad
Log in to vote
0
Answered by 5 months 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 3028 — 5mo
0
Doesn't matter greatneil80 1747 — 5mo
0
You're teaching someone to use the wrong method vs the right method, so it does matter. Azarth 3028 — 5mo
Log in to vote
0
Answered by 5 months 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 897 — 5mo
0
I see, thanks! JudgeDuckie 5 — 5mo

Answer this question