Jump to content

[HELP] fromJSON


Dimos7

Recommended Posts

local conection = exports.sql:getConnection()

function makePlayerVehicle(player, command, model, modelName)
    if not model or modelName then
	   return outputChatBox("SYNATX:/ "..command.."[model id]", player, 255, 0, 0)
	end
	local x, y, z = getElementPosition(player)
	local rx, ry, rz = getElementRotation(player)
	local letter1 = string.char(math.random(65,90))
	local letter2 = string.char(math.random(65,90))
	local plate = letter1 .. letter2 .. math.random(0, 9) .. " " .. math.random(1000, 9999)
	local interior = getElementInterior(player)
	local dimension = getElementDimension(player)
	local owner = getPlayerName(player)
	local col = { getVehicleColor(vehicleOb, true)}
	local color1 = toJSON({col[1], col[2], col[3]})
	local color2 = toJSON({col[4], col[5], col[6]})
	local color3 = toJSON({col[7], col[8], col[9]})
	local color4 = toJSON({col[10], col[11], col[12]})
	y = y + 5
	rz = rz + 90 
	dbExec(conection,"INSERT INTO vehicles (model, x, y, z, rotx, roty, rotz, currx, curry, currz, currrx, currry, currrz, color1, color2, color3, color4, plate, owner, dimension, interior, currdimension, currinterior) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",tostring(model),tostring(x),tostring(y),tostring(z),tostring(rx),tostring(ry),tostring(rz),tostring(x), tostring(y), tostring(z), tostring(rx), tostring(ry), tostring(rz), tostring(color1), tostring(color2), tostring(color3), tostring(color4), tostring(plate), owner, tostring(dimension), tostring(interior), tostring(dimension), tostring(interior))
	local vehicleOb = createVehicle(model, x, y, z, rx, ry, rz, plate)
	setElementInterior(vehicleOb, interior)
	setElementDimension(vehicleob, dimension)
	setElementData(player, "vehicle", vehicleOb)
	setElementData(vehicleOb, "owner", owner)
	
	dbQuery(function(queryHandler)
	    local results = dbPoll(queryHandler, 0)
	    local vehicle = results[1]
	    setElementData(vehicleOb, "id", vehicle.id)
	end, conection, "SELECT id FROM vehicles ORDER BY id DESC LIMIT 1")
end
addCommandHandler("makeveh", makePlayerVehicle, false, false)


function loadAllVehicles(queryHandler)
    local results = dbPoll(queryHandler, 0)
	for index, vehicle in ipairs(results) do
	    local vehicleOb = createVehicle(vehicle.model, vehicle.currx, vehicle.curry, vehicle.currz, vehicle.currrx, vehicle.currry, vehicle.currrz, vehicle.plate)
		setElementData(vehicleOb, "id", vehicle.id)
		local color1 = fromJSON(vehicle.color1)
		local color2 = fromJSON(vehicle.color2)
		local color3 = fromJSON(vehicle.color3)
		local color4 = fromJSON(vehicle.color4)
		local headlightColors = fromJSON(vehicle.headlights)
		setVehicleColor(vehicle, color1[1], color1[2], color1[3], color2[1], color2[2], color2[3], color3[1], color3[2], color3[3], color4[1], color4[2], color4[3])
		setVehicleHeadLightColor(vehicle, headlightColors[1], headlightColors[2], headlightColors[3]) 
		setElementHealth(vehicle, vehicle.hp)
		setElementDimension(vehicle, vehicle.currdimension)
		setElementInterior(vehicle, vehicle.currinterior)
		setElementData(vehicle, "owner", vehicle.owner)
		setElementData(vehicle, "engine", vehicle.engine)
		setVehicleOverrideLights(vehicle, vehicle.lights)
		setVehicleLocked(vehicle, vehicle.locked)
		setVehicleSirensOn(vehicle, vehicle.sirens)
		if vehicle.paintjob ~= 0 then
		   setVehiclePaintjob(vehicle, vehicle.paintjob)
		end
		local upgrades = fromJSON(vehicle.upgrades)
		for slot, upgrade in ipairs(upgrades) do
		    if upgrade and tonumber(upgrade) > 0 then
			   addVehicleUpgrade(vehicle, upgrade)
			end
		end
		local panelStates = fromJSON(vehicle.panelStates)
		for panel, state in ipairs(panelStates) do
		    setVehiclePanelState(vehicle, panel-1, tonumber(state) or 0) 
		end
		local doorStates = fromJSON(vehicle.doorStates)
		for door, state in ipairs(doorStates) do
		    setVehicleDoorState(vehicle, door-1, tonumber(state) or 0)
		end
		local wheelStates = fromJSON(vehicle.wheelStates)
		for wheel, state in ipairs(wheelStates) do
		    setVehicleWheelStates(vehicle, wheel-1, tonumber(state) or 0)
		end
	end
