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

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

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

✓ Accepted answer

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.

1 comment

0

Thanks for helping me!

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