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

How do I make a ramdom player chosen as a monster(or alien),player,or detective?

Asked by 9 years ago

So I would like to know how to make a random player be chosen as a monster, citizen or a detective without leaderboards (so they knowing who the monster/detective is)

1 answer

Log in to vote
1
Answered by
SanityMan 239 Moderation Voter
9 years ago

I am assuming you are making a game where there is one monster, one detective, and the rest are citizens? First we must wait until there are at least 3 players:

repeat wait() until game.Players.NumPlayers >= 3

Now that we have 3 players in the game, we must choose the monster and the detective. We can do this by making a table of all the players, and once we have a monster and detective randomly chosen, removing them from the table and making everyone else citizens:

local plrs = {}
plrs = game.Players:GetChildren()
local num

num = math.random(1,#plrs)
local monster = plrs[num] --set a random player as the monster
table.remove(plrs, num) --take our monster out of the player table

num = math.random(1,#plrs)
local detective = plrs[num] --set a random player as the detective
table.remove(plrs,num)

Now that we have a chunk to choose players, lets put it in a function so we can call it every round.

function pick()
    local plrs = {}
    plrs = game.Players:GetChildren()
    local num

    num = math.random(1,#plrs)
    local monster = plrs[num] --set a random player as the monster
    table.remove(plrs, num) --take our monster out of the player table

    num = math.random(1,#plrs)
    local detective = plrs[num] --set a random player as the detective
    table.remove(plrs,num)
end

--your game codes
--When you want to pick the players for a new round, run this:
pick()

Hope this helped you learn a bit more about the basics of random math and tables.

0
Thanks for helping me! kingBOOMyo 0 — 9y
Ad

Answer this question