Jump to content

Fix Dealership


Hiuguer

Recommended Posts

The script sells the car but the buyer can't pick it up and the owner keeps the car, can anyone help?

 

 

addCommandHandler(Config["SELL"],
    function(player, _, id)
        if getElementType(player) == "player" then
            local model = getElementModel(getPedOccupiedVehicle(player))
            if id then
                local pID = getPlayerFromID(tonumber(id))
                if pID then
                    iprint(pID)
                    local query = dbPoll(dbQuery(database, "SELECT * FROM playersConce WHERE Model = ?", tonumber(model)), -1)
                    if vehicleConces[pID] then
                        return dxMsg(player, "O Jogador já possuí um veículo spawnado na cidade.", "error")
                    end
                    if query and #query > 0 then
                        for i = 1, #query do
                            iprint(query["Chassi"], getElementData(vehicleConces[player]))
                            if query["Chassi"] == getElementData(vehicleConces[player], "Yammy.vehicleBank") then
                                local Chassi = query["Chassi"]
                                local query_02 = dbPoll(dbQuery(database, "SELECT * FROM playersConce WHERE Model = ? AND Chassi = ?", tonumber(model), query["Chassi"]), -1)
                                if query and #query_02 > 0 then
                                    iprint("vendido",getVehicleType(vehicleConces[player]))
                                    if getVehicleType(vehicleConces[player]) == "Bike" then
                                        for i,v in ipairs(Config.VEICULOS.MOTOS) do
                                            if Config.VEICULOS.MOTOS.model == model then
                                                iprint(Config.VEICULOS.MOTOS.model, model, Chassi)
                                                local sucess = dbExec(database, "DELETE FROM playersConce WHERE Model = ? AND Chassi = ?", Config.VEICULOS.MOTOS.model, Chassi)
                                                local sucess1 = dbExec(database, "INSERT INTO playersConce (Conta,Model,Veiculo,Chassi) VALUES (?,?,?,?)", getAccountName(getPlayerAccount(pID)), tonumber(model), Config.VEICULOS.MOTOS.nome, Chassi)
                                                iprint("DELTADO:",sucess,"ENVIADO:",sucess1)
                                                iprint("Jogador que comprou",getAccountName(getPlayerAccount(pID)), tonumber(model), Chassi)
                                                if sucess and sucess1 then
                                                    vehicleConces[pID] = vehicleConces[player]
                                                    vehicleConces[player] = nil
                                                    dxMsg(player, "Você vendeu seu veículo com sucesso.", "success")
                                                    dxMsg(pID, "Você recebeu um veículo.", "success")
                                                end
                                            end
                                        end
                                    else
                                        for i,v in ipairs(Config.VEICULOS.CARROS) do
                                            if Config.VEICULOS.CARROS.model == model then
                                                local sucess = dbExec(database, "DELETE FROM playersConce WHERE Model = ? AND Chassi = ?", tonumber(model), qChassi)
                                                local sucess1 = dbExec(database, "INSERT INTO playersConce (Conta,Model,Veiculo,Chassi) VALUES (?,?,?,?)", getAccountName(getPlayerAccount(pID)), tonumber(model), Config.VEICULOS.CARROS.nome, Chassi)
                                                iprint("DELTADO:",sucess,"ENVIADO:",sucess1)
                                                iprint("Jogador que comprou",getAccountName(getPlayerAccount(pID)), tonumber(model), Chassi)
                                                if sucess and sucess1 then
                                                    vehicleConces[pID] = vehicleConces[player]
                                                    vehicleConces[player] = nil
                                                    dxMsg(player, "Você vendeu seu veículo com sucesso.", "success")
                                                    dxMsg(pID, "Você recebeu um veículo.", "success")
                                                end
                                            end
                                        end
                                    end
                                end
                            end
                        end
                    end
                else
                    dxMsg(player, "O(A) ID inserido não foi encontrado!", "warning")
                end
            else
                dxMsg(player, "Você não inseriu o ID do(a) jogador(a) desejado.", "info")
            end
        end
    end
)

 

 

 

 

Anything I can send the whole script

Link to comment

Could you put the code into code tags please? Also, I see a number of iprints, can you share the debugscript log as well? Does

dxMsg(player, "Você vendeu seu veículo com sucesso.", "success")
dxMsg(pID, "Você recebeu um veículo.", "success")

ever get executed?

 

What is this supposed to do, and why?

local query = dbPoll(dbQuery(database, "SELECT * FROM playersConce WHERE Model = ?", tonumber(model)), -1)

It appears to check if there are any vehicles in the database of that model ID, but it feels redundant to check, if you could later go on to check

if query["Chassi"] == getElementData(vehicleConces[player], "Yammy.vehicleBank") then
  local Chassi = query["Chassi"]
  local query_02 = dbPoll(dbQuery(database, "SELECT * FROM playersConce WHERE Model = ? AND Chassi = ?", tonumber(model), query["Chassi"]), -1)

