Jump to content

Ayuda con este script setElementData boolean


Xbom

Recommended Posts

Hola a todos alguien me podría ayudar con este script, no se ejecuta me da error boolean en setElementData, soy nuevo haciendo script con lua y aún no sé cómo funciona del todo el setElementData

 

local number1
 
function SpawnVehPos(player, command , model)

    local db = exports.db:getConnection() 
    local x,y,z = getElementPosition(player)  
    x =x + 2
    dbExec(db, 'INSERT INTO vehicles ( model , x ,  y ,  z) VALUES ( ?, ?, ?, ?) ', model, x, y, z)

    createVehicle(model,x,y,z) 
    
    number1 = tonumber(model) 

    if (number1 <400 or number1 >611) then    
            outputChatBox('ERROR') 
        else
            outputChatBox('Car created ')
    end

    if (not db) then
        outputDebugString("Error: Failed to establish connection to the MySQL database server")
    else
        outputDebugString("Success: Connected to the MySQL database server")
    end
    
end



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


addCommandHandler('car',SpawnVehPos) 


addEventHandler('onResourceStart',resourceRoot,function()
    
    local db = exports.db:getConnection()
     
    dbQuery(loadAllVehicles, db, 'SELECT * FROM vehicles')

end)




 addEventHandler('onResourceStop',resourceRoot,function()

    local vehicles = getElementsByType('vehicle') 
    
    local db = exports.db:getConnection()

    
 	 for index , i 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)

 

Link to comment
  • 3 weeks later...
