How to create a role system similar to murder mystery 2?

Heylo.

So i want to know how to create a system where people (in the beginning of the round) get one of two roles:

  • the catcher
  • the runner

and depending on what role they get, they get different abilities. (special abilities for the catcher and special abilities for the runner) i will work on abilities later

My problem is that im new to roblox developing, and there is a huge possibility that i might just not know some specific code, or event, or function etc.

If there is any chance you can tell me how to do so in “not so hard” terms or provide me with some links to youtube videos or whatever, ill really appreciate it

3 Likes

Essentially what you want to do is get 2 random players from inside the server and select them to fulfill each role. Then, you will give them each of their own abilities.

First, get the list of players in the game.

local players = game.Players:GetPlayers()

Next, we have to select the first player (the catcher) from that list.

local catcher = players[math.random(1, #players)]

What the line of code above is doing is the following: the ‘players’ variable is a table. So, it’s getting an element from that table using the square brackets []. To select which element we want, we’re using math.random(1, #players), which is selecting a random number from 1 to the number of players we currently have in the game. For example, let’s imagine the number of players in the game is 5. It would be like writing math.random(1, 5). Now, let’s imagine the number 3 is picked. We’d be getting the 3rd player from that list (players[3]).

Now, we remove the catcher from the list we created of the players.

table.remove(players, table.find(players, catcher))

The rest is now up to you. If you want to have every single other player be the runner, then you can use the players list we made to iterate over every single other player. However, if you want only 1 other player to be the runner, follow the instructions below:

local runner = players[math.random(1, #players)]

Now we can do the exact same thing we used to get the catcher, but for the runner instead. With all of this finalized, we now have both the players chosen, the catcher and the runner. Using these two players, you can do whatever you want with them, such as changing their walkspeed, giving them a specific item, etc.

The finalized code would look like this:

local players = game.Players:GetPlayers()

local catcher = players[math.random(1, #players)]
table.remove(players, table.find(players, catcher))
local runner = players[math.random(1, #players)] -- you can choose whether or not you want to include this line of code, depending on if you want every single other player to be the runner or only one
3 Likes

Thanks! I’m gonna try this later on. I appreciate you explaining everything in easy steps!

1 Like
local murdered = nil
local sherrif = nil
local players = game.Players

local function ChooseSherrif()
sherrif = players:GetPlayers()[math.random(1,#players:GetPlayers())
end
local function ChooseMurderer()
murderer = players:GetPlayers()[math.random(1,#players:GetPlayers())
if murderer == sherrif then ChooseMurderer() end
end

-- Then do whatever you want.
2 Likes

question. where should i place the script?

It depends on what you are trying to accomplish. For this case, you are likely going to use a regular Script (not a LocalScript!) inside of ServerScriptService.

thats odd, the code gives an error:
ServerScriptService.Script:5: invalid argument #2 to ‘random’ (interval is empty) - Server - Script:5

why do both sheriff and murderer appear as nil if you run the code?

Do you mean at first? If at first its because what else should I put it to?

no no. I made the script to print out murderer and sheriff and it gave nil to both of them

local murderer = nil
local sherrif = nil
local players = game.Players

local function ChooseSherrif()
sherrif = players:GetPlayers()[math.random(1,#players)]
end

local function ChooseMurderer()

murderer = players:GetPlayers()[math.random(1,#players)]

if murderer == sherrif then 
	ChooseMurderer()
end

end

print(murderer)
print(sherrif)

Thats because you need to run ChooseSherrif than ChooseMurderer

can you tell me how can i run them please?

local murderer = nil
local sherrif = nil
local players = game.Players

local function ChooseSherrif()
	sherrif = players:GetPlayers()[math.random(1,#players)]
end

local function ChooseMurderer()

	murderer = players:GetPlayers()[math.random(1,#players)]

	if murderer == sherrif then 
		ChooseMurderer()
	end
end

ChooseSherrif()
ChooseMurderer()

print(murderer)
print(sherrif)
1 Like

it gives me an error:

ServerScriptService.Script:6: attempt to get length of a Instance value - Server - Script:6

local murderer = nil
local sherrif = nil
local players = game.Players

local function ChooseSherrif()
	sherrif = players:GetPlayers()[math.random(1,#players:GetPlayers())]
end

local function ChooseMurderer()

	murderer = players:GetPlayers()[math.random(1,#players:GetPlayers())]

	if murderer == sherrif then 
		ChooseMurderer()
	end
end

ChooseSherrif()
ChooseMurderer()

print(murderer)
print(sherrif)
1 Like

another error :sweat_smile:

ServerScriptService.Script:6: invalid argument #2 to ‘random’ (interval is empty) - Server - Script:6

The issue is I am typing this on devforum and therefore I can not fix such errors nor notice them. It would be better if you could.

local murderer = nil
local sherrif = nil
local players = game.Players

local function ChooseSherrif()
	sherrif = players:GetChildren()[math.random(1,#players:GetChildren())]
end

local function ChooseMurderer()

	murderer = players:GetChildren()[math.random(1,#players:GetChildren())]

	if murderer == sherrif then 
		ChooseMurderer()
	end
end

ChooseSherrif()
ChooseMurderer()

print(murderer)
print(sherrif)

ill so my best, im just not sure what the error means. ill try to google it

Would be annoying if error occurs now as I just tested the script and non.