Jump to content

[Help] problem , buy a car


Stranger

Recommended Posts

hello, i have a problem in this mod that i can't buy more than 1 car, and , the 2nd problem is , when i press a spawn button, without select a car nothing happend but if i select the car and press the spawn button again, it says: you don't own a car, so help.

client side:

dealerwdw = guiCreateWindow(0.19, 0.19, 0.57, 0.70, "", true) 
guiWindowSetSizable(dealerwdw, false) 
  
dealerwdwgrid = guiCreateGridList(0.02, 0.05, 0.97, 0.83, true, dealerwdw) 
guiGridListAddColumn(dealerwdwgrid, "Car", 0.5) 
guiGridListAddColumn(dealerwdwgrid, "Price", 0.5) 
  
dealerwdwclosebtn = guiCreateButton(0.01, 0.88, 0.34, 0.09, "Close", true, dealerwdw) 
guiSetProperty(dealerwdwclosebtn, "NormalTextColour", "FFAAAAAA") 
  
dealerwdwbuybtn = guiCreateButton(0.65, 0.88, 0.34, 0.09, "Buy", true, dealerwdw) 
guiSetProperty(dealerwdwbuybtn, "NormalTextColour", "FFAAAAAA") 
  
guiSetVisible(dealerwdw, false) 
  
function open_dealer_panel () 
if guiGetVisible(dealerwdw) == false then 
guiSetVisible(dealerwdw, true) 
showCursor(true) 
end 
end 
addEvent("open_dealer_wdw", true) 
addEventHandler("open_dealer_wdw", getRootElement(), open_dealer_panel) 
  
DealerVehicles = { 
{"Admiral", 50000}, 
{"Comet", 1000000}, 
{"Infernus", 3000000}, 
{"Bullet", 1500000}, 
{"Sultan", 1000000}, 
{"Huntley", 750000}, 
{"Sabre", 1000000}, 
} 
  
function list_cars () 
guiGridListClear(dealerwdwgrid) 
for i,v in ipairs(DealerVehicles) do 
local row = guiGridListAddRow(dealerwdwgrid) 
guiGridListSetItemText(dealerwdwgrid, row, 1, v[1], false, false) 
guiGridListSetItemText(dealerwdwgrid, row, 2, v[2], false, false) 
end 
end 
addEvent("list_dealer_cars", true) 
addEventHandler("list_dealer_cars", getRootElement(), list_cars) 
  
function dealer_panel_clicked () 
if source == dealerwdwclosebtn and guiGetVisible(dealerwdw) == true then guiSetVisible(dealerwdw, false) showCursor(false) guiGridListClear(dealerwdwgrid) 
end 
end 
addEventHandler("onClientGUIClick", dealerwdw, dealer_panel_clicked) 
  
function dealer_panel_buy (player) 
if source == dealerwdwbuybtn then 
local row = guiGridListGetSelectedItem(dealerwdwgrid) 
triggerServerEvent("buy_dealer_car", getLocalPlayer(), getLocalPlayer(), guiGridListGetItemText(dealerwdwgrid, row, 1), guiGridListGetItemText(dealerwdwgrid, row, 1), guiGridListGetItemText(dealerwdwgrid, row, 2)) 
end 
end 
addEventHandler("onClientGUIClick", dealerwdw, dealer_panel_buy) 
  
---------------------------------------------- 
--------------- Spawner Panel ---------------- 
---------------------------------------------- 
  
  
spawncarwdw = guiCreateWindow(0.71, 0.31, 0.27, 0.52, "Spawn vehicle", true) 
guiWindowSetSizable(spawncarwdw, false) 
guiSetAlpha(spawncarwdw, 1.00) 
  
