Jump to content

Any pro of using table please help


jingzhi

Recommended Posts

Hey guys, I made this script long time ago, but it didnt work, i tried to fix it but i still cant until today. I want to make a ammunation which store the weapon id, ammo ammount, and money in 3 tables and trigger them together, please help if you see where have i done wrong, or you have better way to do this, thank y'all very much!! :)

weapontable = { 
} 
  
ammotable = { 
} 
  
pricetable = { 
} 
  
  
function table.size(tab) 
    local length = 0 
    for _ in pairs(tab) do length = length + 1 end 
    return length 
end 
  
  
addEventHandler("onClientResourceStart", resourceRoot, 
    function() 
local screenW, screenH = guiGetScreenSize() 
        AmmoMenu = guiCreateWindow((screenW - 465) / 2, (screenH - 834) / 2, 465, 834, "Ammu-Nation", false) 
        guiWindowSetSizable(AmmoMenu, false) 
        AmmuNationImage = guiCreateStaticImage(67, 54, 332, 88, ":weapons/ammu-nation.png", false, AmmoMenu) 
        TazerImage = guiCreateStaticImage(52, 179, 70, 56, ":weapons/Tazer.png", false, AmmoMenu) 
        PistolImage = guiCreateStaticImage(52, 253, 70, 59, ":weapons/Pistol.png", false, AmmoMenu) 
        DeserteagleImage = guiCreateStaticImage(52, 328, 70, 55, ":weapons/Desert_Eagle.png", false, AmmoMenu) 
        Ak47Image = guiCreateStaticImage(33, 739, 113, 44, ":weapons/ak47.png", false, AmmoMenu) 
        UziImage = guiCreateStaticImage(53, 401, 69, 52, ":weapons/uzi.png", false, AmmoMenu) 
        Tec9Image = guiCreateStaticImage(53, 468, 69, 50, ":weapons/tec9.png", false, AmmoMenu) 
        Mp5Image = guiCreateStaticImage(46, 532, 86, 45, ":weapons/mp5.png", false, AmmoMenu) 
        Spas12Image = guiCreateStaticImage(33, 592, 113, 50, ":weapons/Spas-12.png", false, AmmoMenu) 
        M4Image = guiCreateStaticImage(33, 663, 114, 55, ":weapons/M4A1.png", false, AmmoMenu) 
        AmmountLabel = guiCreateLabel(196, 478, 55, 17, "Ammount", false, AmmoMenu) 
        AmmountTextbox = guiCreateEdit(172, 508, 104, 24, "", false, AmmoMenu) 
        guiEditSetMaxLength(AmmountTextbox, 6) 
        BuyButton = guiCreateButton(172, 729, 126, 59, "Buy Cart", false, AmmoMenu) 
        guiSetFont(BuyButton, "default-bold-small") 
        guiSetProperty(BuyButton, "NormalTextColour", "FF0000FF") 
        QuitButton = guiCreateButton(315, 729, 126, 59, "Quit", false, AmmoMenu) 
        guiSetFont(QuitButton, "default-bold-small") 
        guiSetProperty(QuitButton, "NormalTextColour", "FFFF0000") 
        Cartlist = guiCreateGridList(172, 551, 269, 122, false, AmmoMenu) 
        Weaponcolumn = guiGridListAddColumn(Cartlist, "Weapon", 0.3) 
        Amountcolumn = guiGridListAddColumn(Cartlist, "Amount", 0.3) 
        Pricecolumn = guiGridListAddColumn(Cartlist, "Price", 0.3)  
        AddcartButton = guiCreateButton(314, 508, 89, 27, "Add to cart", false, AmmoMenu) 
        WeaponInfo = guiCreateScrollPane(177, 179, 223, 238, false, AmmoMenu)     
        Infolabel = guiCreateLabel(17, 150, 150, 19, "Press icon for information", false, AmmoMenu) 
        guiSetFont(Infolabel, "default-bold-small") 
        guiGridListSetSortingEnabled (Cartlist,false) 
        guiSetVisible(AmmoMenu,false) 
    end 
) 
  
  
  
  
function chooseweapon() 
    if source == TazerImage then 
        WeaponChosen = 23 
    end 
    if source == PistolImage then 
        WeaponChosen = 22 
    end 
    if source == DeserteagleImage then 
        WeaponChosen = 24 
    end 
    if source == Ak47Image then 
        WeaponChosen = 30 
    end 
    if source == UziImage then 
        WeaponChosen = 28 
    end 
    if source == Tec9Image then 
        WeaponChosen = 32 
    end 
    if source == Mp5Image then 
        WeaponChosen = 29 
    end 
    if source == Spas12Image then 
        WeaponChosen = 27 
    end 
    if source == M4Image then 
        WeaponChosen = 31 
    end 
    if source == BuyButton then 
        if guiGridListGetRowCount(Cartlist) > 0 then 
            for i,price in pairs(pricetable) do 
                local totalprice = totalprice + price  
            end 
            if getPlayerMoney(localPlayer) >= totalprice then 
                for index,id in pairs(weapontable) do 
                    for i,ammo in pairs(ammotable) do 
                        triggerServerEvent("giveweapon",resourceRoot,localPlayer,id,ammo) 
                    end 
                end 
                takePlayerMoney(totalprice) 
            else 
            outputChatBox("You don't have enough money!",255,0,0) 
            end 
        else 
            outputChatBox("You don't have anything in your cart!",255,0,0) 
        end 
    end 
    if source == QuitButton then 
        showCursor(false) 
        guiSetVisible(AmmoMenu,false)   
        guiGridListClear (Cartlist) 
        guiSetText (AmmountTextbox,"") 
        for id,weaponid in pairs(weapontable) do weapontable[k]=nil end 
        for id,ammo in pairs(ammotable) do ammotable[k]=nil end 
        for id,price in pairs(pricetable) do pricetable[k]=nil end 
    end 
    if source == AddcartButton then 
        if WeaponChosen then 
            if tonumber(guiGetText(AmmountTextbox)) ~= nil then 
                if tonumber(guiGetText(AmmountTextbox)) > 0 then 
                    if tonumber(guiGetText(AmmountTextbox)) == math.floor(guiGetText(AmmountTextbox)) then 
                        local row = guiGridListAddRow(Cartlist) 
                        table.insert(weapontable,WeaponChosen) 
                        table.insert(ammotable,guiGetText(AmmountTextbox)) 
                        table.insert(pricetable,math.floor(10 * guiGetText(AmmountTextbox))) 
                        guiGridListSetItemText(Cartlist,row,Weaponcolumn,""..getWeaponNameFromID(WeaponChosen),false,false) 
                        guiGridListSetItemText(Cartlist,row,Amountcolumn,""..guiGetText(AmmountTextbox),false,false) 
                        guiGridListSetItemText(Cartlist,row,Pricecolumn,"$"..tostring(math.floor(10 * guiGetText(AmmountTextbox))),false,false)              
                    else  
                        outputChatBox("Please enter a integer!",255,0,0) 
                    end 
                else 
                    outputChatBox("Please enter a positive number!",255,0,0) 
                end 
            else 
                outputChatBox("Please enter a integer as the ammount of ammo",255,0,0) 
            end 
        else 
            outputChatBox("Please choose a weapon by clicking on the icon of it",255,0,0) 
        end 
    end 
