Can't load saved cars from MySql

Hello,

I’m trying to make a script to /park a car, which saves its location into a mysql table and loads it whenever the resource is started.

This is my client.lua:

RegisterCommand('park', function()
    local playerID = GetPlayerPed(-1)
    local playerVehicle = GetVehiclePedIsIn(playerID)
    

    if playerVehicle <= 0 or not GetPedInVehicleSeat(playerVehicle, -1) == playerID then
        TriggerEvent("chat:addMessage", {
            args = {"You're not in a vehicle."}
        })
        return
    end

    local carModel = GetEntityModel(playerVehicle) 
    local carPos = GetEntityCoords(playerVehicle)
    local carHeading = GetEntityHeading(playerVehicle)
    local carColor = GetVehicleColours(playerVehicle)


    TriggerServerEvent('ch_savecars:parkcar', playerVehicle, carModel, carPos, carHeading, carColor)

end)

This is my server.lua:

--Save car
RegisterNetEvent('ch_savecars:parkcar', function(playerVehicle, carModel, carPos, carHeading, carColor)
    local pid = source
    MySQL.insert('INSERT INTO parkedcars (model, posx, posy, posz, heading, color) VALUES (?, ?, ?, ?, ?, ?)', 
        {carModel, carPos.x, carPos.y, carPos.z, carHeading, carColor}, function()
            TriggerClientEvent('chat:addMessage', pid, {
                args = {'Successfully saved ' .. carModel .. ' with color ' .. carColor .. ' at your location.'}
            })
        end)
end) 

--Load cars
RegisterNetEvent('onResourceStart', function(resourceName) 
    
    if(GetCurrentResourceName() ~= resourceName) then
        return
    end
    --print('Resource ' .. resourceName .. ' has started! :)')

    local carTable = MySQL.query.await('SELECT model, posx, posy, posz, heading, color FROM parkedcars')
    local savedCars = 0

    if carTable then
        for i = 1, #carTable do
            local car = carTable[i]
            carModel = car.model
            RequestModel(carModel)

            while not HasModelLoaded(carModel) do
                Wait(10)
            end

            CreateVehicle(carModel, car.x, car.y, car.z, car.heading, true)
            savedCars = savedCars + 1
        end
        print('Loaded ' .. savedCars .. ' from car parking.')
    end
end)

All the data is saved perfectly into the MySQL table, except for the car model.
Instead of ‘adder’ the car is saved as -1216765807.
And when I try to load it [RequestModel(carModel)] i get an error

attempt to call a nil value (global ‘RequestModel’)

If I try to print carModel, I get -1216765807.
Seems like it can’t parse the model from this number.
What am I doing wrong?

The RequestModel function does not exist on the server side and makes no sense. Normally you should be able to create the vehicle without this function, but you will have to find out for yourself if it is displayed correctly on the client.

Thanks, now it works, but the vehicle does not spawn.
I tried printing the parameters (model, positions etc) and it prints out correctly, but the vehicle does not spawn :confused:
Is there a solution to this? How do you create vehicle from server side?

Try CreateVehicleServerSetter - FiveM Natives @ Cfx.re Docs

1 Like

Works perfectly, thank you!

1 Like