Hello guys I just wanna know what I messed up in my script I'm facing this error for hours!!
ERROR: vehicles/vehicles.Lua:35: attempt to call global 'SetElementData' (a nil value)
and here is my whole code:
function createVehicleForPlayer(player, command, model)
local db = exports.db:getConnection()
local x, y, z = getElementPosition(player)
y = y + 5
dbExec(db, 'INSERT INTO vehicles (model, x, y, z) VALUES (?, ?, ?, ?)', model, x, y, z)
local vehicleObject = createVehicle(model, x, y, z)
outputChatBox ( "Command Syntax: /makeveh [module id]", getRootElement(), 255, 255, 0, true )
dbQuery(function (queryHandle)
local results = dbPoll(queryHandle, 0)
local result = results[1]
SetElementData(vehicleObject, "id", vehicle.id)
end, db, 'SELECT id FROM vehicles ORDER BY id DESC LIMIT 1')
end
addCommandHandler('makeveh', createVehicleForPlayer, false, false)
function loadAllVehicles(queryHandle)
local results = dbPoll(queryHandle, 0)
for index, vehicle in pairs(results) do
local vehicleObject = createVehicle(vehicle.model, vehicle.x, vehicle.y, vehicle.z)
SetElementData(vehicleObject, "id", vehicle.id)
end
end
addEventHandler('onResourceStart', resourceRoot, function()
local db = exports.db:getConnection()
dbQuery(loadAllVehicles, db, 'SELECT * FROM vehicles')
end)
addEventHandler('onResourceStop', resourceRoot, function()
local vehicles = getElementByType('vehicle')
for index, vehicle in pairs(vehicles) do
local id = getElementData(vehicle, 'id')
local x, y, z = getElementPosition(vehicle)
dbExec(db, 'UPDATE vehicles SET x = ?, y = ?, z = ? WHERE id = ?', x, y, z, id)
end
end)