spawncarwdwgrid = guiCreateGridList(0.05, 0.35, 0.92, 0.60, true, spawncarwdw) 
guiGridListAddColumn(spawncarwdwgrid, "Car", 0.5) 
guiGridListAddColumn(spawncarwdwgrid, "Health", 0.3) 
spawncarwdwlockbtn = guiCreateButton(0.49, 0.22, 0.37, 0.10, "lock", true, spawncarwdw) 
guiSetProperty(spawncarwdwclosebtn, "NormalTextColour", "FFFD0000") 
spawncarwdrefwdw = guiCreateButton(0.05, 0.22, 0.33, 0.09, "Refresh List", true, spawncarwdw) 
guiSetProperty(spawncarwdrefwdw, "NormalTextColour", "FF72F805") 
spawncarwdwspwnwdw = guiCreateButton(0.49, 0.08, 0.37, 0.10, "Spawn", true, spawncarwdw) 
guiSetProperty(spawncarwdwspwnwdw, "NormalTextColour", "FF72F805") 
spawncarwdwparkbtn = guiCreateButton(0.05, 0.08, 0.32, 0.11, "Hide", true, spawncarwdw) 
guiSetProperty(spawncarwdwparkbtn, "NormalTextColour", "FFFE0000") 
  
guiSetVisible(spawncarwdw, false) 
  
function open_spawner_panel () 
if guiGetVisible(spawncarwdw) == false then 
guiSetVisible(spawncarwdw, true) 
showCursor(true) 
guiGridListClear(spawncarwdwgrid)  
triggerServerEvent("list_owned_cars_S", getLocalPlayer(), getLocalPlayer()) 
elseif  
guiGetVisible(spawncarwdw) == true then 
guiSetVisible(spawncarwdw, false) 
showCursor(false) 
end 
end 
addEvent("open_spawner_wdw", true) 
addEventHandler("open_spawner_wdw", getRootElement(), open_spawner_panel) 
  
function vehicle_spawner_panel () 
if source == spawncarwdwlockbtn and guiGetText (spawncarwdwlockbtn) == "lock" then veh = getPedOccupiedVehicle (localPlayer) triggerServerEvent ("lock", localPlayer, veh) guiSetText (spawncarwdwlockbtn, "Unlock") end 
if source == spawncarwdwlockbtn and guiGetText (spawncarwdwlockbtn) == "Unlock" then veh2 = getPedOccupiedVehicle (localPlayer) triggerServerEvent ("unlock", localPlayer, veh2) guiSetText (spawncarwdwlockbtn, "lock") end  
if source == spawncarwdrefwdw then guiGridListClear(spawncarwdwgrid) triggerServerEvent("list_owned_cars_S", getLocalPlayer(), getLocalPlayer()) end 
if source == spawncarwdwspwnwdw then  
local row = guiGridListGetSelectedItem(spawncarwdwgrid) 
triggerServerEvent("spawn_dealer_car", getLocalPlayer(), getLocalPlayer(), guiGridListGetItemText(spawncarwdwgrid, row, 1))  
end 
if source == spawncarwdwparkbtn then triggerServerEvent("destroy_dealer_car", getLocalPlayer(), getLocalPlayer()) end 
end 
addEventHandler("onClientGUIClick", spawncarwdw, vehicle_spawner_panel) 
  
function list_cars_you_own (vehiclemodel,vehicleHealth) 
local row = guiGridListAddRow(spawncarwdwgrid) 
guiGridListSetItemText(spawncarwdwgrid, row, 1, tostring(vehiclemodel), false, false) 
guiGridListSetItemText(spawncarwdwgrid, row, 2, tonumber(vehicleHealth), false, false) 
end 
addEvent("list_owned_cars", true) 
addEventHandler("list_owned_cars", getRootElement(), list_cars_you_own)  

server side:

function make_db () 
executeSQLQuery ("CREATE TABLE IF NOT EXISTS dealercars (id NUMERIC, owner TEXT, model NUMERIC, name TEXT, health NUMBERIC, paintjob NUMERIC, color NUMERIC, upgrades NUMERIC)")  
end 
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), make_db) 
  
local dealerMarker1 = createMarker(2131.75, -1148.138671875, 23.406946182251, "cylinder", 2, 0, 255, 0, 255) 
createBlipAttachedTo(dealerMarker1, 55, 2, 0, 0, 0, 0, 0, 300) 
  
function open_dealer_server (hitElement) 
if source == dealerMarker1 then  
triggerClientEvent(hitElement, "open_dealer_wdw", root) 
triggerClientEvent(hitElement, "list_dealer_cars", root) 
end 
end 
addEventHandler("onMarkerHit", getRootElement(), open_dealer_server) 
  
