What are substitutes for nil?

I have this assign a player a random role script. It assigns people their roles however, a single person has multiple roles.

Ex.
Traitor: TheHungbao
Medic: TheHungbao
Detective: TheHungbao

This happens even if I test with 3-6 players in the server. I suspect the problem being that all of these roles are equal to nil, but then nil shouldn’t have an effect on this.

I thought of changing variables and my indexes, that didn’t help.
Tried setting Killer not equal to Medic, didn’t work.
Killer gets picked first, so I tried removing the Killer out of the player pool for other roles.

local MedicID = math.random(1, #PlayersGroup - Killer)
– or I tried out
local MedicID = math.random(1, #PlayersGroup - KillerID)

Did not work. Can’t subtract nil values.

How do I fix this?

You could substitute “0” for nil. Also can you please indent your code, it’s hard to read.

Tried substituting 0 for nil. Didn’t work

1 Like

Can you please put your code in a code block, I can not read it.

How? I tried using Blockquote but it still pushes everything to the left.

1 Like

You use these 3 keys:

--- These are the 3 keys: ```
local Workspace = game:GetService("Workspace")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StarterGui = game:GetService("StarterGui")

local Event = ReplicatedStorage:WaitForChild("DisplayRole")
local TraitorHUD = StarterGui:WaitForChild("TraitorHUD")

local Killer = nil
local Medic = nil
local Detective = nil
local Survivors = {}

function pickPlayers()
	
	Killer = nil
	Medic = nil
	Detective = nil
	Survivors = {}
	
	local PlayersGroup = Players:GetPlayers()
	local KillerID = math.random(1, #PlayersGroup) -- Picks ONE player from the entire server
	local MedicID = math.random(1, #PlayersGroup)
	local DetectiveID = math.random(1, #PlayersGroup) 
	
	for i, v in pairs(PlayersGroup) do 
		if i == KillerID then
			Killer = v.Name
			break
		end
	end
	
	for i, v in pairs(PlayersGroup) do
		if i == KillerID then
			
			else
			table.insert(Survivors, v.Name)
		end
	end
	
	for z, x in pairs(PlayersGroup) do
		if z == MedicID then
			Medic = x.Name
			
			break
		end
	end
	
	for z, x in pairs(PlayersGroup) do
		if z == MedicID then
			
		else
			table.insert(Survivors, x.Name)
		end
	end
	
	for a, d in pairs(PlayersGroup) do
		if a == DetectiveID then
			Detective = d.Name
			break
		end
	end
	
	for a, d in pairs(PlayersGroup) do
		if a == DetectiveID then
			
		else
			table.insert(Survivors, d.Name)
		end
	end
	
	Event:FireClient(Players[Medic], "You are Medic") -- Sends a text onto a players screen
		Event:FireClient(Players[Detective], "You are Detective")
	for i, v in pairs(Survivors) do
		Event:FireClient(Players[v], "You are Civilian")
	end
	Event:FireClient(Players[Killer], "You are Traitor")
	Players[Killer].PlayerGui.TraitorHUD.Enabled = true
	
	print("Medic: ".. Medic) -- Prints the role and name of a player
for i, v in pairs(Survivors) do
	print("Civilian: ".. v)
	end
	if Players[Killer] then
		print("Traitor: ".. Killer)
	end
	if Players[Detective] then
		print("Detective: ".. Detective)
	end
end

while wait(10) do
	pickPlayers()
end
1 Like

Couldn’t edit my original post. Kept getting “403 error”

1 Like

Adding quotes would turn it into a string. Which nil is already a string. It wouldn’t do anything.

1 Like

Well… what does he mean by substitute nil?

So the game is like mafia. A person should have one role (Traitor, medic, detective, or civilian).

If you’re trying to create a system in which 3 players are assigned different roles, the following logic should be what you’re looking for:

local PlayersGroup = Players:GetPlayers()

local assigned_players = {}
local role_texts = {"Medic", "Killer", "Detective"} --//Used to define keys in the table

for i = 1, 3 do --//3 roles, so iterate 3 times.
local selected_index = math.random(#PlayersGroup)
local chosen_player = PlayersGroup[selected_index] --//Choose random player
local key = role_texts[i] --//Key to use for this iteration

assigned_players[key] = chosen_player --//Store chosen player in a dictionary versus storing 3 different variables

table.remove(PlayersGroup, selected_index) --//Remove the index of the selected player from the PlayersGroup table so it cannot be chosen again
end
3 Likes

The problem is. A player can be for example. A traitor and a medic when they should only be one of them.

That doesn’t change anything. Using a string “nil” is not a substitute for nil.

I understood. I deleted my comment as I saw it was wrong.

1 Like

Do you want to pick a random role out of all the roles the player could play as?

I get an error saying that “#PlayersGroup” is empty. Tested this in ROBLOX Studio Play and actually joining the game. Both times received the same error.

1 Like

Check if the Players in game is higher than 0 if you want to prevent this error:

if tonumber(#PlayersGroup) > 0 then
 
     -- Code
  else
    return
end
1 Like

(#PlayersGroup) Interval is empty

1 Like

What does it return? A number, nil?