Disallowing the use of one job when a certain job is being used - DarkRP

So in my server I want to add a job called Fuhrer. As you can guess, I want this job to basically be another mayor. But I have one issue, when Fuhrer is being used, mayor can also be used. is there a way I can set it so mayor cannot be used Fuhrer is being used? If anyone could help me out that would be great. :smiley:

PS: Thanks for taking the time to read this.

bump

Yes.

You can hook into

GM:PlayerCanJoinTeam which will tell you what team the player is trying to join.

Then using an in pairs loop within that hook you can use

player.GetAll (Alternatively you can use

player.GetHumans if you want to exlcude bots) to cycle through all the players and check if anyone is either job mayor or fuher using

Player:Team.

If someone is mayor or fuher and the person is attempting to join the alternate of the job already occupied then you can simply deny their job change by returning false in the hook.

Or couldn’t you use



if (#team.GetPlayers(mayorindex) > 0) then -- Will return true if there are players in that team


instead?

For you know, to see if there are any players in it.
I think it’d be easier.

(https://wiki.garrysmod.com/page/team/GetPlayers)

how exactly would I use this? I’m sorry, I’m a little new to lua.

Could you possibly further explain is a simpler way. I’m sorry I’m getting new to lua but I know some basics.

Yeah, I will go ahead and break everything down for you. I am up late and can’t sleep so might as well.

First thing you need to do is hook into the function

GM:PlayerCanJoinTeam you can do that with

hook.Add. The resulting line would look like this.

[LUA]hook.Add(“PlayerCanJoinTeam”, “stopJob”, function(ply, job) end) [/LUA]

With this hook every time someone goes to change a job the code we put in the hook will be ran. The hook provides us with the player (ply) and the team (job) they are attempting to join. Since this code only needs to be run when someone is trying to join mayor or fuher we can put a check in to see if they are turning to that job.

[LUA] if job ~= TEAM_FUHER or job ~= TEAM_MAYOR then return true end – the ~= symbol mean not equal to [/LUA]

This line will check if the person is trying to switch to either one of those jobs if they aren’t then the return statement will stop the code from running since it would be pointless to continue. Now if the person is switching to the job FUHER or MAYOR we need to check if the alternative job is already in use and then deny the request to chnage. This can be accomplished with

team.GetPlayers.
[LUA] if job == TEAM_FUHER then – If they are changing to the fuher we need to check the mayor
if (#team.GetPlayers(TEAM_MAYOR) > 0) then – If there is someone in the mayor slot we run the code below
ply:PrintMessage(3, “You can’t become the Fuher while there is a Mayor”) return false end – Notifiying the player they can’t become the fuher
end
else – If they aren’t trying to become the fuher then they are trying to be the mayor
if (#team.GetPlayers(TEAM_FUHER) > 0) then – Now we do the same thing we did above only this time looking for a fuher
ply:PrintMessage(3, “You can’t become the Mayor while there is a Fuher”) return false end
end
end
[/LUA]
Alright I know that looks like a lot but we will break it down. The first thing that is done is to check if the player is joining the job fuher. If the player is joining that job then we need to check if someone is the mayor. We use

team.GetPlayers with the (#) symbol to get the number of people currently in that job. If there is someone currently in that position then we display a message to the player using

Player:PrintMessage which will display in their chat. We then return the value false so that the function we hooked into knows to deny the players request to change the team. Now if the person is not trying to become the fuher we know that they are trying to become the mayor, do to our earlier check at the top. This means we don’t need to check what job they are using and instead we can use the else statement. We then repeat the same process for checking the amount of people in the job as we did with the fuher.

That’s pretty much all there is to it. Here is what all of the code put together should look like. Hopefully this helped you understand what the code is actually doing and you won’t just simply shove it into your server without thinking about it.
[LUA]
hook.Add(“PlayerCanJoinTeam”, “stopJob”, function(ply, job)
if job ~= TEAM_FUHER or job ~= TEAM_MAYOR then return true end

if job == TEAM_FUHER then
if (#team.GetPlayers(TEAM_MAYOR) > 0) then
ply:PrintMessage(3, “You can’t become the Fuher while there is a Mayor”) return false end
end
else
if (#team.GetPlayers(TEAM_FUHER) > 0) then
ply:PrintMessage(3, “You can’t become the Mayor while there is a Fuher”) return false end
end
end
end) – This is the end of the hook very important
[/LUA]

NOTE: You may need to change TEAM_FUHER and TEAM_MAYOR those were just place holders I used to simulate what team ENUMs look like. The ones you are using may be different.

Thanks a ton for doing this for me, I am familiar with some lua, but mostly other languages, so I did understand it all 100%. Thank you for all your help kind sir.