end

addEventHandler("onResourceStart", resourceRoot, function()
    dbQuery(loadAllVehicles,conection, "SELECT * FROM vehicles")
end)

addEventHandler("onResourceStop", resourceRoot, function()
    local vehicles = getElementsByType("vehicle")
	for index, vehicle in ipairs(vehicles) do
	    local id = getElementData(vehicle, "id")
		local x, y, z = getElementPosition(vehicle)
		local rx, ry, rz = getElementRotation(vehicle)
		local dimension = getElementDimension(vehicle)
		local interior = getElementInterior(vehicle)
		local hp = getElementHealth(vehicle)
		local col = { getVehicleColor(vehicle, true)}
		local color1 = toJSON( {col[1], col[2], col[3]})
		local color2 = toJSON( {col[4], col[5], col[6]})
		local color3 = toJSON({col[7], col[8], col[9]})
	    local color4 = toJSON({col[10], col[11], col[12]})
		local paintjob = getVehiclePaintjob(vehicle)
		local engine = getElementData(vehicle, "engine")
		local locked = isVehicleLocked(vehicle) and 1 or 0
		local lights = getVehicleOverrideLights(vehicle)
		local sirens = getVehicleSirensOn(vehicle) and 1 or 0
		local panel0 = getVehiclePanelState(vehicle, 0)
		local panel1 = getVehiclePanelState(vehicle, 1)
		local panel2 = getVehiclePanelState(vehicle, 2)
		local panel3 = getVehiclePanelState(vehicle, 3)
		local panel4 = getVehiclePanelState(vehicle, 4)
		local panel5 = getVehiclePanelState(vehicle, 5)
		local panel6 = getVehiclePanelState(vehicle, 6)
		local door0 = getVehicleDoorState(vehicle, 0)
		local door1 = getVehicleDoorState(vehicle, 1)
		local door2 = getVehicleDoorState(vehicle, 2)
		local door3 = getVehicleDoorState(vehicle, 3)
		local door4 = getVehicleDoorState(vehicle, 4)
		local door5 = getVehicleDoorState(vehicle, 5)
		local wheel1, wheel2, wheel3, wheel4 = getVehicleWheelStates(vehicle)
		local hcol1, hcol2, hcol3 = getVehicleHeadLightColor(vehicle)
		local panelStates = toJSON({panel0, panel1, panel2, panel3, panel4, panel5, panel6})
		local doorStates = toJSON ({door0,door1, door2, door3, door4, door5})
		local wheelStates = toJSON({wheel1, wheel2, wheel3, wheel4})
		local headlights = toJSON({hcol1, hcol2, hcol3})
		dbExec(conection,"UPDATE vehicles SET currx=?, curry=?, currz=?, currrx=?, currry=?, currrz=?, engine=?, locked=?, lights=?, sirens=?, paintob=?, hp=?,  color1=?, color2=?,, color3=?, color4=?, currdimension =?, currinterior=?, panelStates=?, doorStates=?, wheelStates=?,headlights=?,  WHERE id=?",tostring(x),tostring(y),tostring(z),tostring(rx),tostring(ry),tostring(rz), tostring(engine), tostring(lights), tostring(locked), tostring(sirens), tostring(paintjob),tostring(hp),tostring(color1), tostring(color2), tostring(color3), tostring(color4), tostring(dimension), tostring(interior), tostring(panelStates), tostring(doorStates), tostring(wheelStates), tostring(headlights), id)
	end
end)

