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.

[lua]
hook.Add(“DatabaseInitialized”, “DarkRP_PersistentJobs”, function()
MySQLite.query([[
CREATE TABLE IF NOT EXISTS darkrp_persistentjobs(
uid BIGINT NOT NULL PRIMARY KEY,
job TINYINT UNSIGNED NOT NULL
);
]])
end)
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
	ply:updateJob(team.GetName(teamIndex))
	ply:SetTeam(teamIndex)

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

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

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:

Otherwise:

DarkRP Help Thread:
http://forum.facepunch.com/showthread.php?t=1249475

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