end  
addEventHandler("onClientGUIClick",resourceRoot,chooseweapon) 
  
addEvent("showweaponGUI",true) 
  
function showGUI() 
    totalprice = 0 
    outputChatBox("true") 
    showCursor(true) 
    guiSetVisible(AmmoMenu,true) 
end 
addEventHandler("showweaponGUI",getRootElement(),showGUI) 

Link to comment
Hey guys, I made this script long time ago, but it didnt work, i tried to fix it but i still cant until today. I want to make a ammunation which store the weapon id, ammo ammount, and money in 3 tables and trigger them together, please help if you see where have i done wrong, or you have better way to do this, thank y'all very much!! :)
weapontable = { 
} 
  
ammotable = { 
} 
  
pricetable = { 
} 
  
  
function table.size(tab) 
    local length = 0 
    for _ in pairs(tab) do length = length + 1 end 
    return length 
end 
  
  
addEventHandler("onClientResourceStart", resourceRoot, 
    function() 
local screenW, screenH = guiGetScreenSize() 
        AmmoMenu = guiCreateWindow((screenW - 465) / 2, (screenH - 834) / 2, 465, 834, "Ammu-Nation", false) 
        guiWindowSetSizable(AmmoMenu, false) 
        AmmuNationImage = guiCreateStaticImage(67, 54, 332, 88, ":weapons/ammu-nation.png", false, AmmoMenu) 
        TazerImage = guiCreateStaticImage(52, 179, 70, 56, ":weapons/Tazer.png", false, AmmoMenu) 
        PistolImage = guiCreateStaticImage(52, 253, 70, 59, ":weapons/Pistol.png", false, AmmoMenu) 
        DeserteagleImage = guiCreateStaticImage(52, 328, 70, 55, ":weapons/Desert_Eagle.png", false, AmmoMenu) 
        Ak47Image = guiCreateStaticImage(33, 739, 113, 44, ":weapons/ak47.png", false, AmmoMenu) 
        UziImage = guiCreateStaticImage(53, 401, 69, 52, ":weapons/uzi.png", false, AmmoMenu) 
        Tec9Image = guiCreateStaticImage(53, 468, 69, 50, ":weapons/tec9.png", false, AmmoMenu) 
        Mp5Image = guiCreateStaticImage(46, 532, 86, 45, ":weapons/mp5.png", false, AmmoMenu) 
        Spas12Image = guiCreateStaticImage(33, 592, 113, 50, ":weapons/Spas-12.png", false, AmmoMenu) 
        M4Image = guiCreateStaticImage(33, 663, 114, 55, ":weapons/M4A1.png", false, AmmoMenu) 
        AmmountLabel = guiCreateLabel(196, 478, 55, 17, "Ammount", false, AmmoMenu) 
        AmmountTextbox = guiCreateEdit(172, 508, 104, 24, "", false, AmmoMenu) 
        guiEditSetMaxLength(AmmountTextbox, 6) 
        BuyButton = guiCreateButton(172, 729, 126, 59, "Buy Cart", false, AmmoMenu) 
        guiSetFont(BuyButton, "default-bold-small") 
        guiSetProperty(BuyButton, "NormalTextColour", "FF0000FF") 
        QuitButton = guiCreateButton(315, 729, 126, 59, "Quit", false, AmmoMenu) 
        guiSetFont(QuitButton, "default-bold-small") 
        guiSetProperty(QuitButton, "NormalTextColour", "FFFF0000") 
        Cartlist = guiCreateGridList(172, 551, 269, 122, false, AmmoMenu) 
        Weaponcolumn = guiGridListAddColumn(Cartlist, "Weapon", 0.3) 
        Amountcolumn = guiGridListAddColumn(Cartlist, "Amount", 0.3) 
        Pricecolumn = guiGridListAddColumn(Cartlist, "Price", 0.3)  
        AddcartButton = guiCreateButton(314, 508, 89, 27, "Add to cart", false, AmmoMenu) 
        WeaponInfo = guiCreateScrollPane(177, 179, 223, 238, false, AmmoMenu)     
        Infolabel = guiCreateLabel(17, 150, 150, 19, "Press icon for information", false, AmmoMenu) 
        guiSetFont(Infolabel, "default-bold-small") 
        guiGridListSetSortingEnabled (Cartlist,false) 
        guiSetVisible(AmmoMenu,false) 
    end 
) 
  
  
  
  
function chooseweapon() 
    if source == TazerImage then 
        WeaponChosen = 23 
    end 
    if source == PistolImage then 
        WeaponChosen = 22 
    end 
    if source == DeserteagleImage then 
        WeaponChosen = 24 
    end 
    if source == Ak47Image then 
        WeaponChosen = 30 
    end 
    if source == UziImage then 
        WeaponChosen = 28 
    end 
    if source == Tec9Image then 
        WeaponChosen = 32 
    end 
    if source == Mp5Image then 
        WeaponChosen = 29 
    end 
    if source == Spas12Image then 
        WeaponChosen = 27 
    end 
    if source == M4Image then 
        WeaponChosen = 31 
    end 
    if source == BuyButton then 
        if guiGridListGetRowCount(Cartlist) > 0 then 
            for i,price in pairs(pricetable) do 
                local totalprice = totalprice + price  
            end 
            if getPlayerMoney(localPlayer) >= totalprice then 
                for index,id in pairs(weapontable) do 
                    for i,ammo in pairs(ammotable) do 
                        triggerServerEvent("giveweapon",resourceRoot,localPlayer,id,ammo) 
                    end 
                end 
                takePlayerMoney(totalprice) 
            else 
            outputChatBox("You don't have enough money!",255,0,0) 
            end 
        else 
            outputChatBox("You don't have anything in your cart!",255,0,0) 
        end 
    end 
    if source == QuitButton then 
        showCursor(false) 
        guiSetVisible(AmmoMenu,false)   
        guiGridListClear (Cartlist) 
        guiSetText (AmmountTextbox,"") 
        for id,weaponid in pairs(weapontable) do weapontable[k]=nil end 
        for id,ammo in pairs(ammotable) do ammotable[k]=nil end 
        for id,price in pairs(pricetable) do pricetable[k]=nil end 
    end 
    if source == AddcartButton then 
        if WeaponChosen then 
            if tonumber(guiGetText(AmmountTextbox)) ~= nil then 
                if tonumber(guiGetText(AmmountTextbox)) > 0 then 
                    if tonumber(guiGetText(AmmountTextbox)) == math.floor(guiGetText(AmmountTextbox)) then 
                        local row = guiGridListAddRow(Cartlist) 
                        table.insert(weapontable,WeaponChosen) 
                        table.insert(ammotable,guiGetText(AmmountTextbox)) 
                        table.insert(pricetable,math.floor(10 * guiGetText(AmmountTextbox))) 
                        guiGridListSetItemText(Cartlist,row,Weaponcolumn,""..getWeaponNameFromID(WeaponChosen),false,false) 
                        guiGridListSetItemText(Cartlist,row,Amountcolumn,""..guiGetText(AmmountTextbox),false,false) 
                        guiGridListSetItemText(Cartlist,row,Pricecolumn,"$"..tostring(math.floor(10 * guiGetText(AmmountTextbox))),false,false)              
                    else  
                        outputChatBox("Please enter a integer!",255,0,0) 
                    end 
                else 
                    outputChatBox("Please enter a positive number!",255,0,0) 
                end 
            else 
                outputChatBox("Please enter a integer as the ammount of ammo",255,0,0) 
            end 
        else 
            outputChatBox("Please choose a weapon by clicking on the icon of it",255,0,0) 
        end 
    end 