addEventHandler("onVehicleExit", root, function()
    local id = getElementData(source, "id")
	local x, y, z = getElementPosition(source)
	local rx, ry, rz = getElementRotation(source)
	local dimension = getElementDimension(source)
	local interior = getElementInterior(source)
	local hp = getElementHealth(source)
	local col = {getVehicleColor(source, true)}
	local color1 = toJSON({col[1], col[2], col[3]})
	local color2 = toJSON({col[4], col[5], col[6]})
	local color3 = toJSON({col[7], col[8], col[9]})
	local color4 = toJSON({col[10], col[11], col[12]})
	local paintjob = getVehiclePaintjob(source)
	local engine = getElementData(source, "engine")
	local locked = isVehicleLocked(source) and 1 or 0
	local lights = getVehicleOverrideLights(source)
	local sirens = getVehicleSirensOn(source) and 1 or 0
	local panel0 = getVehiclePanelState(source, 0)
	local panel1 = getVehiclePanelState(source, 1)
	local panel2 = getVehiclePanelState(source, 2)
	local panel3 = getVehiclePanelState(source, 3)
	local panel4 = getVehiclePanelState(source, 4)
	local panel5 = getVehiclePanelState(source, 5)
	local panel6 = getVehiclePanelState(source, 6)
	local door0 = getVehicleDoorState(source, 0)
	local door1 = getVehicleDoorState(source, 1)
	local door2 = getVehicleDoorState(source, 2)
	local door3 = getVehicleDoorState(source, 3)
	local door4 = getVehicleDoorState(source, 4)
	local door5 = getVehicleDoorState(source, 5)
    local wheel1, wheel2, wheel3, wheel4 = getVehicleWheelStates(source)
	local hcol1, hcol2, hcol3 = getVehicleHeadLightColor(source)
	local panelStates = toJSON( {panel0, panel1, panel2, panel3, panel4, panel5, panel6})
	local doorStates = toJSON ( {door0,door1, door2, door3, door4, door5})
	local wheelStates = toJSON({wheel1, wheel2, wheel3, wheel4})
	local headlights = toJSON({hcol1, hcol2, hcol3})
	dbExec(conection,"UPDATE vehicles SET currx=?, curry=?, currz=?, currrx=?, currry=?, currrz=?, engine=?, locked=?, lights=?, sirens=?, paintob=?, hp=?,  color1=?, color2=?, color3=?, , color4=? currdimension =?, currinterior=?, panelStates=?, doorStates=?, wheelStates=?,headlights=?,  WHERE id=?",tostring(x),tostring(y),tostring(z),tostring(rx),tostring(ry),tostring(rz), tostring(engine), tostring(lights), tostring(locked), tostring(sirens), tostring(paintjob),tostring(hp),tostring(color1), tostring(color2), tostring(color3), tostring(color4), tostring(dimension), tostring(interior), tostring(panelStates), tostring(doorStates), tostring(wheelStates), tostring(headlights), id)
end)

fromJSOn line 45 expect string at argument 1, got boolen same error at line 46. attempt to index color1 a nil value

Edited by Dimos7
Link to comment
local conection = exports.sql:getConnection()

function makePlayerVehicle(player, command, model, modelName)
    if not model or modelName then
	   return outputChatBox("SYNATX:/ "..command.."[model id]", player, 255, 0, 0)
	end
	local x, y, z = getElementPosition(player)
	local rx, ry, rz = getElementRotation(player)
	local letter1 = string.char(math.random(65,90))
	local letter2 = string.char(math.random(65,90))
	local plate = letter1 .. letter2 .. math.random(0, 9) .. " " .. math.random(1000, 9999)
	local interior = getElementInterior(player)
	local dimension = getElementDimension(player)
	local owner = getPlayerName(player)
	y = y + 5
	rz = rz + 90 
	dbExec(conection,"INSERT INTO vehicles (model, x, y, z, rotx, roty, rotz, currx, curry, currz, currrx, currry, currrz, color1, color2, color3, color4, plate, owner, dimension, interior, currdimension, currinterior) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",tostring(model),tostring(x),tostring(y),tostring(z),tostring(rx),tostring(ry),tostring(rz),tostring(x), tostring(y), tostring(z), tostring(rx), tostring(ry), tostring(rz), tostring(color1), tostring(color2), tostring(color3), tostring(color4), tostring(plate), owner, tostring(dimension), tostring(interior), tostring(dimension), tostring(interior))
	local vehicleOb = createVehicle(model, x, y, z, rx, ry, rz, plate)
	local col = { getVehicleColor(vehicleOb, true)}
	local color1 = toJSON({col[1], col[2], col[3]})
	local color2 = toJSON({col[4], col[5], col[6]})
	local color3 = toJSON({col[7], col[8], col[9]})
	local color4 = toJSON({col[10], col[11], col[12]})
	setElementInterior(vehicleOb, interior)
	setElementDimension(vehicleob, dimension)
	setElementData(player, "vehicle", vehicleOb)
	setElementData(vehicleOb, "owner", owner)
	
	dbQuery(function(queryHandler)
	    local results = dbPoll(queryHandler, 0)
	    local vehicle = results[1]
	    setElementData(vehicleOb, "id", vehicle.id)
	end, conection, "SELECT id FROM vehicles ORDER BY id DESC LIMIT 1")
end
addCommandHandler("makeveh", makePlayerVehicle, false, false)


