You should just be able to create a table with the same length to your roles that has a number in each slot. For each number that matches with your roles, you have a maximum.
For example:
local Roles = {"Scientist", "Spy", "Soldier", "Entity"};
local RoleMaximums = {1,2,6,1};
EDIT: I’m not sure if this is what you needed but this is how I would do it or by creating a multidimensional array (which I can explain if you’d like to know).
Sure thing. So, a multi-dimensional array is just a fancy word for an array inside of an array. Each “dimension” is equivalent to an array being used. Let me give some examples:
1D Array
local array = {}; -- An array is a table but in other programming languages we say array instead of table
2D Array
local array2 = {{}}; -- As you can see, it is an array inside of an array.
For your use case, we would do:
local Roles = {{"Scientist", 1}, {"Spy", 2}};
This allows to retrieve the role name and the role maximum in a loop. For example:
for i=0, i<#Roles do
local roleName, roleMaximum = Roles[i][1], Roles[i][2]; -- Roles[i] is getting each 2nd dimension array. Let's say we have {"Scientist", 1} as our selected array. We can now use [1] and [2] to get the name and maximum.
end
For every dimension you add, you add an extra bracket []. Think of it like accessing the highest array (one with the first and last brackets) then accessing the smaller arrays inside of the biggest one.
Forst off, you should store the maxes in a array (RoleMaxes) corresponding to the Role array, so that you don’t have to do extra work. Next, you need a array to track those already in a certain role (filled). Then, when you want to choose a role you simply figure out which roles are still open (filled value < max value), and choose from those a random one. Before returning it, add one to the filled to represent the role being filled. Then, return the chosen role.
Below is a working example:
local Roles = {"Scientist", "Spy", "Soldier", "Entity"}
local RoleMaxes = {1, 2, 6, 1}
local filled = {0, 0, 0, 0}
local function chooseRole()
local open = {}
for i = 1, #Roles do
if filled[i] < RoleMaxes[i] then
table.insert(open, i)
end
end
if #open > 0 then
local randomOpen = open[math.random(1, #open)]
filled[randomOpen] = filled[randomOpen] + 1
return Roles[randomOpen]
else
return 'NO ROLES LEFT' --You can change this to whatever you want
end
end
local selectedRole = chooseRole()
print(selectedRole)