I found out why your code went wrong. The table "cars" is not split into BMW and VAZ while the grid list is.
Here is the correct code:
DGS = exports.dgs
addEventHandler("onClientResourceStart", resourceRoot,
function()
createBlip(2827.34, 1290.94, 10.76, 55, 2, 255, 0, 0, 255, 0, 250)
carMarker = createMarker(2827.34, 1290.94, 9.6, "cylinder", 2.0, 0, 0, 255, 155)
end
)
carGUI = {}
function markerTrigger(hitPlayer)
if source == carMarker then
showCursor(true)
carGUI.win = DGS:dgsDxCreateWindow(0.75, 0.35, 0.25, 0.5, "Автомагазин", true)
carGUI.window = DGS:dgsDxCreateTabPanel(0, 0, 1, 1, true, carGUI.win)
carGUI.tab1 = DGS:dgsDxCreateTab("BMW", carGUI.window)
carGUI.grid1 = DGS:dgsDxCreateGridList(0, 0, 1, 1, true, carGUI.tab1)
carGUI.grid1Name = DGS:dgsDxGridListAddColumn(carGUI.grid1, "Название", 0.3)
carGUI.grid1Price = DGS:dgsDxGridListAddColumn(carGUI.grid1, "Цена", 0.1)
DGS:dgsDxGUISetProperty(carGUI.grid1,"Mark","BMW")
carGUI.tab2 = DGS:dgsDxCreateTab("VAZ", carGUI.window)
carGUI.grid2 = DGS:dgsDxCreateGridList(0, 0, 1, 1, true, carGUI.tab2)
carGUI.grid2Name = DGS:dgsDxGridListAddColumn(carGUI.grid2, "Название", 0.3)
carGUI.grid2Price = DGS:dgsDxGridListAddColumn(carGUI.grid2, "Цена", 0.1)
DGS:dgsDxGUISetProperty(carGUI.grid2,"Mark","VAZ")
setCameraMatrix(2814.61, 1316.01, 10.75)
--setElementDimension(localPlayer, 100)
for i=1,1 do
addCarsToGrids("a",100,"BMW",411)
end
for i=1,2 do
addCarsToGrids("a",100,"VAZ",422)
end
end
end
addEventHandler("onClientMarkerHit", getRootElement(), markerTrigger)
cars = {}
function addCarsToGrids(name, price, mark, model)
cars[mark] = cars[mark] or {}
cars[mark].name = {
price = tonumber(price),
model = tonumber(model),
name = name
}
if mark == "BMW" then
local row = DGS:dgsDxGridListAddRow(carGUI.grid1)
DGS:dgsDxGridListSetItemText(carGUI.grid1, row, carGUI.grid1Name, name)
DGS:dgsDxGridListSetItemText(carGUI.grid1, row, carGUI.grid1Price, price)
elseif mark == "VAZ" then
local row = DGS:dgsDxGridListAddRow(carGUI.grid2)
DGS:dgsDxGridListSetItemText(carGUI.grid2, row, carGUI.grid2Name, name)
DGS:dgsDxGridListSetItemText(carGUI.grid2, row, carGUI.grid2Price, price)
end
end
addEvent("getCar", true)
addEventHandler("getCar", getRootElement(), addCarsToGrids)
function clickTrigger(button, state)
if button == "left" and state == "up" then
local mark = DGS:dgsDxGUIGetProperty(source,"Mark")
if mark then
iprint(source)
print(mark)
if source == carGUI.grid1 then
local selected = DGS:dgsDxGridListGetSelectedItem(carGUI.grid1)
if selected ~= -1 then
if carGUI.buyButton == nil then
carGUI.buyButton = DGS:dgsDxCreateButton(0.3, 0.7, 0.3, 0.15, "Купить", true)
end
local name = DGS:dgsDxGridListGetItemText(carGUI.grid1, selected, carGUI.grid1Name)
if isElement(car) and getElementType(car) == "vehicle" then
destroyElement(car, false)
end
car = createVehicle(cars[mark].name.model, 2802.79, 1309.43, 10.89)
--setElementDimension(car, 100)
outputChatBox(cars[mark].name.name)
outputChatBox(cars[mark].name.model)
end
elseif source == carGUI.grid2 then
local selected = DGS:dgsDxGridListGetSelectedItem(carGUI.grid2)
if selected ~= -1 then
if carGUI.buyButton == nil then
carGUI.buyButton = DGS:dgsDxCreateButton(0.3, 0.7, 0.3, 0.15, "Купить", true)
end
local name = DGS:dgsDxGridListGetItemText(carGUI.grid2, selected, carGUI.grid2Name)
if isElement(car) and getElementType(car) == "vehicle" then
destroyElement(car, false)
end
car = createVehicle(cars[mark].name.model, 2802.79, 1309.43, 10.89)
outputChatBox(cars[mark].name.name)
outputChatBox(cars[mark].name.model)
end
end
end
end
end
addEventHandler("onClientDgsDxMouseClick", getRootElement(), clickTrigger)
function windowClosed()
if source == carGUI.win then
showCursor(false)
setCameraTarget(localPlayer, localPlayer)
setElementDimension(localPlayer, 0)
destroyElement(carGUI.buyButton)
end
end
addEventHandler("onClientDgsDxWindowClose", getRootElement(), windowClosed)