function loadAllVehicles(queryHandler)
    local results = dbPoll(queryHandler, 0)
	for index, vehicle in ipairs(results) do
	    local vehicleOb = createVehicle(vehicle.model, vehicle.currx, vehicle.curry, vehicle.currz, vehicle.currrx, vehicle.currry, vehicle.currrz, vehicle.plate)
		setElementData(vehicleOb, "id", vehicle.id)
		local color1 = fromJSON(vehicle.color1)
		local color2 = fromJSON(vehicle.color2)
		local color3 = fromJSON(vehicle.color3)
		local color4 = fromJSON(vehicle.color4)
		local headlightColors = fromJSON(vehicle.headlights)
		setVehicleColor(vehicle, color1[1], color1[2], color1[3], color2[1], color2[2], color2[3], color3[1], color3[2], color3[3], color4[1], color4[2], color4[3])
		setVehicleHeadLightColor(vehicle, headlightColors[1], headlightColors[2], headlightColors[3]) 
		setElementHealth(vehicle, vehicle.hp)
		setElementDimension(vehicle, vehicle.currdimension)
		setElementInterior(vehicle, vehicle.currinterior)
		setElementData(vehicle, "owner", vehicle.owner)
		setElementData(vehicle, "engine", vehicle.engine)
		setVehicleOverrideLights(vehicle, vehicle.lights)
		setVehicleLocked(vehicle, vehicle.locked)
		setVehicleSirensOn(vehicle, vehicle.sirens)
		if vehicle.paintjob ~= 0 then
		   setVehiclePaintjob(vehicle, vehicle.paintjob)
		end
		local upgrades = fromJSON(vehicle.upgrades)
		for slot, upgrade in ipairs(upgrades) do
		    if upgrade and tonumber(upgrade) > 0 then
			   addVehicleUpgrade(vehicle, upgrade)
			end
		end
		local panelStates = fromJSON(vehicle.panelStates)
		for panel, state in ipairs(panelStates) do
		    setVehiclePanelState(vehicle, panel-1, tonumber(state) or 0) 
		end
		local doorStates = fromJSON(vehicle.doorStates)
		for door, state in ipairs(doorStates) do
		    setVehicleDoorState(vehicle, door-1, tonumber(state) or 0)
		end
		local wheelStates = fromJSON(vehicle.wheelStates)
		for wheel, state in ipairs(wheelStates) do
		    setVehicleWheelStates(vehicle, wheel-1, tonumber(state) or 0)
		end
	end
end

addEventHandler("onResourceStart", resourceRoot, function()
    dbQuery(loadAllVehicles,conection, "SELECT * FROM vehicles")
end)

addEventHandler("onResourceStop", resourceRoot, function()
    local vehicles = getElementsByType("vehicle")
	for index, vehicle in ipairs(vehicles) do
	    local id = getElementData(vehicle, "id")
		local x, y, z = getElementPosition(vehicle)
		local rx, ry, rz = getElementRotation(vehicle)
		local dimension = getElementDimension(vehicle)
		local interior = getElementInterior(vehicle)
		local hp = getElementHealth(vehicle)
		local col = { getVehicleColor(vehicle, true)}
		local color1 = toJSON( {col[1], col[2], col[3]})
		local color2 = toJSON( {col[4], col[5], col[6]})
		local color3 = toJSON({col[7], col[8], col[9]})
	    local color4 = toJSON({col[10], col[11], col[12]})
		local paintjob = getVehiclePaintjob(vehicle)
		local engine = getElementData(vehicle, "engine")
		local locked = isVehicleLocked(vehicle) and 1 or 0
		local lights = getVehicleOverrideLights(vehicle)
		local sirens = getVehicleSirensOn(vehicle) and 1 or 0
		local panel0 = getVehiclePanelState(vehicle, 0)
		local panel1 = getVehiclePanelState(vehicle, 1)
		local panel2 = getVehiclePanelState(vehicle, 2)
		local panel3 = getVehiclePanelState(vehicle, 3)
		local panel4 = getVehiclePanelState(vehicle, 4)
		local panel5 = getVehiclePanelState(vehicle, 5)
		local panel6 = getVehiclePanelState(vehicle, 6)
		local door0 = getVehicleDoorState(vehicle, 0)
		local door1 = getVehicleDoorState(vehicle, 1)
		local door2 = getVehicleDoorState(vehicle, 2)
		local door3 = getVehicleDoorState(vehicle, 3)
		local door4 = getVehicleDoorState(vehicle, 4)
		local door5 = getVehicleDoorState(vehicle, 5)
		local wheel1, wheel2, wheel3, wheel4 = getVehicleWheelStates(vehicle)
		local hcol1, hcol2, hcol3 = getVehicleHeadLightColor(vehicle)
		local panelStates = toJSON({panel0, panel1, panel2, panel3, panel4, panel5, panel6})
		local doorStates = toJSON ({door0,door1, door2, door3, door4, door5})
		local wheelStates = toJSON({wheel1, wheel2, wheel3, wheel4})
		local headlights = toJSON({hcol1, hcol2, hcol3})
		dbExec(conection,"UPDATE vehicles SET currx=?, curry=?, currz=?, currrx=?, currry=?, currrz=?, engine=?, locked=?, lights=?, sirens=?, paintob=?, hp=?,  color1=?, color2=?, color3=?, color4=?, currdimension =?, currinterior=?, panelStates=?, doorStates=?, wheelStates=?, headlights=?,  WHERE id=?",tostring(x),tostring(y),tostring(z),tostring(rx),tostring(ry),tostring(rz), tostring(engine), tostring(lights), tostring(locked), tostring(sirens), tostring(paintjob),tostring(hp),tostring(color1), tostring(color2), tostring(color3), tostring(color4), tostring(dimension), tostring(interior), tostring(panelStates), tostring(doorStates), tostring(wheelStates), tostring(headlights), id)
	end
end)