addEventHandler( "onResourceStart", resourceRoot,
	function( )
		-- load all vehicles
		local result = exports.sql:query_assoc( "SELECT * FROM vehicles ORDER BY vehicleID ASC" )
		if result then
			for key, data in ipairs( result ) do
				if data.inactivo == 0 and data.cepo == 0 then
				local vehicle = createVehicle( data.model, data.posX, data.posY, data.posZ, data.rotX, data.rotY, data.rotZ )
				setElementFrozen(vehicle, true)
				setElementAlpha(vehicle, 127)
				setElementCollisionsEnabled(vehicle, false)
				setTimer(setElementCollisionsEnabled, 2000, 1, vehicle, true)
				setTimer(setElementAlpha, 2000, 1, vehicle, 255)
				-- tables for ID -> vehicle and vehicle -> data
				vehicleIDs[ data.vehicleID ] = vehicle
				vehicles[ vehicle ] = { vehicleID = data.vehicleID, respawnInterior = data.respawnInterior, respawnDimension = data.respawnDimension, characterID = data.characterID, engineState = not doesVehicleHaveEngine( vehicle ) or data.engineState == 1, tintedWindows = data.tintedWindows == 1, fuel = data.fuel, km = data.km, pinturas = data.pinturas, motor = data.motor, mejoramotor = data.mejoramotor, frenos = data.frenos, mejorafrenos = data.mejorafrenos, model = data.model }

				setElementHealth( vehicle, data.health )
				if data.health <= 300 then
					vehicles[ vehicle ].engineState = false
				end
				if data.color1 and data.color2 then
					r, g, b = HEX2RGB(data.color1)
					r2, g2, b2 = HEX2RGB(data.color2)
					setVehicleColor( vehicle, r, g, b, r2, g2, b2 )
				end
				if data.color3 then
					r, g, b = HEX2RGB(data.color3)
					setVehicleHeadLightColor ( vehicle, r, g, b )
				end
				setVehiclePlateText(vehicle, tostring(addZero( data.vehicleID, 4 )))
				setVehicleRespawnPosition( vehicle, data.respawnPosX, data.respawnPosY, data.respawnPosZ, data.respawnRotX, data.respawnRotY, data.respawnRotZ )
				setElementInterior( vehicle, data.interior )
				setElementDimension( vehicle, data.dimension )
				if data.alarma == 1 then
					setElementData(vehicle, "havealarm", true)
				end
				setVehicleLocked( vehicle, true )
				setVehicleEngineState( vehicle, false )
				setVehicleOverrideLights( vehicle, data.lightsState + 1 )
				setElementData( vehicle, "fuel", data.fuel )
				if data.cepo == 1 then setVehicleEngineState( vehicle, false ) setElementData( vehicle, "cepo", 1 ) end
				setVehiclePaintjob ( vehicle, data.pinturas )
				setElementData(vehicle, "km", data.km)
				setElementData(vehicle, "idowner", data.characterID)
				setElementData(vehicle, "idveh", data.vehicleID)
				exports.vehicles_auxiliar:applyTunning(vehicle)
				setElementData(vehicle, "model", data.model)
				setElementData(vehicle, "fasemotor", data.fasemotor)
				setElementData(vehicle, "fasefrenos", data.fasefrenos)
				if data.diasLimpio > 0 then
					-- Solicitamos limpiarlo a todos los conectados y además lo guardamos.
					-- Preparado para una reforma futura
					setElementData(vehicle, "limpio", true)
				end
				if data.marchas == 1 then
					setElementData(vehicle, "marchas", 1)
				end
				if data.marchas == 0 then
					setElementData(vehicle, "marchas", 0)
				end
				if data.fasemotor > 0 then
					exports.vehicles_auxiliar:solicitarMejora(vehicle, 1, data.fasemotor)
				end
				if data.fasefrenos > 0 then
					exports.vehicles_auxiliar:solicitarMejora(vehicle, 2, data.fasefrenos)
				end
				end
			end
		end
		
		-- bind a key for everyone
		for key, value in ipairs( getElementsByType( "player" ) ) do
			bindKey( value, "num_3", "down", "lockvehicle" )
			bindKey( value, "num_1", "down", "toggleengine" )
			bindKey( value, "num_2", "down", "togglelights" )
			bindKey( value, "num_0", "down", toggleFreezeStatus )
			bindKey( value, "n", "down", toggleFreezeStatus )
			bindKey( value, "k", "down", "lockvehicle" )
			bindKey( value, "j", "down", "toggleengine" )
			bindKey( value, "l", "down", "togglelights" )
			bindKey( value, "c", "down", "cinturon" )
		end
		
		--
		
		-- Fuel update
		setTimer(
			function( )
				for vehicle, data in pairs( vehicles ) do
					if not isElement( vehicle ) or getElementType( vehicle ) ~= "vehicle" then
						vehicles[ vehicle ] = nil
					elseif data.engineState and data.fuel and not isVehicleEmpty( vehicle ) and doesVehicleHaveEngine( vehicle ) and doesVehicleHaveFuel( vehicle ) and not ( models [ getElementModel ( vehicle ) ] ) then
						local vx, vy, vz = getElementVelocity( vehicle )
						local speed = math.sqrt( vx * vx + vy * vy )
						local loss = ( speed > 0.65 and 2 * speed or speed ) * 0.5 + 0.005
						
						data.fuel = math.max( data.fuel - loss, 0 )
						if math.floor( data.fuel + 0.5 ) ~= getElementData( vehicle, "fuel" ) then
							setElementData( vehicle, "fuel", math.floor( data.fuel + 0.5 ) )
						end
						
						if data.fuel == 0 then
							setVehicleEngineState( vehicle, false )
							setElementData (vehicle, "sinGasolina", true)
							for seat = 0, getVehicleMaxPassengers( vehicle ) do
								local player = getVehicleOccupant( vehicle, seat )
								if player then
									triggerClientEvent( player, "gui:hint", player, "Sin gasolina", "Tu " .. getVehicleName( vehicle ) .. " No arranca sin gasolina!\nPara que no te vuelva a pasar esto recarga cada un tiempo.", 3 )
									setVehicleEngineState( vehicle, false )
								end
							end
						end
					end
				end
			end,
			10000,
			0
		)
		-- KM update
		setTimer(
			function( )
				for vehicle, data in pairs( vehicles ) do
					if not isElement( vehicle ) or getElementType( vehicle ) ~= "vehicle" then
						vehicles[ vehicle ] = nil
					elseif data.engineState and not isVehicleEmpty( vehicle ) and doesVehicleHaveEngine( vehicle ) then
						local vx, vy, vz = getElementVelocity( vehicle )
						local speed = math.sqrt( vx * vx + vy * vy )
						local loss = (( speed > 0.65 and 2 * speed or speed ) * 0.8 + 0.005)*15
						if data.km and data.km > 0 then
							data.km = math.max( data.km + loss, 0 )
						else
							data.km = math.max( loss, 0 )
						end
						if math.floor( data.km ) ~= getElementData( vehicle, "km" ) then
							setElementData( vehicle, "km", math.floor( data.km ) )
						end
						
					end
				end
			end,
			10000,
			0
		)
	end
)
function reloadVehicle(vehicleID)
	if vehicleID then
		if getVehicle(tonumber(vehicleID)) then destroyElement(getVehicle(tonumber(vehicleID))) end
		local data = exports.sql:query_assoc_single("SELECT * FROM vehicles WHERE vehicleID = "..vehicleID)
		if not data then return false end
		if data.inactivo == 0 then
			local vehicle = createVehicle( data.model, data.posX, data.posY, data.posZ, data.rotX, data.rotY, data.rotZ )
			setElementFrozen(vehicle, true)
			setElementAlpha(vehicle, 127)
			setElementCollisionsEnabled(vehicle, false)
			setTimer(setElementCollisionsEnabled, 2000, 1, vehicle, true)
			setTimer(setElementAlpha, 2000, 1, vehicle, 255)
			-- tables for ID -> vehicle and vehicle -> data
			vehicleIDs[ data.vehicleID ] = vehicle
			vehicles[ vehicle ] = { vehicleID = data.vehicleID, respawnInterior = data.respawnInterior, respawnDimension = data.respawnDimension, characterID = data.characterID, engineState = not doesVehicleHaveEngine( vehicle ) or data.engineState == 1, tintedWindows = data.tintedWindows == 1, fuel = data.fuel, km = data.km, pinturas = data.pinturas, motor = data.motor, mejoramotor = data.mejoramotor, frenos = data.frenos, mejorafrenos = data.mejorafrenos, model = data.model }

			setElementHealth( vehicle, data.health )
			if data.health <= 300 then
				vehicles[ vehicle ].engineState = false
			end
			if data.color1 and data.color2 then
				r, g, b = HEX2RGB(data.color1)
				r2, g2, b2 = HEX2RGB(data.color2)
				setVehicleColor( vehicle, r, g, b, r2, g2, b2 )
			end
			if data.color3 then
				r, g, b = HEX2RGB(data.color3)
				setVehicleHeadLightColor ( vehicle, r, g, b )
			end
			setVehiclePlateText(vehicle, tostring(addZero( data.vehicleID, 4 )))
			setVehicleRespawnPosition( vehicle, data.respawnPosX, data.respawnPosY, data.respawnPosZ, data.respawnRotX, data.respawnRotY, data.respawnRotZ )
			setElementInterior( vehicle, data.interior )
			setElementDimension( vehicle, data.dimension )
			if data.alarma == 1 then
				setElementData(vehicle, "havealarm", true)
			end
			setVehicleLocked( vehicle, true )
			setVehicleEngineState( vehicle, false )
			setVehicleOverrideLights( vehicle, data.lightsState + 1 )
			setElementData( vehicle, "fuel", data.fuel )
			if data.cepo == 1 then setVehicleEngineState( vehicle, false ) setElementData( vehicle, "cepo", 1 ) end
			setVehiclePaintjob ( vehicle, data.pinturas )
			setElementData(vehicle, "km", data.km)
			setElementData(vehicle, "idowner", data.characterID)
			setElementData(vehicle, "idveh", data.vehicleID)
			exports.vehicles_auxiliar:applyTunning(vehicle)
			setElementData(vehicle, "model", data.model)
			setElementData(vehicle, "fasemotor", data.fasemotor)
			setElementData(vehicle, "fasefrenos", data.fasefrenos)
			if data.marchas == 1 then
				setElementData(vehicle, "marchas", 1)
			end
			if data.marchas == 0 then
				setElementData(vehicle, "marchas", 0)
			end
			if data.fasemotor > 0 then
				exports.vehicles_auxiliar:solicitarMejora(vehicle, 1, data.fasemotor)
			end
			if data.fasefrenos > 0 then
				exports.vehicles_auxiliar:solicitarMejora(vehicle, 2, data.fasefrenos)
			end
			return true
		end
	end
