Jump to content

[HELP] vehicle-system


Recommended Posts

local connection = exports["mysql"]:getConnection()

function createPremVehicle(thePlayer, command, ...)
    local argus = {...}
	if (#argus < 3) then
	   return  outputChatBox("SYNTAX: /"..command.." [id] [color1] [color2]", thePlayer, 255, 0, 0)
	end
	local vehicleID = tonumber(argus[1])
	local col1 = tonumber(argus[2])
	local col2 = tonumber(argus[3])
	local interior = getElementInterior(thePlayer)
	local dimension = getElementDimension(thePlayer)
	local x, y, z = getElementPosition(thePlayer)
	local r = getPedRotation(thePlayer)
	local owner = getPlayerName(thePlayer)
	x = x + ((math.cos(math.rad(r)))*5)
	y = y + ((math.sin(math.rad(r)))*5)
	local later1 = string.char(math.random(65,90))
	local later2 = string.char(math.random(65,90))
	local plate = later1..later2..math.random(0,9).." "..math.random(1000, 9999)
	local veh = createVehicle(vehicleID, x, y, z, 0, 0, r, plate)
	setElementInterior(veh, interior)
	setElementDimension(veh, dimension)
	setElementData(veh, "owner", owner)
	setElementData(thePlayer, "vehicle", veh)
	setVehicleColor(veh, col1, col2, col1, col2)
	local col = {getVehicleColor(veh, true)}
	local color1 = toJSON({col[1], col[2], col[3]})
	local color2 = toJSON({col[4], col[5], col[6]})
	local color3 = toJSON({col[8], col[8], col[9]})
	local color4 = toJSON({col[10], col[11], col[12]})
	dbExec(connection, "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(vehicleID), tostring(x), tostring(y), tostring(z), "0", "0", tostring(r), tostring(x), tostring(y), tostring(z), "0", "0", tostring(r), tostring(color1), tostring(color2), tostring(color3), tostring(color4), tostring(plate), owner, tostring(dimension), tostring(interior), tostring(dimension), tostring(interior))
	dbQuery(function(queryHandler)
	    local results = dbPoll(queryHandler, 0)
		local vehicle = results[1]
		setElementData(veh, "id", vehicle.id)
	end, connection, "SELECT id FROM vehicles ORDER BY id DESC LIMIT 1")
end
addCommandHandler("makeveh", createPremVehicle, false, false)

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

addEventHandler("onResourceStart", resourceRoot, function()
    dbQuery(loadAllVehicle, connection, "SELECT * FROM vehicles")
end)

addEventHandler("onResourceStop", resourceRoot, function()
    local vehicles = getElementsByType("vehicle")
    for index, vehicle in ipairs(vehicles) do
	    local id = getElementData(vehicle, "id")
		print("the id is "..id)
	    local x, y, z = getElementPosition(vehicle)
		print("the x,y,z is "..x..","..y..","..z)
		local rx, ry, rz = getElementRotation(vehicle)
		print("the rotx,roty,rotz is "..rx..","..ry..","..rz)
		local dimension = getElementDimension(vehicle)
		print("the dimension is "..dimension)
		local interior = getElementInterior(vehicle)
		print("the interior is "..interior)
		local engine = getElementData(vehicle, "engine")
		print("the engine is "..engine)
		local locked = isVehicleLocked(vehicle) and 1  or 0
		print("the locked is "..locked)
		local lights = getVehicleOverrideLights(vehicle)
		print("the lights is "..lights)
		local sirens = getVehicleSirensOn(vehicle) and 1  or 0
		print("the sirens is "..sirens)
		local paintJob = getVehiclePaintjob(vehicle)
		print("the paintjob is "..paintJob)
		local hp = getElementHealth(vehicle)
		print("the health is "..hp)
		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[8], col[8], col[9]})
	    local color4 = toJSON({col[10], col[11], col[12]})
		print("the color1 is "..color1)
		print("the color2 is "..color2)
		print("the color3 is "..color3)
		print("the color4 is "..color4)
		local wheel1, wheel2, wheel3, wheel4 = getVehicleWheelStates(vehicle)
		local upgrade0 = getVehicleUpgradeOnSlot(vehicle, 0)
		local upgrade1 = getVehicleUpgradeOnSlot(vehicle, 1)
		local upgrade2 = getVehicleUpgradeOnSlot(vehicle, 2)
		local upgrade3 = getVehicleUpgradeOnSlot(vehicle, 3)
		local upgrade4 = getVehicleUpgradeOnSlot(vehicle, 4)
		local upgrade5 = getVehicleUpgradeOnSlot(vehicle, 5)
		local upgrade6 = getVehicleUpgradeOnSlot(vehicle, 6)
		local upgrade7 = getVehicleUpgradeOnSlot(vehicle, 7)
		local upgrade8 = getVehicleUpgradeOnSlot(vehicle, 8)
		local upgrade9 = getVehicleUpgradeOnSlot(vehicle, 9)
		local upgrade10 = getVehicleUpgradeOnSlot(vehicle, 10)
		local upgrade11 = getVehicleUpgradeOnSlot(vehicle, 11)
		local upgrade12 = getVehicleUpgradeOnSlot(vehicle, 12)
		local upgrade13 = getVehicleUpgradeOnSlot(vehicle, 13)
		local upgrade14 = getVehicleUpgradeOnSlot(vehicle, 14)
		local upgrade15 = getVehicleUpgradeOnSlot(vehicle, 15)
		local upgrade16 = getVehicleUpgradeOnSlot(vehicle, 16)
		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 headligh1, headligh2, headligh3 = getVehicleHeadLightColor(vehicle)
		local upgrades = toJSON({upgrade0, upgrade1, upgrade2, upgrade3, upgrade4, upgrade5, upgrade6, upgrade8, upgrade9, upgrade10, upgrade11, upgrade12, upgrade13, upgrade14, upgrade15, upgrade16})
		local wheelStates = toJSON({wheel1, wheel2, wheel3, wheel4})
		local panelStates = toJSON({panel0, panel1, panel2, panel3, panel4, panel5, panel6})
		local doorStates = toJSON({door0, door1, door2, door3, door4, door5})
		local headlights = toJSON({headligh1, headligh2, headligh3})
		print("the upgrades is "..upgrades)
	    print("the wheelStates is "..wheelStates)
	    print("the panelStates is "..panelStates)
	    print("the doorStates is "..doorStates)
	    print("the headlights is "..headlights)
		local bool = dbExec(connection, "UPDATE vehicles SET currx=?, curry=?, currz=?, currrx=?, currry=?, currrz=?, engine=?, locked=?, lights=?, sirens=?, paintjob=?, hp=?,color1=?, color2=?, color3=?, color4=?, currdimension=?, currinterior=?, upgrades=?, wheelStates=?, panelStates=?, doorStates=?, headlights=? WHERE id=?", tostring(x), tostring(y), tostring(z),tostring(rx), tostring(ry), tostring(rz), engine, tostring(locked), tostring(sirens), tostring(paintJob), tostring(hp), tostring(color1), tostring(color2), tostring(color3), tostring(color4), tostring(dimension), tostring(interior), tostring(upgrades), tostring(wheelStates), tostring(panelStates), tostring(doorStates), tostring(headlights), id)
		print("the result is "..bool)
	end
end)