addEventHandler("onVehicleExit", root, function()
    local id = getElementData(source, "id")
	local x, y, z = getElementPosition(source)
	local rx, ry, rz = getElementRotation(source)
	local dimension = getElementDimension(source)
	local interior = getElementInterior(source)
	local hp = getElementHealth(source)
	local col = {getVehicleColor(source, true)}
	local color1 = toJSON({col[1], col[2], col[3]})
	local color2 = toJSON({col[4], col[5], col[6]})
	local color3 = toJSON({col[7], col[8], col[9]})
	local color4 = toJSON({col[10], col[11], col[12]})
	local paintjob = getVehiclePaintjob(source)
	local engine = getElementData(source, "engine")
	local locked = isVehicleLocked(source) and 1 or 0
	local lights = getVehicleOverrideLights(source)
	local sirens = getVehicleSirensOn(source) and 1 or 0
	local panel0 = getVehiclePanelState(source, 0)
	local panel1 = getVehiclePanelState(source, 1)
	local panel2 = getVehiclePanelState(source, 2)
	local panel3 = getVehiclePanelState(source, 3)
	local panel4 = getVehiclePanelState(source, 4)
	local panel5 = getVehiclePanelState(source, 5)
	local panel6 = getVehiclePanelState(source, 6)
	local door0 = getVehicleDoorState(source, 0)
	local door1 = getVehicleDoorState(source, 1)
	local door2 = getVehicleDoorState(source, 2)
	local door3 = getVehicleDoorState(source, 3)
	local door4 = getVehicleDoorState(source, 4)
	local door5 = getVehicleDoorState(source, 5)
    local wheel1, wheel2, wheel3, wheel4 = getVehicleWheelStates(source)
	local hcol1, hcol2, hcol3 = getVehicleHeadLightColor(source)
	local panelStates = toJSON( {panel0, panel1, panel2, panel3, panel4, panel5, panel6})
	local doorStates = toJSON ( {door0,door1, door2, door3, door4, door5})
	local wheelStates = toJSON({wheel1, wheel2, wheel3, wheel4})
	local headlights = toJSON({hcol1, hcol2, hcol3})
	dbExec(conection,"UPDATE vehicles SET currx=?, curry=?, currz=?, currrx=?, currry=?, currrz=?, engine=?, locked=?, lights=?, sirens=?, paintob=?, hp=?,  color1=?, color2=?, color3=?, , color4=? currdimension =?, currinterior=?, panelStates=?, doorStates=?, wheelStates=?,headlights=?,  WHERE id=?",tostring(x),tostring(y),tostring(z),tostring(rx),tostring(ry),tostring(rz), tostring(engine), tostring(lights), tostring(locked), tostring(sirens), tostring(paintjob),tostring(hp),tostring(color1), tostring(color2), tostring(color3), tostring(color4), tostring(dimension), tostring(interior), tostring(panelStates), tostring(doorStates), tostring(wheelStates), tostring(headlights), id)
end)

seems it get nil when the object created idk why  also i ahve a error now at sytax of query at 124 near where

Link to comment
  • Discord Moderators

Could you give us a picture about the SQL Table structure?Maybe, it doesnt even save the values, due to invalid VAR lenght in SQL.

Try outputting the table values with a simple 

inspect()

function.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...