Jump to content

Need help with the ammunation and cart system


jingzhi

Recommended Posts

I am making a script for ammunation, and I want to also make a cart for the ammunation. I made a gridlist for cart, and a buy button. Is there any way to make it like, when I press "Buy" and then i buy every item in the cart? And is there anyway to make it like, if there is already this kind of ammo in the cart, it won't create another row for these ammo, but will combine with the other same type of ammo? (like i have 10 m4 now in my cart, i want to add 10 more, then i have 20 m4 ammos but not 2 rows of 10 m4 ammos) Please help, thank y'all :)

Here is my scripts

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 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,quantity) 
    if getPlayerMoney(buyer) >= math.floor(quantity * 10) then 
        giveWeapon(buyer,getElementData(buyer,"WeaponChosen"),quantity) 
        takePlayerMoney(buyer,math.floor(quantity * 10)) 
    else 
        outputChatBox("You don't have enough money for this",buyer,255,0,0) 
    end 
end) 

Client

  
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 
        setElementData(localPlayer,"WeaponChosen",23) 
    end 
    if source == PistolImage then 
        setElementData(localPlayer,"WeaponChosen",22) 
    end 
    if source == DeserteagleImage then 
        setElementData(localPlayer,"WeaponChosen",24) 
    end 
    if source == Ak47Image then 
        setElementData(localPlayer,"WeaponChosen",30) 
    end 
    if source == UziImage then 
        setElementData(localPlayer,"WeaponChosen",28) 
    end 
    if source == Tec9Image then 
        setElementData(localPlayer,"WeaponChosen",32) 
    end 
    if source == Mp5Image then 
        setElementData(localPlayer,"WeaponChosen",29) 
    end 
    if source == Spas12Image then 
        setElementData(localPlayer,"WeaponChosen",27) 
    end 
    if source == M4Image then 
        setElementData(localPlayer,"WeaponChosen",31) 
    end 
    if source == BuyButton then 
        if guiGridListGetRowCount(Cartlist) > 0 then 
            triggerServerEvent("GiveWeapon",resourceRoot,localPlayer,guiGetText(AmmountTextbox)) 
        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,"") 
    end 
    if source == AddcartButton then 
    if getElementData(localPlayer,"WeaponChosen") ~= false then 
            if tonumber(guiGetText(AmmountTextbox)) > 0 and tonumber(guiGetText(AmmountTextbox)) ~= nil then  
                local row = guiGridListAddRow(Cartlist) 
                guiGridListSetItemText(Cartlist,row,Weaponcolumn,""..getWeaponNameFromID(getElementData(localPlayer,"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 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() 
    outputChatBox("true") 
    showCursor(true) 
    guiSetVisible(AmmoMenu,true) 
end 
addEventHandler("showweaponGUI",getRootElement(),showGUI) 
     
  

Link to comment
function getCartTotalCost(cart) 
    local totalCost = 0 
    for k, v in pairs(cart) do --used pairs, in case the keys/indices aren't in order 
        totalCost = totalCost + prices[v.weapon] --update the last bit according to your script 
    end 
    return totalCost 
end 

Ps. why do you use element data to store the chosen weapon? Wouldn't it be easier to make it a variable?

Link to comment
function getCartTotalCost(cart) 
    local totalCost = 0 
    for k, v in pairs(cart) do --used pairs, in case the keys/indices aren't in order 
        totalCost = totalCost + prices[v.weapon] --update the last bit according to your script 
    end 
    return totalCost 
end 

Ps. why do you use element data to store the chosen weapon? Wouldn't it be easier to make it a variable?

Yeah, I guess i should use variables :) and thank you

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