Random Role Picker for each round

Hello, Ive been trying to make it so roles are chosen randomly at the beginning of each round.
I want to limit the amount of players that can be assigned to each role.
For example I want to limit the Lawyer role to max of 2, the judge role to a max of 1 and defended to max of 1 and witness to max of 1 and then have the rest of the players assigned to the jury duty. Then the role to display on the screen of the player and their given role. But I just cant figure it out. I have scoured the roblox dev forum and cant find anything. Anything helps :slight_smile:

I already have a round timer system that repeats and is displayed at the top.
This is my current Script.


local Team = game:GetService("Teams"):GetChildren()


local function RoundRestart(plr)
	--if RoundStarted == false then
		RoundStarted = true

		local RoundGui = plr.PlayerGui:WaitForChild("RoundGui")

		for i = 10, 0, -1 do
			RoundGui.RoundFrame.RoundTextLabel.Text = "Next case starts in: " ..i
			wait(1)
		end

		

		plr.Character.Humanoid.Health = 0

		local RoundTime = 60
		local Seconds = 60
		local Minutes 

		while RoundTime > 0 do
			Minutes = math.floor(RoundTime / 60)
			Seconds = RoundTime % 60

			RoundGui.RoundFrame.RoundTextLabel.Text = "Time Left: "..string.format("%02d", Minutes)..":"..string.format("%02d", Seconds)

			RoundTime = RoundTime - 1
			wait(1)
		end
		RoundStarted = false
	RoundRestart(plr)
end

game.Players.PlayerAdded:Connect(function(plr)
	
	if RoundStarted == false then
		RoundStarted = true
		
		local RoundGui = plr.PlayerGui:WaitForChild("RoundGui")
		
		for i = 10, 0, -1 do
			RoundGui.RoundFrame.RoundTextLabel.Text = "Next case starts in: " ..i
			wait(1)
		end

		plr.Character.Humanoid.Health = 0
		
		local RoundTime = 60
		local Seconds = 60
		local Minutes 
			
		while RoundTime > 0 do
		Minutes = math.floor(RoundTime / 60)
		Seconds = RoundTime % 60
		
		RoundGui.RoundFrame.RoundTextLabel.Text = "Time Left: "..string.format("%02d", Minutes)..":"..string.format("%02d", Seconds)
		
		RoundTime = RoundTime - 1
		wait(1)
		end
		RoundStarted = false
		RoundRestart(plr)
	end
end)```

local Players = game:GetService(“Players”)
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)

local roles = {
“one”,
“two”,
“three”,
“four”
}

local availablePlayers = Players:GetPlayers()
local availableRoles = roles

local function onGameStart(value)
if value == “InProgress” then
repeat
local selectedRole = math.random(1, #availableRoles)
local selectedPlayer = math.random(1, #availablePlayers)
table.remove(availableRoles, selectedRole)
table.remove(availablePlayers, selectedPlayer)
print(roles[selectedRole], Players:GetPlayers()[selectedPlayer])
wait()
until #availableRoles == 0
end
end

local function onGameEnd(value)
if value ~= “InProgress” then
for i, v in pairs(Players:GetPlayers()) do
v.Character.HumanoidRootPart.CFrame = game.Workspace.Lobby.SpawnLocation.CFrame
end
end
end

ReplicatedStorage.GameState.Changed:Connect(onGameStart)
ReplicatedStorage.GameState.Changed:Connect(onGameEnd)
hope this helps

Hey traxy, I know this is probably super picky and annoying but I formatted your code, put it in a codeblock and fixed up your doublequotes.

For next time, remember to put your code in a codeblock since the roblox devforum really likes to mess up your doublequotes and set them as another character, which isn’t the same as the regular doublequote character, and it doesn’t work when you just copy & paste.

You’re welcome : )

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local roles = {
  "one",
  "two",
  "three",
  "four"
}

local availablePlayers = Players:GetPlayers()
local availableRoles = roles

local function onGameStart(value)
  if value == "InProgress" then
    repeat
      local selectedRole = math.random(1, #availableRoles)
      local selectedPlayer = math.random(1, #availablePlayers)
      table.remove(availableRoles, selectedRole)
      table.remove(availablePlayers, selectedPlayer)
      print(roles[selectedRole], Players:GetPlayers()[selectedPlayer])
      wait()
    until #availableRoles == 0
  end
end

local function onGameEnd(value)
  if value ~= "InProgress" then
    for i, v in pairs(Players:GetPlayers()) do
      v.Character.HumanoidRootPart.CFrame = game.Workspace.Lobby.SpawnLocation.CFrame
    end
  end
end

ReplicatedStorage.GameState.Changed:Connect(onGameStart)
ReplicatedStorage.GameState.Changed:Connect(onGameEnd)

What am I suppose to do with this regarding my script? Do I need a Number Value inside a server event? Got me confused.

Nvm I figured this out my brain is just broken apparently. But now I am getting ``Invalid Argument #2 ‘random’ (Interval is empty)`

1 Like