function buy_car_from_dealer (owner,model,name,money) 
if getPlayerMoney(owner) >= tonumber(money) then 
local ID = executeSQLQuery("SELECT * FROM dealercars") 
for i,v in ipairs(ID) do 
carid = v.id+1 
end 
local account = getAccountName(getPlayerAccount(owner)) 
local modela = getVehicleModelFromName(model) 
local carbought = executeSQLQuery("INSERT INTO dealercars(id,owner,model,name,health,paintjob,color,upgrades) VALUES(?,?,?,?,?,?,?,?)", carid, tostring(account), tonumber(modela), tostring(model), 1000, 0, 0, 0)  
takePlayerMoney(owner, money) 
setElementData(owner, "gotCar", "no") 
outputChatBox("Congratulations you've bought a "..model.." for "..money, owner, 0, 255, 0) 
else 
outputChatBox("You don't have enough money", owner, 255, 0, 0) 
end 
end 
addEvent("buy_dealer_car", true) 
addEventHandler("buy_dealer_car", getRootElement(), buy_car_from_dealer) 
  
  
---------------------------------------------- 
--------------- Spawner Panel ---------------- 
---------------------------------------------- 
function keybindpla () 
for i,v in ipairs(getElementsByType("player")) do 
bindKey(v, "f3", "down", open_spawner_pla) 
setElementData(v, "gotCar", "no") 
end 
end 
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), keybindpla) 
addEventHandler("onPlayerLogin", getRootElement(), keybindpla) 
  
  
function open_spawner_pla (source) 
triggerClientEvent(source, "open_spawner_wdw", root) 
end 
  
function list_owned_cars_server (player) 
local playerAcc = getAccountName(getPlayerAccount(player)) 
local carlmao = executeSQLQuery("SELECT * FROM dealercars WHERE owner=?", playerAcc) 
for i,k in ipairs(carlmao) do 
health = getElementHealth (k.name) 
triggerClientEvent(player, "list_owned_cars", root, k.name, k.health) 
end 
end 
addEvent("list_owned_cars_S", true) 
addEventHandler("list_owned_cars_S", getRootElement(), list_owned_cars_server) 
  
function spawn_car_pleaseD (ownerName,carName) 
if getElementData(ownerName, "gotCar") == "no" then 
local gayowner = getAccountName(getPlayerAccount(ownerName)) 
local carModel = getVehicleModelFromName(carName) 
local x,y,z = getElementPosition(ownerName) 
OwnerVehicle = createVehicle(carModel, x, y, z) 
setTimer(warpPedIntoVehicle, 50, 1, ownerName, OwnerVehicle) 
setElementData(OwnerVehicle, "owner", tostring(gayowner)) 
setElementData(ownerName, "gotCar", "yes") 
local carlma = executeSQLQuery("SELECT * FROM dealercars WHERE owner=?", gayowner) 
else 
outputChatBox("You don't own a car", ownerName, 255, 0, 0) 
end 
end 
addEvent("spawn_dealer_car", true) 
addEventHandler("spawn_dealer_car", getRootElement(), spawn_car_pleaseD) 
  
function destroy_car_pleaseD (ownerName) 
pedCar = getPedOccupiedVehicle(ownerName) 
gayownerAcc = getAccountName(getPlayerAccount(ownerName)) 
if isPedInVehicle(ownerName) == true then 
if getElementData(ownerName, "gotCar") == "yes" and getElementData(pedCar, "owner") == gayownerAcc then 
CurrentCar = getPedOccupiedVehicle(ownerName) 
destroyElement(CurrentCar) 
setElementData(ownerName, "gotCar", "no") 
else  
outputChatBox("You can't park a car you don't own", ownerName, 255, 0, 0) 
end 
else  
outputChatBox("You aren't in a vehicle", ownerName, 255, 0, 0) 
end 
end 
addEvent("destroy_dealer_car", true) 
addEventHandler("destroy_dealer_car", getRootElement(), destroy_car_pleaseD) 
  
addEvent ("lock", true) 
addEventHandler ("lock", root, function (veh) 
setVehicleLocked (veh, true) 
end) 
  
addEvent ("unlock", true) 
addEventHandler ("unlock", root, function (veh2) 
setVehicleLocked (veh2, false) 
end) 