end

addCommandHandler( { "createvehicle", "makevehicle" },
	function( player, commandName, ... )
		model = table.concat( { ... }, " " )
		model = getVehicleModelFromName( model ) or tonumber( model )
		if model then
			local x, y, z, rz = getPositionInFrontOf( player )
			local vehicle = createVehicle( model, x, y, z, 0, 0, rz )
			if vehicle then
				local r,g,b,r2,g2,b2 = getVehicleColor( vehicle )
				local luz1, luz2, luz3 = getVehicleHeadLightColor ( vehicle )
				local c1 = RGB2HEX (r or 255, g or 255, b or 255)
				local c2 = RGB2HEX (r2 or 255, g2 or 255, b2 or 255)
				local c3 = RGB2HEX (luz1 or 255, luz2 or 255, luz3 or 255)			
				local vehicleID, error = exports.sql:query_insertid("INSERT INTO vehicles (model, posX, posY, posZ, rotX, rotY, rotZ, color1, color2, color3, respawnPosX, respawnPosY, respawnPosZ, respawnRotX, respawnRotY, respawnRotZ, interior, dimension, respawnInterior, respawnDimension, numberplate) VALUES (" .. table.concat( { model, x, y, z, 0, 0, rz,"'".. c1 .."'", "'".. c2 .."'", "'".. c3 .."'", x, y, z, 0, 0, rz, getElementInterior( player ), getElementDimension( player ), getElementInterior( player ), getElementDimension( player ), "'".. getVehiclePlateText( vehicle ) .."'" }, ", " ) .. ")" )
				if vehicleID then
					-- tables for ID -> vehicle and vehicle -> data
					vehicleIDs[ vehicleID ] = vehicle
					vehicles[ vehicle ] = { vehicleID = vehicleID, respawnInterior = getElementInterior( player ), respawnDimension = getElementDimension( player ), characterID = 0, engineState = false, tintedWindows = false, fuel = 100 }				
					-- some properties
					setElementInterior( vehicle, getElementInterior( player ) )
					setElementDimension( vehicle, getElementDimension( player ) )
					setVehicleEngineState( vehicle, false )
					setVehicleOverrideLights( vehicle, 1 )
				    setElementData(vehicle, "idveh", vehicleID)						
					setElementData( vehicle, "fuel", 100 )
					setElementData( vehicle, "km", 0 )
					setElementData( vehicle, "marchas", 0 )
					setVehiclePlateText(vehicle, vehicleID)
					
					exports.vehicles_auxiliar:saveColors(vehicle)
					-- success message
					outputChatBox( "Has creado un " .. getVehicleName( vehicle ) .. " con el ID " .. vehicleID .. ".", player, 0, 255, 0 )
				else
					destroyElement( vehicle )
					outputChatBox( "MySQL-Query failed.", player, 255, 0, 0 )
				end
			else
				outputChatBox( "El nombre del vehículo es incorrecto. Vuelve a intentarlo.", player, 255, 0, 0 )
			end
		else
			outputChatBox( "Syntax: /" .. commandName .. " [modelo]", player, 255, 255, 255 )
		end
	end,
	true
)