addEventHandler("onVehicleExit", root, function()
    local id = getElementData(source, "id")
	print("the id is "..id)
	local x, y, z = getElementPosition(source)
	print("the x,y,z is "..x..","..y..","..z)
	local rx, ry, rz = getElementRotation(source)
	print("the rotx,roty,rotz is "..rx..","..ry..","..rz)
	local dimension = getElementDimension(source)
	print("the dimension is "..dimension)
	local interior = getElementInterior(source)
	print("the interior is "..interior)
	local engine = getElementData(source, "engine")
	print("the engine is "..engine)
	local locked = isVehicleLocked(source) and 1  or 0
	print("the locked is "..locked)
	local lights = getVehicleOverrideLights(source)
	print("the lights is "..lights)
	local sirens = getVehicleSirensOn(source) and 1  or 0
	print("the sirens is "..sirens)
	local paintJob = getVehiclePaintjob(source)
	print("the paintjob is "..paintJob)
	local hp = getElementHealth(source)
	print("the health is "..hp)
	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[8], col[8], col[9]})
	local color4 = toJSON({col[10], col[11], col[12]})
    print("the color1 is "..color1)
	print("the color2 is "..color2)
	print("the color3 is "..color3)
	print("the color4 is "..color4)
    local wheel1, wheel2, wheel3, wheel4 = getVehicleWheelStates(source)
	local upgrade0 = getVehicleUpgradeOnSlot(source, 0)
	local upgrade1 = getVehicleUpgradeOnSlot(source, 1)
	local upgrade2 = getVehicleUpgradeOnSlot(source, 2)
	local upgrade3 = getVehicleUpgradeOnSlot(source, 3)
	local upgrade4 = getVehicleUpgradeOnSlot(source, 4)
	local upgrade5 = getVehicleUpgradeOnSlot(source, 5)
	local upgrade6 = getVehicleUpgradeOnSlot(source, 6)
	local upgrade7 = getVehicleUpgradeOnSlot(source, 7)
	local upgrade8 = getVehicleUpgradeOnSlot(source, 8)
	local upgrade9 = getVehicleUpgradeOnSlot(source, 9)
	local upgrade10 = getVehicleUpgradeOnSlot(source, 10)
	local upgrade11 = getVehicleUpgradeOnSlot(source, 11)
	local upgrade12 = getVehicleUpgradeOnSlot(source, 12)
	local upgrade13 = getVehicleUpgradeOnSlot(source, 13)
	local upgrade14 = getVehicleUpgradeOnSlot(source, 14)
	local upgrade15 = getVehicleUpgradeOnSlot(source, 15)
	local upgrade16 = getVehicleUpgradeOnSlot(source, 16)
	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 headligh1, headligh2, headligh3 = getVehicleHeadLightColor(source)
	local upgrades = toJSON({upgrade0, upgrade1, upgrade2, upgrade3, upgrade4, upgrade5, upgrade6, upgrade8, upgrade9, upgrade10, upgrade11, upgrade12, upgrade13, upgrade14, upgrade15, upgrade16})
	local wheelStates = toJSON({wheel1, wheel2, wheel3, wheel4})
	local panelStates = toJSON({panel0, panel1, panel2, panel3, panel4, panel5, panel6})
	local doorStates = toJSON({door0, door1, door2, door3, door4, door5})
	local headlights = toJSON({headligh1, headligh2, headligh3})
	print("the upgrades is "..upgrades)
	print("the wheelStates is "..wheelStates)
	print("the panelStates is "..panelStates)
	print("the doorStates is "..doorStates)
	print("the headlights is "..headlights)
	local bool =  dbExec(connection, "UPDATE vehicles SET currx=?, curry=?, currz=?, currrx=?, currry=?, currrz=?, engine=?, locked=?, lights=?, sirens=?, paintjob=?, hp=?,color1=?, color2=?, color3=?, color4=?, currdimension=?, currinterior=?, upgrades=?, wheelStates=?, panelStates=?, doorStates=?, headlights=? WHERE id=?", tostring(x), tostring(y), tostring(z),tostring(rx), tostring(ry), tostring(rz), engine, tostring(locked), tostring(sirens), tostring(paintJob), tostring(hp), tostring(color1), tostring(color2), tostring(color3), tostring(color4), tostring(dimension), tostring(interior), tostring(upgrades), tostring(wheelStates), tostring(panelStates), tostring(doorStates), tostring(headlights), id)
	print("the result is "..bool)