end  
addEventHandler("onClientGUIClick",resourceRoot,chooseweapon) 
  
addEvent("showweaponGUI",true) 
  
function showGUI() 
    totalprice = 0 
    outputChatBox("true") 
    showCursor(true) 
    guiSetVisible(AmmoMenu,true) 
end 
addEventHandler("showweaponGUI",getRootElement(),showGUI) 

It doesnt take money, and gives wrong weapon and wrong amount of ammo

Link to comment
Wheres your server side?

and dont take money on client side you have to take them on server side

This is the serverside

local weaponmarker = createMarker(296.42404,-38.29199,1000.5,"cylinder",1.5,0,255,0) 
setElementInterior(weaponmarker,1) 
  
function showClientGUI(marker) 
    if marker == weaponmarker then 
        if getTeamName(getPlayerTeam(source)) ~= "Criminal" then 
            triggerClientEvent(source,"showweaponGUI",source) 
        else 
            outputChatBox("Ammunation won't sell guns&ammos to criminals, buy it in black market",source,255,0,0) 
        end 
    end 
end 
addEventHandler("onPlayerMarkerHit",getRootElement(),showClientGUI) 
  
addEvent("giveweapon",true) 
addEventHandler("giveweapon",getRootElement(), 
function(buyer,weapon,quantity) 
        giveWeapon(buyer,weapon,quantity) 
end) 