addCommandHandler( { "deletevehicle", "delvehicle" },
	function( player, commandName, vehicleID )
		if hasObjectPermissionTo( player, "command.createvehicle", false ) or hasObjectPermissionTo( player, "command.temporaryvehicle", false ) then
			vehicleID = tonumber( vehicleID )
			if vehicleID and vehicleID ~= 0 then
				if ( vehicleID >= 0 and not hasObjectPermissionTo( player, "command.createvehicle", false ) ) or ( vehicleID < 0 and not hasObjectPermissionTo( player, "command.temporaryvehicle", false ) ) then
					outputChatBox( "No puedes borrar este vehículo.", player, 255, 0, 0 )
				else
					local vehicle = vehicleIDs[ vehicleID ]
					if vehicle then
						if vehicleID < 0 or exports.sql:query_free( "DELETE FROM vehicles WHERE vehicleID = " .. vehicleID ) then
							outputChatBox( "Has borrado el vehículo con ID " .. vehicleID .. " (" .. getVehicleName( vehicle ) .. ").", player, 0, 255, 153 )
							if vehicleID > 0 then
								exports.logs:addLogMessage("delveh", "Vehículo ID "..vehicleID.. ", borrado por "..getPlayerName(player)..".\n")
							end
							-- remove from vehicles list
							vehicleIDs[ vehicleID ] = nil
							vehicles[ vehicle ] = nil
							
							destroyElement( vehicle )
						else
							outputChatBox( "MySQL-Query failed.", player, 255, 0, 0 )
						end
					else
						outputChatBox( "Vehículo no encontrado.", player, 255, 0, 0 )
					end
				end
			else
				outputChatBox( "Syntax: /" .. commandName .. " [id]", player, 255, 255, 255 )
			end
		end
	end
)

