DarkRP If no mayor then job not accessible

Hey there, need help with this, so far i have this
when the mayor dies, he gets changed to civilian, and so do the people in the job mayor’s protectors
but, how do i make it so if there is no mayor, no one can join the job mayor’s protectors?

I got this so far
[lua]
hook.Add(“PlayerDeath”,“DemoteMayor”,function(ply, victim, attacker)
if (ply:Team() == TEAM_MAYOR) then
ply:ChangeTeam(TEAM_CITIZEN,true);
for k, v in ipairs(player.GetAll()) do
v:PrintMessage(HUD_PRINTCENTER,“The current Mayor of the city has been Assassinated!”);
if v:Team() == TEAM_SECRET then
v:ChangeTeam(TEAM_CITIZEN,true);
end;
end;
end;
end);

function secretandmayor( ply )
if table.Count( team.GetPlayers(TEAM_MAYOR) ) > 0 then
–this is where i assume i need to make it so secret cannot join mayor, help please <3
end
[/lua]



	customCheck = function(ply) for k,v in pairs(player.GetAll()) do if (IsValid(v) and (v:Team() == TEAM_MAYOR) and v != ply) then return true end end return CLIENT end,
	CustomCheckFailMsg = "You need a leader!" -- The message it sends to the client


team.GetPlayers(Integer TeamIndex)

[lua]
return #team.GetPlayers(TEAM_MAYOR) >= 1
[/lua]

Wait, I’m confused,
1st of all, where would i put this code
second of all, where does it define that if your trying to join team_secret it denies you, all i see is just team_mayor?

It goes in the TEAM_SECRET job in shared.lua.

Okay, but what did brandon quote? why did he wriite
[lua]
return #team.GetPlayers(TEAM_MAYOR) >= 1
[/lua]

EDIT: MuteTM explains his advantages below this post.

[LUA]
customCheck = function(ply) for k,v in pairs(player.GetAll()) do if (IsValid(v) and (v:Team() == TEAM_MAYOR) and v != ply) then return true end end return CLIENT end,
CustomCheckFailMsg = “You need a leader!”, – The message it sends to the client
[/LUA]

Might want to also note checking if the player switching is the mayor or not. But yeah, at the end of the day it’s two paths to the same place. Both work fine.

Completely missed that, sorry. That’s quite an important factor.

To put this all in one post.

In jobs.lua(refactor) or shared.lua(master)



TEAM_X = AddExtraTeam("Generic", {
	color = Color(255, 255, 255, 255),
	model = "models/player/whatever.mdl",
	description = [[Example class.]],
	weapons = {"example"},
	command = "example",
	max = 1,
	salary = 80,
	admin = 0,
	vote = false,
	hasLicense = false,
        INSERT CODE FROM BELOW HERE
})


Where it says INSERT CODE, you have a few options:

Option 1 - Hide the job when there is no mayor. The player may not switch from mayor to guard.



customCheck = function(ply) return (#team.GetPlayers(TEAM_MAYOR) >= 1 and (ply:Team() != TEAM_MAYOR)) end,
CustomCheckFailMsg = "You need a leader!" -- The message it sends to the client


Option 2 - Job shows always, but they may not switch when there is no mayor. The player may not switch from mayor to guard.



customCheck = function(ply) return CLIENT or (#team.GetPlayers(TEAM_MAYOR) >= 1 and (ply:Team() != TEAM_MAYOR)) end,
CustomCheckFailMsg = "You need a leader!" -- The message it sends to the client


Though I coded the method I posted previously, I recommend the method here for the sake of less code cluttering the shared/jobs lua.

If you want one character less code then you could use team.NumPlayers.

Thanks guys so much