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

  1. Create a normal Script in ServerScriptService.
  2. Write this code in it:
local players = game:GetService("Players") -- Get Players service

local function startRound() -- startRound function
    local possiblePlayers = players:GetPlayers() -- Get all the players in the server and put them in a table
    
    local catcher = possiblePlayers[math.random(1, #possiblePlayers)] -- Select a random player to be the catcher
    table.remove(possiblePlayers, catcher) -- Remove the catcher from the possible players to choose from
    local runner = possiblePlayers[math.random(1, #possiblePlayers)] -- Select a random player to be the runner
    table.remove(possiblePlayers, runner) -- Remove the runner from the possible players to choose from
    
    -- Do what you will with the catcher and runner
end

startRound()
  1. Now, whenever you call startRound(), it will select a catcher and a runner, and you can add on to the function to do whatever you want with the catcher and runner.

  2. (optional) If you want to loop the startRound() function over and over again, add this to the script:

while true do -- Forever loop
    startRound() -- Start round
    
    task.wait(180) -- Replace 180 with however many seconds you want in between rounds
end
1 Like

question. what if i want to have 1 catcher and make everyone else a runner, not just 1

also it gives an error:
ServerScriptService.Script2:6: invalid argument #2 to ‘random’ (interval is empty) - Server - Script2:6

1 Like

Hey man, if you still need help or if anybody wants the code, here it is:

function roleChooser.ChooseRole()
	local players = game:GetService("Players") -- Get Players service

	local function startRound() -- startRound function
		local possiblePlayers = players:GetPlayers() -- Get all the players in the server and put them in a table
		
		players.PlayerAdded:Connect(function()
			
			local catcher = possiblePlayers[math.random(1, #possiblePlayers)] -- Select a random player to be the catcher
			table.remove(possiblePlayers, catcher) -- Remove the catcher from the possible players to choose from
			local runner = possiblePlayers[math.random(1, #possiblePlayers)] -- Select a random player to be the runner
			table.remove(possiblePlayers, runner) -- Remove the runner from the possible players to choose from

			
			
		end)


	end

	startRound()
end

1 Like