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?