Jump to content

[HELP] Triggering a table into a function


..:D&G:..

Recommended Posts

Hello guys, I am trying to trigger a function which one of the arguments is a table with a string and a number.

I am trying to send the table to the client side into a gui

function showPartsPricesWindow(carPartsTable) 
    local Width = 560 
    local Height = 547 
    local screenwidth, screenheight = guiGetScreenSize() 
    local X = (screenwidth - Width)/2 
    local Y = (screenheight - Height)/2 
  
    if not (wPrices) then 
        wPrices = guiCreateWindow(X, Y, Width, Height, "Car Parts Prices", false) 
        guiWindowSetSizable(wPrices, false) 
  
        gParts = guiCreateGridList(29, 87, 491, 427, false, wPrices) 
        glNume = guiGridListAddColumn(gParts, "Part Name", 0.5) 
        glPrice = guiGridListAddColumn(gParts, "Part Price", 0.5) 
         
        for key, value in pairs(carPartsTable) do 
            local row = guiGridListAddRow( gParts ) 
            guiGridListSetItemText( gParts, row, glNume, value[1], false, true) 
            guiGridListSetItemData( gParts, row, glNume, tostring(value[1])) 
            guiGridListSetItemText( gParts, row, glPrice, value[2].. " $", false, true) 
            guiGridListSetItemData( gParts, row, glPrice, tonumber( value[2])) 
        end 

From a server side function

function getCarPartsPrices(carPartName, carPartID) 
    if (carPartID) then 
        local carPartsTable = {} 
        local carPartPrice = mysql:query_fetch_assoc("SELECT price FROM veh_upgrades_prices WHERE upgrade = " .. carPartID .. "") 
        table.insert(carPartsTable, {carPartName, carPartPrice, carPartID})  
        triggerClientEvent( client, "showPartsPricesWindow", client, carPartsTable) 
    end 
end 

But I get an error in client side for "value[2]" that attempt to concatenate field '?' (a table value) but I don't know why...

Thank you.

Link to comment
table.insert(carPartsTable, carPartName .. "," .. carPartPrice .. "," .. carPartID) 

Then split it into a table in client side.

function string:split( inSplitPattern, outResults ) 
  if not outResults then 
    outResults = { } 
  end 
  local theStart = 1 
  local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart ) 
  while theSplitStart do 
    table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) ) 
    theStart = theSplitEnd + 1 
    theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart ) 
  end 
  table.insert( outResults, string.sub( self, theStart ) ) 
  return outResults 
end 

Link to comment
table.insert(carPartsTable, carPartName .. "," .. carPartPrice .. "," .. carPartID) 

Then split it into a table in client side.

function string:split( inSplitPattern, outResults ) 
  if not outResults then 
    outResults = { } 
  end 
  local theStart = 1 
  local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart ) 
  while theSplitStart do 
    table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) ) 
    theStart = theSplitEnd + 1 
    theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart ) 
  end 
  table.insert( outResults, string.sub( self, theStart ) ) 
  return outResults 
end 

Edited by Guest
Link to comment

Well, now it works, but only the first element from the "upgrades" table is displayed, but not the rest...

SERVER SIDE

function getCarPartsPrices(carPartName, carPartID) 
    if (carPartID) then 
        local carPartPrice = mysql:query_fetch_assoc("SELECT price FROM veh_upgrades_prices WHERE upgrade = " .. carPartID .. "") 
        local carPartsString =  carPartName .. "," .. carPartPrice.price .. "," .. carPartID 
        --table.insert(carPartsTable, carPartsString) 
        triggerClientEvent( client, "showPartsPricesWindow", client, carPartsString) 
    end 
end 
addEvent("getCarPartsPrices", true) 
addEventHandler("getCarPartsPrices", getRootElement(), getCarPartsPrices) 

CLIENT SIDE

function sendCarPartsPricesToServer() 
    for i, v in ipairs(upgrades) do 
        if not (v == false) then 
            triggerServerEvent("getCarPartsPrices", getLocalPlayer(), tostring(v[1]), tonumber(v[3])) 
        end 
    end 
end 
addCommandHandler("test123", sendCarPartsPricesToServer) 
local carPartsTable = split(carPartsTables, ';') 
        for key, v in ipairs(carPartsTable) do 
            local carPartName = gettok(v, 1, string.byte(',')) 
            local carPartPrice = gettok(v, 2, string.byte(',')) or 0 
            local carPartID = gettok(v, 3, string.byte(',')) 
            local row = guiGridListAddRow( gParts ) 
            guiGridListSetItemText( gParts, row, glNume, carPartName, false, true) 
            guiGridListSetItemData( gParts, row, glNume, tostring(carPartName)) 
            guiGridListSetItemText( gParts, row, glPrice, carPartPrice.. " $", false, true) 
            guiGridListSetItemData( gParts, row, glPrice, tonumber(carPartPrice)) 
        end 

Link to comment

You are using it wrong.

carPartsTable has three elements (1-3). You are iterating through each one and using value[ x ], but the values are not tables, they are some other data type (string, integer, etc.)

So, don't loop it. Remove the for loop and just use carPartsTable[ 1 ] etc.

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