and the health of the car,

it's not work

it shows like this: 1000

but if i broke the car, and destroy it,

and spawn it again, will be fixed.

Edited by Guest
Link to comment
  • Moderators

Try this:

Server:

function make_db () 
    executeSQLQuery ("CREATE TABLE IF NOT EXISTS dealercars (id NUMERIC, owner TEXT, model NUMERIC, name TEXT, health NUMERIC, paintjob NUMERIC, color NUMERIC, upgrades NUMERIC)") 
end 
addEventHandler("onResourceStart", resourceRoot, make_db) 
  
local dealerMarker1 = createMarker(2131.75, -1148.138671875, 23.406946182251, "cylinder", 2, 0, 255, 0, 255) 
createBlipAttachedTo(dealerMarker1, 55, 2, 0, 0, 0, 0, 0, 300) 
  
function open_dealer_server ( hitElement ) 
    if source == dealerMarker1 and getElementType( hitElement ) == "player" then 
        triggerClientEvent(hitElement, "open_dealer_wdw", hitElement) 
    end 
end 
addEventHandler("onMarkerHit", root, open_dealer_server) 
  
function buy_car_from_dealer (model, price) 
    if getPlayerMoney(client) >= price then 
        local vehicles = executeSQLQuery("SELECT id FROM dealercars") 
        local carid = 1 
        for k, row in ipairs ( vehicles ) do 
            if row.id > carid then 
                carid = row.id 
            end 
        end 
        local account = getAccountName(getPlayerAccount(client)) 
        local modela = getVehicleModelFromName(model) 
        local x, y, z = getElementPosition( client ) 
        local veh = createVehicle( modela, x, y, z+1 ) 
        takePlayerMoney(client, money) 
        setElementID( veh, carid ) 
        local carbought = executeSQLQuery( 
            "INSERT INTO dealercars(id,owner,model,name,health,paintjob,color,upgrades) VALUES(?,?,?,?,?,?,?,?)", 
            carid, tostring(account), modela, tostring(model), 1000, 0, 0, 0 
        ) 
        outputChatBox("Congratulations you've bought a "..model.." for $"..money, client, 0, 255, 0) 
    else 
        outputChatBox("You don't have enough money", client, 255, 0, 0) 
    end 
end 
addEvent("buy_dealer_car", true) 
addEventHandler("buy_dealer_car", root, buy_car_from_dealer) 
  
  
---------------------------------------------- 
--------------- Spawner Panel ---------------- 
---------------------------------------------- 
  
function open_spawner_pla () 
    triggerClientEvent(client, "open_spawner_wdw", root) 
end 
  
function list_owned_cars_server () 
    local playerAcc = getAccountName(getPlayerAccount(player)) 
    local carlmao = executeSQLQuery("SELECT * FROM dealercars WHERE owner=?", playerAcc) 
    if carlmao and type(carlmao) == "table" and #carlmao > 0 then 
        triggerClientEvent(client, "list_owned_cars", root, carlmao) 
    end 
end 
addEvent("list_owned_cars_S", true) 
addEventHandler("list_owned_cars_S", root, list_owned_cars_server) 
  
function spawn_car_pleaseD ( vehID ) 
    local gayowner = getAccountName(getPlayerAccount(client)) 
    local carModel = getVehicleModelFromName(carName) 
    local x,y,z = getElementPosition(client) 
    local veh 
    if vehID then 
        veh = getElementByID( vehID ) 
        if veh and getElementType( veh ) == "vehicle" then 
            fixVehicle( veh ) 
        end 
    else 
        veh = createVehicle(carModel, x, y, z) 
        setElementID( veh, vehID ) 
        setElementData(veh, "owner", tostring(gayowner)) 
    end 
    setTimer(warpPedIntoVehicle, 100, 1, client, veh) 
end 
addEvent("spawn_dealer_car", true) 
addEventHandler("spawn_dealer_car", root, spawn_car_pleaseD) 
  
