Making DarkRP jobs stay permanent through the database?

I’m trying to get jobs to become permanent for my friends server. Anyone know how to do this? I have no knowledge of Sql and lua combined…

Do you want custom job names to save too? (/job <name>)

No, but comment it out so I can learn what you did :stuck_out_tongue: Thank you Bo98!

Try this. Completely untested so you may want to test it on a test server first if you wish. I’ll test it myself tomorrow if you haven’t already done so.

Completely serverside.

hook.Add(“DatabaseInitialized”, “DarkRP_PersistentJobs”, function()
CREATE TABLE IF NOT EXISTS darkrp_persistentjobs(
local initialSpawn = GM.PlayerInitialSpawn
function GM:PlayerInitialSpawn(ply)
initialSpawn(self, ply)
MySQLite.query("SELECT job FROM darkrp_persistentjobs WHERE uid = " … ply:UniqueID() … “;”, function(data)
if not IsValid(ply) then return end

	local info = data and data[1] or {}
	local teamIndex = info.job or self.DefaultTeam

	if not data then
		MySQLite.query([[REPLACE INTO darkrp_persistentjobs VALUES(]] .. ply:UniqueID() .. [[, ]] .. teamIndex .. [[);]])

hook.Add(“OnPlayerChangedTeam”, “DarkRP_PersistentJobs”, function(ply, _, newTeam)
MySQLite.query([[UPDATE darkrp_persistentjobs SET job = ]] … newTeam … [[ WHERE uid = ]] … ply:UniqueID())

Just as a reference since you seemed interested, here’s what it would take to support custom job names:

  • Add a VARCHAR(25) nullable column to the SQL table
  • Use this in the call to updateJob in PlayerInitalSpawn while falling back to team.GetName(teamIndex) if the value is equal to the string “NULL” or is nil
  • Look for job name changes and update the SQL table. There is currently no hook for after a job name is changed(!) but you can still get around that. canChangeJob is run before the name is changed and this can easily be cancelled by another hook so this is not reliable. You could listen for the “job” DarkRPVar in DarkRPVarChanged hook but this will call when a player goes AFK and you have enabled the AFK module as the module masks the players job name to “AFK” without calling ply:updateJob. If you don’t have the AFK module enabled then that hook is fine. Otherwise you’ll have to detour ply:updateJob or the /job command. There’s a problem however (unless you detour the /job command) where if you were to change a job’s name in jobs.lua then people will keep the old name until they change jobs. This can only really be fixed by detouring the /job command since there’s not a proper hook. Do note that if you detour the /job command then you will need to set the custom job name in the SQL table to “NULL” in OnPlayerChangedTeam. If you do not then when the player rejoins, their job name will go back to the last time they ran /job.

I probably didn’t make much sense so I could show you an example of that if you wanted.

EDIT: I’m getting CloudFlare CAPTCHAs when posting or editing this. I’ve obviously talked too much that it thinks it as a malicious packet.

Thanks alot Bo98! Works great and you made it easy for me to learn! Thanks again mate!

If this is solved, please mark it solved in the top left corner to avoid confusion :slight_smile:


DarkRP Help Thread:

You’ll have a lot more luck in that thread; the same question has probably already been asked, and answered, in that thread.