jingzhi Posted March 13, 2015 Share Posted March 13, 2015 So I'm trying to make a script for ammunation using gridlist and saving values in tables, I wrote this script, when I test it my computer dies, please help to check where's wrong 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 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 local count = 0 while count <= guiGridListGetRowCount(Cartlist) do local count = count + 1 for count,price in pairs(pricetable) do local totalprice = totalprice + price if count == guiGridListGetRowCount(Cartlist) then if getPlayerMoney(localPlayer) >= totalprice then while count <= guiGridListGetRowCount(Cartlist) do for count,id in pairs(weapontable) do for count,ammo in pairs(ammotable) do triggerServerEvent(localPlayer,"giveweapon",resourceRoot,localPlayer,id,ammo) end end end takePlayerMoney(localPlayer,totalprice) else outputChatBox("You don't have enough money!",localPlayer,255,0,0) end end end end takePlayerMoney(buyer,totalprice) 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 getElementData(localPlayer,"WeaponChosen") ~= false 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,getElementData(localPlayer,"WeaponChosen")) table.insert(ammotable,guiGetText(AmmountTextbox)) table.insert(pricetable,math.floor(10 * guiGetText(AmmountTextbox))) 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!",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
Addlibs Posted March 13, 2015 Share Posted March 13, 2015 if source == BuyButton then if guiGridListGetRowCount(Cartlist) > 0 then local count = 0 while count <= guiGridListGetRowCount(Cartlist) do local count = count + 1 for count,price in pairs(pricetable) do local totalprice = totalprice + price if count == guiGridListGetRowCount(Cartlist) then if getPlayerMoney(localPlayer) >= totalprice then while count <= guiGridListGetRowCount(Cartlist) do for count,id in pairs(weapontable) do for count,ammo in pairs(ammotable) do triggerServerEvent(localPlayer,"giveweapon",resourceRoot,localPlayer,id,ammo) end end end takePlayerMoney(localPlayer,totalprice) else outputChatBox("You don't have enough money!",localPlayer,255,0,0) end end end end takePlayerMoney(buyer,totalprice) else outputChatBox("You don't have anything in your cart!",255,0,0) end end Could you explain what you were trying to achieve with this? I find it hard to find my way in this script. Link to comment
jingzhi Posted March 14, 2015 Author Share Posted March 14, 2015 if source == BuyButton then if guiGridListGetRowCount(Cartlist) > 0 then local count = 0 while count <= guiGridListGetRowCount(Cartlist) do local count = count + 1 for count,price in pairs(pricetable) do local totalprice = totalprice + price if count == guiGridListGetRowCount(Cartlist) then if getPlayerMoney(localPlayer) >= totalprice then while count <= guiGridListGetRowCount(Cartlist) do for count,id in pairs(weapontable) do for count,ammo in pairs(ammotable) do triggerServerEvent(localPlayer,"giveweapon",resourceRoot,localPlayer,id,ammo) end end end takePlayerMoney(localPlayer,totalprice) else outputChatBox("You don't have enough money!",localPlayer,255,0,0) end end end end takePlayerMoney(buyer,totalprice) else outputChatBox("You don't have anything in your cart!",255,0,0) end end Could you explain what you were trying to achieve with this? I find it hard to find my way in this script. Hey, the gridlist is a cart, when the player press buy button, we need to give him the weapons in the cart and take his money Link to comment
Addlibs Posted March 14, 2015 Share Posted March 14, 2015 I mean, give more comments about what the 'while' statements do - I keep getting lost in that script, sorry. Link to comment
jingzhi Posted March 14, 2015 Author Share Posted March 14, 2015 I mean, give more comments about what the 'while' statements do - I keep getting lost in that script, sorry. I read on the wiki, that "while" loops the function when it meet the circumstance that follows it Link to comment
Addlibs Posted March 14, 2015 Share Posted March 14, 2015 I know what a 'while' statement does, but I don't understand your usage of it. Could you add some comments within the code? Link to comment
JR10 Posted March 15, 2015 Share Posted March 15, 2015 You're redefining count as a local variable inside the while loop, so eventually the count variable never gets edited. You end up with an infinite while loop. Aside from that, you're using the same named variable in all your loops, so they all override their predecessors. I've changed the first count to index, if this doesn't work as you expect, explain further what you're trying to achieve. if source == BuyButton then if guiGridListGetRowCount(Cartlist) > 0 then local index = 0 while index <= guiGridListGetRowCount(Cartlist) do index = index + 1 for count,price in pairs(pricetable) do local totalprice = totalprice + price if count == guiGridListGetRowCount(Cartlist) then if getPlayerMoney(localPlayer) >= totalprice then while count <= guiGridListGetRowCount(Cartlist) do for count,id in pairs(weapontable) do for count,ammo in pairs(ammotable) do triggerServerEvent(localPlayer,"giveweapon",resourceRoot,localPlayer,id,ammo) end end end takePlayerMoney(localPlayer,totalprice) else outputChatBox("You don't have enough money!",localPlayer,255,0,0) end end end end takePlayerMoney(buyer,totalprice) else outputChatBox("You don't have anything in your cart!",255,0,0) end end Link to comment
jingzhi Posted March 15, 2015 Author Share Posted March 15, 2015 You're redefining count as a local variable inside the while loop, so eventually the count variable never gets edited. You end up with an infinite while loop.Aside from that, you're using the same named variable in all your loops, so they all override their predecessors. I've changed the first count to index, if this doesn't work as you expect, explain further what you're trying to achieve. if source == BuyButton then if guiGridListGetRowCount(Cartlist) > 0 then local index = 0 while index <= guiGridListGetRowCount(Cartlist) do index = index + 1 for count,price in pairs(pricetable) do local totalprice = totalprice + price if count == guiGridListGetRowCount(Cartlist) then if getPlayerMoney(localPlayer) >= totalprice then while count <= guiGridListGetRowCount(Cartlist) do for count,id in pairs(weapontable) do for count,ammo in pairs(ammotable) do triggerServerEvent(localPlayer,"giveweapon",resourceRoot,localPlayer,id,ammo) end end end takePlayerMoney(localPlayer,totalprice) else outputChatBox("You don't have enough money!",localPlayer,255,0,0) end end end end takePlayerMoney(buyer,totalprice) else outputChatBox("You don't have anything in your cart!",255,0,0) end end Hey JR10, thank you for your help again, now it's partly working, but the big problem remains. What I'm trying to do is, to make a ammunation system with cart like this It should take the total money from player and give the player weapons he bought that was in the cart Thank you again Link to comment
jingzhi Posted March 23, 2015 Author Share Posted March 23, 2015 You're redefining count as a local variable inside the while loop, so eventually the count variable never gets edited. You end up with an infinite while loop.Aside from that, you're using the same named variable in all your loops, so they all override their predecessors. I've changed the first count to index, if this doesn't work as you expect, explain further what you're trying to achieve. if source == BuyButton then if guiGridListGetRowCount(Cartlist) > 0 then local index = 0 while index <= guiGridListGetRowCount(Cartlist) do index = index + 1 for count,price in pairs(pricetable) do local totalprice = totalprice + price if count == guiGridListGetRowCount(Cartlist) then if getPlayerMoney(localPlayer) >= totalprice then while count <= guiGridListGetRowCount(Cartlist) do for count,id in pairs(weapontable) do for count,ammo in pairs(ammotable) do triggerServerEvent(localPlayer,"giveweapon",resourceRoot,localPlayer,id,ammo) end end end takePlayerMoney(localPlayer,totalprice) else outputChatBox("You don't have enough money!",localPlayer,255,0,0) end end end end takePlayerMoney(buyer,totalprice) else outputChatBox("You don't have anything in your cart!",255,0,0) end end Sorry, last pic was token badly, basically what I want to do is to make an ammunition with cart system, when you check out you pay the total money and get all the weapon you got in your cart, please help, I'm stuck here for long time Link to comment
jingzhi Posted March 24, 2015 Author Share Posted March 24, 2015 Again, someone please help, if you understand, i've been stuck here for a long time Link to comment
JR10 Posted March 25, 2015 Share Posted March 25, 2015 And what is the problem? You just need to store what the player buys in a table (client-side would work) and when he buys, loop through the table and give him the weapons taking money away (make sure to check if he can afford the weapons, since we're using client-side). Link to comment
jingzhi Posted March 25, 2015 Author Share Posted March 25, 2015 And what is the problem? You just need to store what the player buys in a table (client-side would work) and when he buys, loop through the table and give him the weapons taking money away (make sure to check if he can afford the weapons, since we're using client-side). I am not pretty sure how to store and loop them when the player buys them, can you please help on that, thank you again! Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now