Link to comment

CLIENT:

    if source == QuitButton then 
        showCursor(false) 
        guiSetVisible(AmmoMenu,false)   
        guiGridListClear (Cartlist) 
        guiSetText (AmmountTextbox,"") 
        for id,weaponid in pairs(weapontable) do table.remove(weapontable, id) end 
        for id,ammo in pairs(ammotable) do table.remove(ammotable, id) end 
        for id,price in pairs(pricetable) do table.remove(pricetable, id) end 
    end 

SERVER:

local weaponmarker = createMarker(296.42404,-38.29199,1000.5,"cylinder",1.5,0,255,0) 
setElementInterior(weaponmarker,1) 
  
function showClientGUI(marker) 
    if marker == weaponmarker then 
        if getPlayerTeam(source) and getTeamName(getPlayerTeam(source)) ~= "Criminal" then 
            triggerClientEvent(source,"showweaponGUI",source) 
        else 
            outputChatBox("Ammunation won't sell guns&ammos to criminals, buy it in black market",source,255,0,0) 
        end 
    end 
end 
addEventHandler("onPlayerMarkerHit",getRootElement(),showClientGUI) 
  
addEvent("giveweapon",true) 
addEventHandler("giveweapon",getRootElement(), 
function(buyer,weapon,quantity) 
        giveWeapon(buyer,weapon,quantity,true) 
end) 

Link to comment
CLIENT:
    if source == QuitButton then 
        showCursor(false) 
        guiSetVisible(AmmoMenu,false)   
        guiGridListClear (Cartlist) 
        guiSetText (AmmountTextbox,"") 
        for id,weaponid in pairs(weapontable) do table.remove(weapontable, id) end 
        for id,ammo in pairs(ammotable) do table.remove(ammotable, id) end 
        for id,price in pairs(pricetable) do table.remove(pricetable, id) end 
    end 

SERVER:

local weaponmarker = createMarker(296.42404,-38.29199,1000.5,"cylinder",1.5,0,255,0) 
setElementInterior(weaponmarker,1) 
  
function showClientGUI(marker) 
    if marker == weaponmarker then 
        if getPlayerTeam(source) and getTeamName(getPlayerTeam(source)) ~= "Criminal" then 
            triggerClientEvent(source,"showweaponGUI",source) 
        else 
            outputChatBox("Ammunation won't sell guns&ammos to criminals, buy it in black market",source,255,0,0) 
        end 
    end 
