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

How can I make it so math.random always chooses a different player instead of the same one?

In my game, I want one person to be "The Ghost" and one person as "The Hunter", and all of the others are bystanders, but sometimes math.random chooses the same player for both. How can I make it so math.random always chooses a different player instead of the same one for both the ghost and hunter?

1 comment

0

Make a table of the players, and remove the player the script chooses from that table for the ghost so that there's no option for that player to be picked again.

1 answer

✓ Accepted answer

This method works by picking a random player twice, but removing them from the pool of possible players once they have been selected

--Fetch all players
local players = game.Players:GetPlayers()

--Pick a random hunter by generating a number
local hunterMathRandom = math.random(1, #players)
local hunter = players[hunterMathRandom]

--Remove the hunter from the pool of possible players
table.remove(players, hunterMathRandom)

--Pick a random ghost by generating a number
local ghostMathRandom = math.random(1, #players)
local ghost = players[ghostMathRandom]

Some code might be wrong here, I wrote this half asleep, but you get the concept.

1 comment

0

That looks very repetitive tbh, at least to me. >.>

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