function deleteVehicle ( vehicle )
	if not vehicle then return end
	local data = vehicles[ vehicle ]
	vehicleID = tonumber( data.vehicleID )
	if vehicleID then
		if vehicleID < 0 or exports.sql:query_free( "DELETE FROM vehicles WHERE vehicleID = " .. vehicleID ) then
			vehicleIDs[ vehicleID ] = nil
			vehicles[ vehicle ] = nil	
			destroyElement( vehicle )
			return true
		else
			return false
		end
	else
		return false
	end
end

function saveVehicle( vehicle, force )
	if vehicle then
		if models [getElementModel(vehicle)] and not force then return false end
		if getOwner(vehicle) and getOwner(vehicle) <= -1 and not force then return end
		local data = vehicles[ vehicle ]
		if data and data.vehicleID > 0 then
			local x, y, z = getElementPosition( vehicle )
			local rx, ry, rz = getVehicleRotation( vehicle )
			local km = getElementData(vehicle, "km")
			local success, error = exports.sql:query_free( "UPDATE vehicles SET respawnPosX = " .. x .. ", respawnPosY = " .. y .. ", respawnPosZ = " .. z .. ", respawnRotX = " .. rx .. ", respawnRotY = " .. ry .. ", respawnRotZ = " .. rz .. ", respawnInterior = " .. getElementInterior( vehicle ) .. ", respawnDimension = " .. getElementDimension( vehicle ) .. ", posX = " .. x .. ", km = " .. km .. ", posY = " .. y .. ", posZ = " .. z .. ", rotX = " .. rx .. ", rotY = " .. ry .. ", rotZ = " .. rz .. ", health = " .. math.min( 1000, math.ceil( getElementHealth( vehicle ) ) ) .. ", interior = " .. getElementInterior( vehicle ) .. ", dimension = " .. getElementDimension( vehicle ) .. ", fuel = " .. data.fuel .. " WHERE vehicleID = " .. data.vehicleID )	
		end  
	end
end

addEventHandler( "onResourceStop", resourceRoot,
	function( )	
		for k, v in pairs( getElementsByType("vehicle") ) do
			saveVehicle( v )
		end
		vehicles = { }
		vehicleIDs = { }
	end
)

addEventHandler( "onVehicleRespawn", resourceRoot,
	function( )
		local data = vehicles[ source ]
		if data and data.vehicleID > 0 then
			setElementInterior( source, data.respawnInterior )
			setElementDimension( source, data.respawnDimension )
			saveVehicle( source )
		end
	end
)

Aqui tienes una ayuda, para que puedas ver el link completo del codigo (aqui)

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...