end 
addEventHandler("onPlayerMarkerHit",getRootElement(),showClientGUI) 
  
addEvent("giveweapon",true) 
addEventHandler("giveweapon",getRootElement(), 
function(buyer,weapon,quantity) 
        giveWeapon(buyer,weapon,quantity,true) 
end) 

Thank you very much, i will try this out as soon as possible :D

Link to comment
  • 3 weeks later...
CLIENT:
    if source == QuitButton then 
        showCursor(false) 
        guiSetVisible(AmmoMenu,false)   
        guiGridListClear (Cartlist) 
        guiSetText (AmmountTextbox,"") 
        for id,weaponid in pairs(weapontable) do table.remove(weapontable, id) end 
        for id,ammo in pairs(ammotable) do table.remove(ammotable, id) end 
        for id,price in pairs(pricetable) do table.remove(pricetable, id) end 
    end 

SERVER:

local weaponmarker = createMarker(296.42404,-38.29199,1000.5,"cylinder",1.5,0,255,0) 
setElementInterior(weaponmarker,1) 
  
function showClientGUI(marker) 
    if marker == weaponmarker then 
        if getPlayerTeam(source) and getTeamName(getPlayerTeam(source)) ~= "Criminal" then 
            triggerClientEvent(source,"showweaponGUI",source) 
        else 
            outputChatBox("Ammunation won't sell guns&ammos to criminals, buy it in black market",source,255,0,0) 
        end 
    end 
end 
addEventHandler("onPlayerMarkerHit",getRootElement(),showClientGUI) 
  
addEvent("giveweapon",true) 
addEventHandler("giveweapon",getRootElement(), 
function(buyer,weapon,quantity) 
        giveWeapon(buyer,weapon,quantity,true) 
end) 

Still gives wrong number of ammo when I am putting 2+ items in the cart, when I put only one in the cart its fine

Link to comment
CLIENT:
    if source == QuitButton then 
        showCursor(false) 
        guiSetVisible(AmmoMenu,false)   
        guiGridListClear (Cartlist) 
        guiSetText (AmmountTextbox,"") 
        for id,weaponid in pairs(weapontable) do table.remove(weapontable, id) end 
        for id,ammo in pairs(ammotable) do table.remove(ammotable, id) end 
        for id,price in pairs(pricetable) do table.remove(pricetable, id) end 
    end 

SERVER:

local weaponmarker = createMarker(296.42404,-38.29199,1000.5,"cylinder",1.5,0,255,0) 
setElementInterior(weaponmarker,1) 
  
function showClientGUI(marker) 
    if marker == weaponmarker then 
        if getPlayerTeam(source) and getTeamName(getPlayerTeam(source)) ~= "Criminal" then 
            triggerClientEvent(source,"showweaponGUI",source) 
        else 
            outputChatBox("Ammunation won't sell guns&ammos to criminals, buy it in black market",source,255,0,0) 
        end 
    end 
end 
addEventHandler("onPlayerMarkerHit",getRootElement(),showClientGUI) 
  
addEvent("giveweapon",true) 
addEventHandler("giveweapon",getRootElement(), 
function(buyer,weapon,quantity) 
        giveWeapon(buyer,weapon,quantity,true) 
end) 

Still gives wrong number of ammo when I am putting 2+ items in the cart, when I put only one in the cart its fine

Anyone please help :/

Link to comment
CLIENT:
    if source == QuitButton then 
        showCursor(false) 
        guiSetVisible(AmmoMenu,false)   
        guiGridListClear (Cartlist) 
        guiSetText (AmmountTextbox,"") 
        for id,weaponid in pairs(weapontable) do table.remove(weapontable, id) end 
        for id,ammo in pairs(ammotable) do table.remove(ammotable, id) end 
        for id,price in pairs(pricetable) do table.remove(pricetable, id) end 
    end 

SERVER:

local weaponmarker = createMarker(296.42404,-38.29199,1000.5,"cylinder",1.5,0,255,0) 
setElementInterior(weaponmarker,1) 
  
function showClientGUI(marker) 
    if marker == weaponmarker then 
        if getPlayerTeam(source) and getTeamName(getPlayerTeam(source)) ~= "Criminal" then 
            triggerClientEvent(source,"showweaponGUI",source) 
        else 
            outputChatBox("Ammunation won't sell guns&ammos to criminals, buy it in black market",source,255,0,0) 
        end 
    end 
end 
addEventHandler("onPlayerMarkerHit",getRootElement(),showClientGUI) 
  
addEvent("giveweapon",true) 
addEventHandler("giveweapon",getRootElement(), 
function(buyer,weapon,quantity) 
        giveWeapon(buyer,weapon,quantity,true) 
end) 

