Priority, and how to go about doing it

Hey there, I’m currently making a game like Town Of Salem, for those of you who don’t know it, it’s a social deduction role based game. Here’s the issue I’m running into, In TOS, when the night time phase is over, It gathers all the results of what everyone chose to do for the night and computed it, I thought I had a good system going, this is how I was doing it

for i,v in pairs(game.Players:GetPlayers()) do
		if v.PlayerData.RoleBlocked.Value ~= true then
			
		if v.PlayerData.Role.Value == "Veteran" then
                  --do the role stuff
             end
                if v.PlayerData.Role.Value == "Sheriff" then
                  --do the role stuff
             end
              if v.PlayerData.Role.Value == "Escort" then
                  --do the role stuff
             end

      end
end

This was working quite well, the issue that I ran into though, was the fact that since it’s a for loop of the players, Whoevers first in players, will have their role go first, The issue with this is that there are VERY many different priority levels for each role, For example Veteran > Escort > Bodyguard > Godfather
The issue I’m dealing with is that if Player 2 is a veteran, and player 1 is an escort Player 1 will go first because that’s just how the loop is set up. I was thinking about instead of for i,v in pairs(game.Players:GetPlayers()) I could like, Make a priority for each role, and do it like that, But the issue is there are SO many roles, And SO many different priorities, Does anyone know some way that I could do it how I currently have it set up, IE, through having what role I want to go first first, then the next one so on so forth, but have it so that it goes through role priority and not player priority, I know that’s confusing but if anyone understands what Im saying and can help It’d be greatly appreciated.

I’m thinking you could do it with tables/dictionaries.

local players = game:GetService('Players')
local roles = {
    [1] = 'GodFather';
    [2] = 'BodyGuard'; --etc... 1 will be first, 2 will be second etc
}

local roleFuncs = {
    ['GodFather'] = function(player)
        print('godfather', player)
    end; -- etc for the other roles
}
local blankPlayerRoles = {}
for i,v in pairs(roles) do -- create a blank container for players
    blankPlayerRoles[i] = {}
end

for i,v in pairs(players:GetPlayers()) do
    local role = v.PlayerData.Role.Value
    local rolePosition = table.find(roles, role)
    blankPlayerRoles[rolePosition] = v
end

for i,v in ipairs(blankPlayerRoles) do -- ensure you use ipairs here as you want it to go in order 100% of the time
    for i2,v2 in pairs(v) do
        roleFuncs[roles[i]](v2) -- roleFuncs[role] basically, then add the 2 parentheses at the end to ensure you're calling it
    end
end
1 Like

Yeah I think I’ll do something like this, Idk if I’ll do it with a dictionary though we’ll see Thank you

1 Like