How to set max people per class?

I am making a horror murder type game with 4 classes. How do I set max people per class? Here is what I have done so far.

local spawns = {"Spawn1", "Spawn2", "Spawn3", "Spawn4", "Spawn5", "Spawn6"}

local selectedSpawn = spawns[math.random(1, #spawns)]

local spawn = workspace[selectedSpawn]

local Roles = {"Scientist", "Spy", "Soldier", "Entity"}
MaxSpy = 1
MaxSoldier = 2
MaxScientist = 6
MaxEntity = 1

local selectedRole = Roles[math.random(1, #Roles)]
print (selectedRole)
1 Like

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

1 Like

Yeah, I am a bit confused can you please explain it to me?

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.

1 Like

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)

If you have any questions, feel free to ask them.

Edit: Added better explanation

2 Likes

Ah! Thank you, makes more sense now!