Still gives wrong number of ammo when I am putting 2+ items in the cart, when I put only one in the cart its fine

Anyone please help :/

Anyone?

Link to comment

Try this copy and past my code i changed many things

function table.size(tab) 
    local length = 0 
    for _ in pairs(tab) do length = length + 1 end 
    return length 
end 
  
  
addEventHandler("onClientResourceStart", resourceRoot, 
    function() 
        local screenW, screenH = guiGetScreenSize() 
        AmmoMenu = guiCreateWindow((screenW - 465) / 2, (screenH - 834) / 2, 465, 834, "Ammu-Nation", false) 
        guiWindowSetSizable(AmmoMenu, false) 
        AmmuNationImage = guiCreateStaticImage(67, 54, 332, 88, ":weapons/ammu-nation.png", false, AmmoMenu) 
        TazerImage = guiCreateStaticImage(52, 179, 70, 56, ":weapons/Tazer.png", false, AmmoMenu) 
        PistolImage = guiCreateStaticImage(52, 253, 70, 59, ":weapons/Pistol.png", false, AmmoMenu) 
        DeserteagleImage = guiCreateStaticImage(52, 328, 70, 55, ":weapons/Desert_Eagle.png", false, AmmoMenu) 
        Ak47Image = guiCreateStaticImage(33, 739, 113, 44, ":weapons/ak47.png", false, AmmoMenu) 
        UziImage = guiCreateStaticImage(53, 401, 69, 52, ":weapons/uzi.png", false, AmmoMenu) 
        Tec9Image = guiCreateStaticImage(53, 468, 69, 50, ":weapons/tec9.png", false, AmmoMenu) 
        Mp5Image = guiCreateStaticImage(46, 532, 86, 45, ":weapons/mp5.png", false, AmmoMenu) 
        Spas12Image = guiCreateStaticImage(33, 592, 113, 50, ":weapons/Spas-12.png", false, AmmoMenu) 
        M4Image = guiCreateStaticImage(33, 663, 114, 55, ":weapons/M4A1.png", false, AmmoMenu) 
        AmmountLabel = guiCreateLabel(196, 478, 55, 17, "Ammount", false, AmmoMenu) 
        AmmountTextbox = guiCreateEdit(172, 508, 104, 24, "", false, AmmoMenu) 
        guiEditSetMaxLength(AmmountTextbox, 6) 
        BuyButton = guiCreateButton(172, 729, 126, 59, "Buy Cart", false, AmmoMenu) 
        guiSetFont(BuyButton, "default-bold-small") 
        guiSetProperty(BuyButton, "NormalTextColour", "FF0000FF") 
        QuitButton = guiCreateButton(315, 729, 126, 59, "Quit", false, AmmoMenu) 
        guiSetFont(QuitButton, "default-bold-small") 
        guiSetProperty(QuitButton, "NormalTextColour", "FFFF0000") 
        Cartlist = guiCreateGridList(172, 551, 269, 122, false, AmmoMenu) 
        Weaponcolumn = guiGridListAddColumn(Cartlist, "Weapon", 0.3) 
        Amountcolumn = guiGridListAddColumn(Cartlist, "Amount", 0.3) 
        Pricecolumn = guiGridListAddColumn(Cartlist, "Price", 0.3) 
        AddcartButton = guiCreateButton(314, 508, 89, 27, "Add to cart", false, AmmoMenu) 
        WeaponInfo = guiCreateScrollPane(177, 179, 223, 238, false, AmmoMenu)     
        Infolabel = guiCreateLabel(17, 150, 150, 19, "Press icon for information", false, AmmoMenu) 
        guiSetFont(Infolabel, "default-bold-small") 
        guiGridListSetSortingEnabled (Cartlist,false) 
        guiSetVisible(AmmoMenu,false) 
    end 
) 
  
  
  
  
function chooseweapon() 
    if source == TazerImage then 
        WeaponChosen = 23 
    elseif source == PistolImage then 
        WeaponChosen = 22 
    elseif source == DeserteagleImage then 
        WeaponChosen = 24 
    elseif source == Ak47Image then 
        WeaponChosen = 30 
    elseif source == UziImage then 
        WeaponChosen = 28 
    elseif source == Tec9Image then 
        WeaponChosen = 32 
    elseif source == Mp5Image then 
        WeaponChosen = 29 
    elseif source == Spas12Image then 
        WeaponChosen = 27 
    elseif source == M4Image then 
        WeaponChosen = 31 
     
     
    -- Add weapons  
    elseif source == AddcartButton then 
        if WeaponChosen then 
            if tonumber(guiGetText(AmmountTextbox)) ~= nil then 
                if tonumber(guiGetText(AmmountTextbox)) > 0 then 
                    if tonumber(guiGetText(AmmountTextbox)) == math.floor(guiGetText(AmmountTextbox)) then 
                        local row = guiGridListAddRow(Cartlist) 
                        table.insert(weapontable,{WeaponChosen,guiGetText(AmmountTextbox),math.floor(10 * guiGetText(AmmountTextbox))}) 
                        guiGridListSetItemText(Cartlist,row,Weaponcolumn,""..getWeaponNameFromID(WeaponChosen),false,false) 
                        guiGridListSetItemText(Cartlist,row,Amountcolumn,""..guiGetText(AmmountTextbox),false,false) 
                        guiGridListSetItemText(Cartlist,row,Pricecolumn,"$"..tonumber(math.floor(10 * guiGetText(AmmountTextbox))),false,false)             
                    else 
                        outputChatBox("Please enter a integer!",255,0,0) 
                    end 
                else 
                    outputChatBox("Please enter a positive number!",255,0,0) 
                end 
            else 
                outputChatBox("Please enter a integer as the ammount of ammo",255,0,0) 
            end 
        else 
            outputChatBox("Please choose a weapon by clicking on the icon of it",255,0,0) 
    end 
         
    elseif source == BuyButton then 
        if guiGridListGetRowCount(Cartlist) > 0 then 
        for i , v in pairs (weapontable) do  
            local totalprice = totalprice + tonumber(v[3]) 
                if getPlayerMoney(localPlayer) >= totalprice then 
                    triggerServerEvent("giveweapon",localPlayer,v[1],v[2]) 
                    takePlayerMoney(totalprice) 
                else 
                outputChatBox("You don't have enough money!",255,0,0) 
            end 
        end  
        else 
        outputChatBox("You don't have anything in your cart!",255,0,0) 
    end  
     
    elseif source == QuitButton then 
        showCursor(false) 
        guiSetVisible(AmmoMenu,false)   
        guiGridListClear (Cartlist) 
        guiSetText (AmmountTextbox,"") 
    end  