function destroy_car_pleaseD ( ownerName ) 
    local gayownerAcc = getAccountName(getPlayerAccount(ownerName)) 
    local pedCar = getPedOccupiedVehicle(ownerName) 
    if pedCar then 
        if getElementData(pedCar, "owner") == gayownerAcc then 
            local currentCar = getPedOccupiedVehicle(ownerName) 
  
            -- SQL query (UPDATE) here to update the vehicle datas in the database 
  
            destroyElement(currentCar) 
        else 
            outputChatBox("You can't park a car you don't own", ownerName, 255, 0, 0) 
        end 
    else 
        outputChatBox("You aren't in a vehicle", ownerName, 255, 0, 0) 
    end 
end 
addEvent("destroy_dealer_car", true) 
addEventHandler("destroy_dealer_car", root, destroy_car_pleaseD) 
  
addEvent ("lock", true) 
addEventHandler ("lock", root, function (veh) 
    local veh = getPedOccupiedVehicle (localPlayer) 
    setVehicleLocked (veh, true) 
end) 
  
addEvent ("unlock", true) 
addEventHandler ("unlock", root, function (veh) 
    local veh = getPedOccupiedVehicle (localPlayer) 
    setVehicleLocked (veh, false) 
end) 

Line 88, make a query to update the database

Client:

dealerwdw = guiCreateWindow(0.19, 0.19, 0.57, 0.70, "", true) 
guiWindowSetSizable(dealerwdw, false) 
  
dealerwdwgrid = guiCreateGridList(0.02, 0.05, 0.97, 0.83, true, dealerwdw) 
guiGridListAddColumn(dealerwdwgrid, "Car", 0.5) 
guiGridListAddColumn(dealerwdwgrid, "Price", 0.5) 
  
dealerwdwclosebtn = guiCreateButton(0.01, 0.88, 0.34, 0.09, "Close", true, dealerwdw) 
guiSetProperty(dealerwdwclosebtn, "NormalTextColour", "FFAAAAAA") 
  
dealerwdwbuybtn = guiCreateButton(0.65, 0.88, 0.34, 0.09, "Buy", true, dealerwdw) 
guiSetProperty(dealerwdwbuybtn, "NormalTextColour", "FFAAAAAA") 
  
guiSetVisible(dealerwdw, false) 
  
function open_dealer_panel () 
    if guiGetVisible(dealerwdw) == false then 
        list_cars() 
        guiSetVisible(dealerwdw, true) 
        showCursor(true) 
    end 
end 
addEvent("open_dealer_wdw", true) 
addEventHandler("open_dealer_wdw", root, open_dealer_panel) 
  
DealerVehicles = { 
    {"Admiral", 50000}, 
    {"Comet", 1000000}, 
    {"Infernus", 3000000}, 
    {"Bullet", 1500000}, 
    {"Sultan", 1000000}, 
    {"Huntley", 750000}, 
    {"Sabre", 1000000} 
} 
  
function list_cars () 
    guiGridListClear(dealerwdwgrid) 
    for i,v in ipairs(DealerVehicles) do 
        local row = guiGridListAddRow(dealerwdwgrid) 
        guiGridListSetItemText(dealerwdwgrid, row, 1, v[1], false, false) 
        guiGridListSetItemText(dealerwdwgrid, row, 2, v[2], false, false) 
    end 
end 
  
function dealer_panel_clicked () 
    if source == dealerwdwclosebtn and guiGetVisible(dealerwdw) == true then 
        guiSetVisible(dealerwdw, false) 
        showCursor(false) 
        guiGridListClear(dealerwdwgrid) 
    end 
end 
addEventHandler("onClientGUIClick", dealerwdw, dealer_panel_clicked) 
  
function dealer_panel_buy (player) 
    if source == dealerwdwbuybtn then 
        local row = guiGridListGetSelectedItem(dealerwdwgrid) 
        local model = guiGridListGetItemText(dealerwdwgrid, row, 1) 
        local price = guiGridListGetItemText(dealerwdwgrid, row, 2) 
        triggerServerEvent("buy_dealer_car", localPlayer, model, tonumber(price)) 
    end 
end 
addEventHandler("onClientGUIClick", dealerwdw, dealer_panel_buy) 
  
---------------------------------------------- 
--------------- Spawner Panel ---------------- 
---------------------------------------------- 
  
  
spawncarwdw = guiCreateWindow(0.71, 0.31, 0.27, 0.52, "Spawn vehicle", true) 
guiWindowSetSizable(spawncarwdw, false) 
guiSetAlpha(spawncarwdw, 1.00) 
  