end)

function toggleEngine(thePlayer)
    if isPedInVehicle(thePlayer) then
	   local veh = getPedOccupiedVehicle(thePlayer)
	   if getPedOccupiedVehicleSeat(thePlayer) == 0 then
	     local owner = getElementData(veh, "owner")
		 if owner == getPlayerName(thePlayer) then
	        if getVehicleEngineState(veh) == false then
		       setVehicleEngineState(veh, true)
			   setElementData(veh, "engine", "1")
		    else
		       setVehicleEngineState(veh, false)
			   setElementData(veh, "engine", "0")
		    end
		 else
		    outputChatBox("You are not the owner!", thePlayer, 255, 0, 0)
		 end
	   else
	     outputChatBox("You are not in driver seat!", thePlayer, 255, 0, 0)
	   end
	else
	  outputChatBox("You are not in a vehicle!", thePlayer, 255, 0 , 0)
	end
end
addCommandHandler("engine", toggleEngine, false, false)

function toggleLights(thePlayer)
    if isPedInVehicle(thePlayer) then
	   local veh = getPedOccupiedVehicle(thePlayer)
	   if getPedOccupiedVehicleSeat(thePlayer) == 0 then
	     if getVehicleOverrideLights(veh) ~= 2 then
		    setVehicleOverrideLights(veh, 2)
		 else
		    setVehicleOverrideLights(veh, 1)
		 end
	   else
	      outputChatBox("You are not in driver seat!", thePlayer, 255, 0, 0)
	   end
	else
	  outputChatBox("You are not in a vehicle!", thePlayer, 255, 0 , 0)
	end
end
addCommandHandler("lights", toggleLights, false, false)

function toggleLock(thePlayer)
   if isPedInVehicle(thePlayer) then
      local veh = getPedOccupiedVehicle(thePlayer)
	  local owner = getElementData(veh, "owner")
	  if getPedOccupiedVehicleSeat(thePlayer) == 0 then
	     if owner == getPlayerName(thePlayer) then
		    if not isVehicleLocked(veh) then
			   setVehicleLocked(veh, true)
			else
			   setVehicleLocked(veh, false)
			end
		 else
		   outputChatBox("You are not the owner!", thePlayer, 255, 0 , 0)
		 end
	  else
	     outputChatBox("You are not in driver seat!", thePlayer, 255, 0, 0)
	  end
   else
     outputChatBox("You are not in a vehicle!", thePlayer, 255, 0, 0)
   end
end
addCommandHandler("carlock", toggleLock, false, false)

addEventHandler("onVehicleEnter", root, function()
    if getElementData(source, "engine") == 1 then
	   setVehicleEngineState(source, true)
	else
	  setVehicleEngineState(source, false)
	end
end)

addEventHandler("onVehicleStartEnter", root, function(thePlayer)
    local owner = getElementData(source, "owner")
	if isVehicleLocked(source)and not owner == getPlayerName(thePlayer) then
	   cancelEvent()
	end
end)

error 92 attempt to conculate id (boolen value)

warning  56 setVehicleLocked (arugment 2 expect boolen got number 0)

warning 57 setVehicleSirensOn(argument 2 expect boolen got number 0)

when i restart the resource and enter the car try to turn on the enine say i a m not the owner but i am 

Edited by Dimos7
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...