end 
addEventHandler("onClientGUIClick",resourceRoot,chooseweapon) 
  
  
function showGUI() 
    weapontable = {} 
    totalprice = 0 
    showCursor(true) 
    guiSetVisible(AmmoMenu,true) 
end 
addEvent("showweaponGUI",true) 
addEventHandler("showweaponGUI",getRootElement(),showGUI) 

Link to comment
Try this copy and past my code i changed many things
function table.size(tab) 
    local length = 0 
    for _ in pairs(tab) do length = length + 1 end 
    return length 
end 
  
  
addEventHandler("onClientResourceStart", resourceRoot, 
    function() 
        local screenW, screenH = guiGetScreenSize() 
        AmmoMenu = guiCreateWindow((screenW - 465) / 2, (screenH - 834) / 2, 465, 834, "Ammu-Nation", false) 
        guiWindowSetSizable(AmmoMenu, false) 
        AmmuNationImage = guiCreateStaticImage(67, 54, 332, 88, ":weapons/ammu-nation.png", false, AmmoMenu) 
        TazerImage = guiCreateStaticImage(52, 179, 70, 56, ":weapons/Tazer.png", false, AmmoMenu) 
        PistolImage = guiCreateStaticImage(52, 253, 70, 59, ":weapons/Pistol.png", false, AmmoMenu) 
        DeserteagleImage = guiCreateStaticImage(52, 328, 70, 55, ":weapons/Desert_Eagle.png", false, AmmoMenu) 
        Ak47Image = guiCreateStaticImage(33, 739, 113, 44, ":weapons/ak47.png", false, AmmoMenu) 
        UziImage = guiCreateStaticImage(53, 401, 69, 52, ":weapons/uzi.png", false, AmmoMenu) 
        Tec9Image = guiCreateStaticImage(53, 468, 69, 50, ":weapons/tec9.png", false, AmmoMenu) 
        Mp5Image = guiCreateStaticImage(46, 532, 86, 45, ":weapons/mp5.png", false, AmmoMenu) 
        Spas12Image = guiCreateStaticImage(33, 592, 113, 50, ":weapons/Spas-12.png", false, AmmoMenu) 
        M4Image = guiCreateStaticImage(33, 663, 114, 55, ":weapons/M4A1.png", false, AmmoMenu) 
        AmmountLabel = guiCreateLabel(196, 478, 55, 17, "Ammount", false, AmmoMenu) 
        AmmountTextbox = guiCreateEdit(172, 508, 104, 24, "", false, AmmoMenu) 
        guiEditSetMaxLength(AmmountTextbox, 6) 
        BuyButton = guiCreateButton(172, 729, 126, 59, "Buy Cart", false, AmmoMenu) 
        guiSetFont(BuyButton, "default-bold-small") 
        guiSetProperty(BuyButton, "NormalTextColour", "FF0000FF") 
        QuitButton = guiCreateButton(315, 729, 126, 59, "Quit", false, AmmoMenu) 
        guiSetFont(QuitButton, "default-bold-small") 
        guiSetProperty(QuitButton, "NormalTextColour", "FFFF0000") 
        Cartlist = guiCreateGridList(172, 551, 269, 122, false, AmmoMenu) 
        Weaponcolumn = guiGridListAddColumn(Cartlist, "Weapon", 0.3) 
        Amountcolumn = guiGridListAddColumn(Cartlist, "Amount", 0.3) 
        Pricecolumn = guiGridListAddColumn(Cartlist, "Price", 0.3) 
        AddcartButton = guiCreateButton(314, 508, 89, 27, "Add to cart", false, AmmoMenu) 
        WeaponInfo = guiCreateScrollPane(177, 179, 223, 238, false, AmmoMenu)     
        Infolabel = guiCreateLabel(17, 150, 150, 19, "Press icon for information", false, AmmoMenu) 
        guiSetFont(Infolabel, "default-bold-small") 
        guiGridListSetSortingEnabled (Cartlist,false) 
        guiSetVisible(AmmoMenu,false) 
    end 
) 
  
  
  
  
function chooseweapon() 
    if source == TazerImage then 
        WeaponChosen = 23 
    elseif source == PistolImage then 
        WeaponChosen = 22 
    elseif source == DeserteagleImage then 
        WeaponChosen = 24 
    elseif source == Ak47Image then 
        WeaponChosen = 30 
    elseif source == UziImage then 
        WeaponChosen = 28 
    elseif source == Tec9Image then 
        WeaponChosen = 32 
    elseif source == Mp5Image then 
        WeaponChosen = 29 
    elseif source == Spas12Image then 
        WeaponChosen = 27 
    elseif source == M4Image then 
        WeaponChosen = 31 
     
     
    -- Add weapons  
    elseif source == AddcartButton then 
        if WeaponChosen then 
            if tonumber(guiGetText(AmmountTextbox)) ~= nil then 
                if tonumber(guiGetText(AmmountTextbox)) > 0 then 
                    if tonumber(guiGetText(AmmountTextbox)) == math.floor(guiGetText(AmmountTextbox)) then 
                        local row = guiGridListAddRow(Cartlist) 
                        table.insert(weapontable,{WeaponChosen,guiGetText(AmmountTextbox),math.floor(10 * guiGetText(AmmountTextbox))}) 
                        guiGridListSetItemText(Cartlist,row,Weaponcolumn,""..getWeaponNameFromID(WeaponChosen),false,false) 
                        guiGridListSetItemText(Cartlist,row,Amountcolumn,""..guiGetText(AmmountTextbox),false,false) 
                        guiGridListSetItemText(Cartlist,row,Pricecolumn,"$"..tonumber(math.floor(10 * guiGetText(AmmountTextbox))),false,false)             
                    else 
                        outputChatBox("Please enter a integer!",255,0,0) 
                    end 
                else 
                    outputChatBox("Please enter a positive number!",255,0,0) 
                end 
            else 
                outputChatBox("Please enter a integer as the ammount of ammo",255,0,0) 
            end 
        else 
            outputChatBox("Please choose a weapon by clicking on the icon of it",255,0,0) 
    end 
         
    elseif source == BuyButton then 
        if guiGridListGetRowCount(Cartlist) > 0 then 
        for i , v in pairs (weapontable) do  
            local totalprice = totalprice + tonumber(v[3]) 
                if getPlayerMoney(localPlayer) >= totalprice then 
                    triggerServerEvent("giveweapon",localPlayer,v[1],v[2]) 
                    takePlayerMoney(totalprice) 
                else 
                outputChatBox("You don't have enough money!",255,0,0) 
            end 
        end  
        else 
        outputChatBox("You don't have anything in your cart!",255,0,0) 
    end  
     
    elseif source == QuitButton then 
        showCursor(false) 
        guiSetVisible(AmmoMenu,false)   
        guiGridListClear (Cartlist) 
        guiSetText (AmmountTextbox,"") 
    end  
end 
addEventHandler("onClientGUIClick",resourceRoot,chooseweapon) 
  
  
function showGUI() 
    weapontable = {} 
    totalprice = 0 
    showCursor(true) 
    guiSetVisible(AmmoMenu,true) 
end 
addEvent("showweaponGUI",true) 
addEventHandler("showweaponGUI",getRootElement(),showGUI) 

Thank you, I find this is a nicer way to do it :D , I will try out later

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