🍕Job system - Create job in DB

Here is an example of code that can be used to create pls_jobsystem (link) directly in the job database.

Valid for ESX only

1) Go to server.lua - Find event pls_jobsystem:server:saveNewJob

Insert the code registerJobInDatabase(jobData) under the SaveJobs() function

       if not IsJobExist(jobData.job) then
                    table.insert(Jobs, jobData)
                    lib.notify(src, {
                        title="Hell yeah!",
                        description="A new job has been created!",
                        type="success"
                    })
                    SaveJobs()

                registerJobInDatabase(jobData)
            else

Then, at the end of server.lua, insert the new function we will create.

function registerJobInDatabase(data)

end

What does the data value contain?

data.label = Job label - STRING
data.coords = Player coords - VECTOR3
data.job = job name - STRING
data.area = Area ( size) - INT(NUMBER)

Function registerJobInDatabase(data)

function registerJobInDatabase(data)
    MySQL.query('SELECT * FROM jobs WHERE name = ?', { data.job }, function(result)
            if #result == 0 then
                -- If job not exist
                MySQL.insert('INSERT INTO jobs (name, label, whitelisted) VALUES (?, ?, ?)', 
                { data.job, data.label, 0 }, function(insertId)
                    if insertId then
                        print("Job successfully inserted with ID: " .. insertId)
    
                        -- Define grades
                        local grades = {
                            {job_name = data.job, grade = 0, name = 'newbie', label = 'Newbie', salary = 200, skin_male = '{}', skin_female = '{}'},
                            {job_name = data.job, grade = 1, name = 'employe', label = 'Employe', salary = 400, skin_male = '{}', skin_female = '{}'},
                            {job_name = data.job, grade = 2, name = 'manage', label = 'Manage', salary = 500, skin_male = '{}', skin_female = '{}'},
                            {job_name = data.job, grade = 3, name = 'boss', label = 'Boss', salary = 1000, skin_male = '{}', skin_female = '{}'}
                        }
    
                        -- Insert grades
                        for _, grade in ipairs(grades) do
                            MySQL.insert('INSERT INTO job_grades (job_name, grade, name, label, salary, skin_male, skin_female) VALUES (?, ?, ?, ?, ?, ?, ?)', 
                            { grade.job_name, grade.grade, grade.name, grade.label, grade.salary, grade.skin_male, grade.skin_female }, function(gradeInsertId)
                                if gradeInsertId then
                                    print("Grade successfully inserted with ID: " .. gradeInsertId)
                                else
                                    print("Failed to insert grade: " .. grade.name)
                                end
                            end)
                        end
                    else
                        print("Failed to insert job")
                    end
                end)
            else
                print("Job already exists")
            end
        end)
end

Don't forget to add to fxmanifest.lua

server_scripts {
 "@oxmysql/lib/MySQL.lua", -- This line on start 
}

IMPORTANT

This code assumes that you are using oxmysql https://github.com/overextended/oxmysql

Last updated