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)
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
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
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.