spawncarwdwgrid = guiCreateGridList(0.05, 0.35, 0.92, 0.60, true, spawncarwdw) 
guiGridListAddColumn(spawncarwdwgrid, "Car", 0.5) 
guiGridListAddColumn(spawncarwdwgrid, "Health", 0.3) 
spawncarwdwlockbtn = guiCreateButton(0.49, 0.22, 0.37, 0.10, "lock", true, spawncarwdw) 
guiSetProperty(spawncarwdwclosebtn, "NormalTextColour", "FFFD0000") 
spawncarwdrefwdw = guiCreateButton(0.05, 0.22, 0.33, 0.09, "Refresh List", true, spawncarwdw) 
guiSetProperty(spawncarwdrefwdw, "NormalTextColour", "FF72F805") 
spawncarwdwspwnwdw = guiCreateButton(0.49, 0.08, 0.37, 0.10, "Spawn", true, spawncarwdw) 
guiSetProperty(spawncarwdwspwnwdw, "NormalTextColour", "FF72F805") 
spawncarwdwparkbtn = guiCreateButton(0.05, 0.08, 0.32, 0.11, "Hide", true, spawncarwdw) 
guiSetProperty(spawncarwdwparkbtn, "NormalTextColour", "FFFE0000") 
  
guiSetVisible(spawncarwdw, false) 
  
function open_spawner_panel () 
    if guiGetVisible(spawncarwdw) == false then 
        triggerServerEvent("list_owned_cars_S", localPlayer) 
        guiSetVisible(spawncarwdw, true) 
        showCursor(true) 
    elseif 
        guiGetVisible(spawncarwdw) == true then 
        guiSetVisible(spawncarwdw, false) 
        showCursor(false) 
    end 
end 
bindKey("f3", "down", open_spawner_pla) 
  
function vehicle_spawner_panel () 
    if source == spawncarwdwlockbtn and guiGetText (spawncarwdwlockbtn) == "lock" then 
        triggerServerEvent ("lock", localPlayer) 
        guiSetText (spawncarwdwlockbtn, "Unlock") 
    end 
    if source == spawncarwdwlockbtn and guiGetText (spawncarwdwlockbtn) == "Unlock" then 
        triggerServerEvent ("unlock", localPlayer) 
        guiSetText (spawncarwdwlockbtn, "lock") 
    end 
    if source == spawncarwdrefwdw then 
        triggerServerEvent("list_owned_cars_S", localPlayer) 
    end 
    if source == spawncarwdwspwnwdw then 
        local row = guiGridListGetSelectedItem(spawncarwdwgrid) 
        local vehID = guiGridListGetItemData(spawncarwdwgrid, row, 1) 
        triggerServerEvent("spawn_dealer_car", localPlayer, vehID) 
    end 
    if source == spawncarwdwparkbtn then 
        triggerServerEvent("destroy_dealer_car", localPlayer) 
    end 
end 
addEventHandler("onClientGUIClick", spawncarwdw, vehicle_spawner_panel) 
  
function list_cars_you_own ( vehicles ) 
    guiGridListClear(spawncarwdwgrid) 
    for k, veh in ipairs ( vehicles ) do 
        local row = guiGridListAddRow(spawncarwdwgrid) 
        guiGridListSetItemText(spawncarwdwgrid, row, 1, tostring(veh.name), false, false) 
        guiGridListSetItemData(spawncarwdwgrid, row, 1, veh.id) 
        local spawnedVeh = getElementByID( veh.id ) 
        local vehHealth = veh.health 
        if spawnedVeh and getElementType( spawnedVeh ) == "vehicle" then 
            vehHealth = getElementHealth( spawnedVeh ) 
        end 
        guiGridListSetItemText(spawncarwdwgrid, row, 2, tonumber(vehHealth), false, false) 
    end 
end 
addEvent("list_owned_cars", true) 
addEventHandler("list_owned_cars", root, list_cars_you_own) 

Dunno if it will work immediatly, so please provide errors from the server but also from the client (/debugscript 3)

(Wasted some time to indent that code correctly. So please be sure your code is indented using tabs the next time you post some code.)

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