This could be shortened to just one query, similar to the latter one, but in place of query["Chassi"] it would just be getElementdata(vehicleConces[player], "Yammy.vehicleBank")

The way your current code is structured is to execute a SELECT statement checking Model and Chassi for every record in playersConce where Model is tonumber(model). This means if you have 3000 records with model = 429 (Banshee), your code executes 3000 searches, x, for matching Model = 429 and Chassi = x["Chassi"] = getElementData(vehicleConces[player], "Yammy.vehicleBank")), where just one would suffice: SELECT search for Model = 429 and Chassi = getElementData(vehicleConces[player], "Yammy.vehicleBank")

Edited by Addlibs
Link to comment
Just now, Addlibs said:

Você poderia colocar o código em tags de código, por favor? Além disso, vejo vários iprints, você pode compartilhar o log de depuração também? Faz

    

alguma vez ser executado?

 

O que isso deveria fazer, e por quê?

  

Parece verificar se há algum veículo no banco de dados desse ID de modelo, mas parece redundante verificar, se você puder verificar mais tarde

   
  
    

Isso poderia ser reduzido para apenas uma consulta, semelhante à última, mas no lugar de query["Chassi"] seria apenas getElementdata(vehicleConces[player], "Yammy.vehicleBank")

A forma como seu código atual está estruturado é executar uma instrução SELECT verificando Model e Chassi para cada registro em playersConce onde Model é tonumber(model) . Isso significa que se você tiver 3.000 registros com model = 429 (Banshee), seu código executa 3.000 pesquisas, x , para corresponder Model = 429 e Chassi = x["Cassi"] , onde apenas uma seria suficiente.

Can I send you the whole file?

Link to comment
2 minutes ago, Addlibs said:

Uma captura de tela de /debugscript 3 ao tentar vender um veículo seria um bom começo.

I know you're trying to help, I activated this on my server and it keeps running errors, there's no time to print the error, can you do that?

Link to comment

Additionally, it seems Chassi is suposed to be unique, yet the database contains the following duplicates:

ID,Conta,Vehiculo,Chassi
379,fynkffx,Mercedes AMG 63,6762
483,mercenario,Impala 1967,6762

665,Kryvios,GTR R35,31969
666,CireloPlay,GTR R35,31969

279,Nirvana,Civic Type R,61810
667,Gabriel665,Civic Type R,61810

244,Nirvana,Nissan GTR,77690
337,relmen,GTR R35,77690

513,NINJATV22,Audi R8,83450
646,michael,Shineray,83450

Some of these duplicates appear to be the same vehicle, others not. You should probably mark the column as UNIQUE in the database to prevent this from being allowed by the database.


Also, this is the wrong way to do what you're setting out to do

local sucess = dbExec(database, "DELETE FROM playersConce WHERE Model = ? AND Chassi = ?", tonumber(model), qChassi)
local sucess1 = dbExec(database, "INSERT INTO playersConce (Conta,Model,Veiculo,Chassi) VALUES (?,?,?,?)", getAccountName(getPlayerAccount(pID)), tonumber(model), Config.VEICULOS.CARROS.nome, Chassi)

What you probably want is

local sucess = dbExec(database, "UPDATE playersConce SET Conta = ? WHERE Chassi = ?", getAccountName(getPlayerAccount(pID)), qChassi)

which changes the vehicle's owner by modifying the row, instead of deleting the old one and inserting a new one.

Edited by Addlibs
Link to comment
14 minutes ago, Addlibs said:

Além disso, parece que o Chassi deve ser único, mas o banco de dados contém as seguintes duplicatas:

ID,Conta,
Veículo,Cassi 379,fynkffx,Mercedes AMG 63,6762
483,mercenário,Impala 1967,6762

665,Kryvios,GTR R35,31969
666,CireloPlay,GTR R35,31969

279,Nirvana,Civic Type R,61810
667 ,Gabriel665,Civic Type R,61810

244,Nirvana,Nissan GTR,77690
337,relmen,GTR R35,77690

513,NINJATV22,Audi R8,83450
646,michael,Shineray,83450

Algumas dessas duplicatas parecem ser o mesmo veículo, outras não. Você provavelmente deve marcar a coluna como UNIQUE no banco de dados para evitar que isso seja permitido pelo banco de dados.


Além disso, esta é a maneira errada de fazer o que você está planejando fazer

 
 

O que você provavelmente quer é

 

que altera o proprietário do veículo modificando a linha, em vez de excluir a antiga e inserir uma nova.

Have you already added this in the code? If not, could you add it, upload it and send it to me? I'm very layman about it

I have no idea where to add this, or could you tell me which file it is in

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