Hello, I am trying to make a sort of “Mafia” game where the computer auto-assigns roles for everyone. I have dictionaries nested inside of other dictionaries to help me, but I’m having trouble referencing to get the values that I need. The roles according to player count is just to choose how many of each role there needs to be for Five, Six, Seven, or Eight players. The player roles table is just to keep the the players in a neat fashion, having arrays in the dictionaries that contain the players. If there’s an easy way to reference dictionaries nested in other dictionaries then it would be great to know. Thanks!
if #game.Players:GetChildren() >= 5 then
if gamemode == "Automatic" then
local playerRoles = {
Mafia = {},
Host = {},
Sherrif = {},
Doctor = {},
Towny = {}
}
local rolesAccordingToPlayerCount = {
Five = {Mafia = 1, Host = 1, Sherrif = 1, Doctor = 0, Towny = 1},
Six = {Mafia = 1, Host = 1, Sherrif = 1, Doctor = 1, Towny = 2},
Seven = {Mafia = 1, Host = 1, Sherrif = 1, Doctor = 1, Towny = 3},
Eight = {Mafia = 1, Host = 1, Sherrif = 2, Doctor = 1, Towny = 3},
}
if numPlayers == 5 then
numPlayersInWords = "Five"
elseif numPlayers == 6 then
numPlayersInWords = "Six"
elseif numPlayers == 7 then
numPlayersInWords = "Seven"
elseif numPlayers == 8 then
numPlayersInWords = "Eight"
end
for key,value in pairs(playersToAssign) do
for k,v in pairs(rolesAccordingToPlayerCount[numPlayersInWords]) do -- looping through table of number of roles left according to player count
if v["Mafia"] >= 1 then
v["Mafia"] = v["Mafia"] - 1
for i,player in pairs(playersToAssign) do
if playersToAssign[i] == player then
table.remove(playersToAssign,i) -- finding value of player in table and removing them
table.insert(playerRoles["Mafia"],1,player)
end
end
elseif v["Host"] >= 1 then
v["Host"] = v["Host"] - 1
for i,player in pairs(playersToAssign) do
if playersToAssign[i] == player then
table.remove(playersToAssign,i) -- finding value of player in table and removing them
table.insert(playerRoles["Host"],1,player)
end
end
elseif v["Sherrif"] >= 1 then
v["Sherrif"] = v["Sherrif"] - 1
for i,player in pairs(playersToAssign) do
if playersToAssign[i] == player then
table.remove(playersToAssign,i) -- finding value of player in table and removing them
table.insert(playerRoles["Sherrif"],1,player)
end
end
elseif v["Doctor"] >= 1 then
v["Doctor"] = v["Doctor"] - 1
for i,player in pairs(playersToAssign) do
if playersToAssign[i] == player then
table.remove(playersToAssign,i) -- finding value of player in table and removing them
table.insert(playerRoles["Doctor"],1,player)
end
end
elseif v["Towny"] >= 1 then
v["Towny"] = v["Towny"] - 1
for i,player in pairs(playersToAssign) do
if playersToAssign[i] == player then
table.remove(playersToAssign,i) -- finding value of player in table and removing them
table.insert(playerRoles["Towny"],1,player)
print("Inserted player " .. player.Name .. " to role Towny")
end
end
end
end
end
end
end
If you need the error that it gave, it said “attempt to index number with ‘Mafia’”.