Drakath Posted April 19, 2014 Share Posted April 19, 2014 I need the script to store some weapons into a table and then give those weapons to a player and remove them from the table. It is easy to store one weapon into a table but how can I store multiple weapons? This is the part of my script: local weaponID = getWeaponIDFromName(weapon) table.insert(Cart.weapons, weaponID) elseif tonumber(Cart.weapons) then --This means that if something is already stored in the table. table.insert(Cart.weapons, Cart.weapons and weaponID) However it doesn't work. "Cart.weapons and weaponID" does not work. If there isn't any way to make it work, I also have a grid list with those weapons but how can I import them to table? Link to comment
Saml1er Posted April 19, 2014 Share Posted April 19, 2014 local weapons = { } local ID = getWeaponIDFromName(weapon) if #weapons == 0 then -- means if the table is empty -- do your stuff else -- if the table is not empty weapons[#weapons+1] = {weaponName, ID } -- insert it into table ( row ), you can use this same method to insert more end P.S I play on your server sometimes. Keep up the good work. EDIT: Use #TABLENAME to get the table length and you can also get the weapon by: local weaponName = weapons[1][1] local weaponID = weapons[1][2] ----- for random local r = math.random (1,#weapons) local weaponName = weapons[r][1] local weaponID = weapons[r][2] Link to comment
Drakath Posted April 19, 2014 Author Share Posted April 19, 2014 That's not really what I wanted. Lets say the table already has weapon id: 22 in it. I want to add another ID: 30 to it. How do I do that? Link to comment
Saml1er Posted April 19, 2014 Share Posted April 19, 2014 That's not really what I wanted. Lets say the table already has weapon id: 22 in it. I want to add another ID: 30 to it. How do I do that? local ID = 30 local weapons = { } weapons[#weapons+1] = ID -- this will insert 30 in it. It doesn't matter if the table has some value, you can insert tons of data into a table Are you trying to say that you want to check if 22 value exists in the table? Link to comment
Woovie Posted April 19, 2014 Share Posted April 19, 2014 So you want to store 2 values in 1 spot? Your answer is tables. local weapons = {} tabel.insert ( Cart.weapons, { weaponid, weaponid } ) This will appear like this. Cart.weapons = { { weaponid, weaponid }, { weaponid, weaponid }, { weaponid, weaponid } } OR if you meant insert more values, just call table.insert again. Link to comment
Drakath Posted April 19, 2014 Author Share Posted April 19, 2014 I want to insert more values. The table already has a value and I want to insert another value. I called table.insert again but outputChatBox(unpack(Cart.weapons)) only outputs one value. How can I retrieve all values from that table? Link to comment
Saml1er Posted April 19, 2014 Share Posted April 19, 2014 I want to insert more values. The table already has a value and I want to insert another value.I called table.insert again but outputChatBox(unpack(Cart.weapons)) only outputs one value. How can I retrieve all values from that table? A table with Three rows. local weapons = { { 1, 2}, {6666, 21312}, {1231, 423542}, } for _,v in pairs (weapons) do -- by looping through the table local value1 = v[1] local value2 = v[2] outputChatBox ("ROW: value1: ".. value1..", value2: "..value2) end Link to comment
Drakath Posted April 19, 2014 Author Share Posted April 19, 2014 attempt to index local 'v' (a number value) Link to comment
Saml1er Posted April 19, 2014 Share Posted April 19, 2014 attempt to index local 'v' (a number value) Thats weird. I tested it just now and it outputs. ROW: value1: 1, value2: 2 ROW: value1: 6666, value2: 21312 ROW: value1: 1231, value2: 423542 Link to comment
Drakath Posted April 19, 2014 Author Share Posted April 19, 2014 My table isn't local weapons = { { 1, 2}, {6666, 21312}, {1231, 423542}, } It is like this: local Cart = { weapons = { }, prices = { } } local weaponID = getWeaponIDFromName(weapon) table.insert(Cart.weapons, weaponID) --When table is empty this happen table.insert ( Cart.weapons, { Cart.weapons, weaponID } ) --When table is not empty this happen Link to comment
Saml1er Posted April 19, 2014 Share Posted April 19, 2014 My table isn't local weapons = { { 1, 2}, {6666, 21312}, {1231, 423542}, } It is like this: local Cart = { weapons = { }, prices = { } } local weaponID = getWeaponIDFromName(weapon) table.insert(Cart.weapons, weaponID) --When table is empty this happen table.insert ( Cart.weapons, { Cart.weapons, weaponID } ) --When table is not empty this happen Then why don't you do it like this? local Cart = { weapons = { }, prices = { } } local weaponID = 22 Cart.weapons[1] = weaponID ---- This will insert the weapon id into the table and the table will become like this: local Cart = { weapons = {22 }, prices = { } } But if you want to insert more into the same weapons then. Cart.weapons[2] = weaponID -- just keep adding +1 to the Cart.weapons[value] Link to comment
Drakath Posted April 19, 2014 Author Share Posted April 19, 2014 Because a player can buy a lot of weapons. So It would insert a lot of values. I need the script to handle that. And this: local Cart = { weapons = {22 }, prices = { } } --would just replace everything with weapon id 22. Link to comment
Saml1er Posted April 19, 2014 Share Posted April 19, 2014 Because a player can buy a lot of weapons. So It would insert a lot of values. I need the script to handle that.And this: local Cart = { weapons = {22 }, prices = { } } --would just replace everything with weapon id 22. It won't. Use this: The script should be client side. local w_count = 1 local w_count = w_count + 1 -- whenever you insert anything again in weapons then add +1 Cart.weapons[w_count] = weaponID Link to comment
Drakath Posted April 19, 2014 Author Share Posted April 19, 2014 79: attempt to index local 'v' (a number value) 78: for _,v in pairs (Cart.weapons) do 79: local value1 = v[1] Link to comment
Saml1er Posted April 19, 2014 Share Posted April 19, 2014 79: attempt to index local 'v' (a number value)78: for _,v in pairs (Cart.weapons) do 79: local value1 = v[1] Well I've never done a loop like that but anyway for the time being use: for i=1,46 do local a = Cart.weapons[i] if a ~= nil then outputChatBox (a) end end Link to comment
Drakath Posted April 19, 2014 Author Share Posted April 19, 2014 Alright, I did some fixing of your code and I can finally retrieve the ids, but now I also need the amount of bullets. I tried this: Cart.weapons[w_count] = weaponID and bulletsAmount This: Cart.weapons[w_count] = weaponID, bulletsAmount This: Cart.weapons[w_count] = (weaponID, bulletsAmount) But none worked. I think you understood what I want. weaponID and bulletsAmount only outputted bullets. Link to comment
Saml1er Posted April 19, 2014 Share Posted April 19, 2014 Cart.weapons[w_count] = { weaponID, bulletsAmount } -- but now you'll need to use local weaponID = Cart.weapons[1][1] local bulletsAmount = Cart.weapons[1][2] Link to comment
Drakath Posted April 19, 2014 Author Share Posted April 19, 2014 One last thing. I need to make an ability to remove a weapon from the cart. How can I remove a value from table if I know the weapon id and amount of bullets? Link to comment
Saml1er Posted April 19, 2014 Share Posted April 19, 2014 One last thing. I need to make an ability to remove a weapon from the cart. How can I remove a value from table if I know the weapon id and amount of bullets? You'll probably need to loop for that since the data aren't keyed. Once you loop and compare the value and so if they match then set them to nil. Link to comment
Drakath Posted April 20, 2014 Author Share Posted April 20, 2014 local weaponID = getWeaponIDFromName(weapon) for i=1,46 do local a = Cart.weapons[i] if a == weaponID then table.remove(Cart.weapons, a) --How do I remove weaponID value from the table? This didn't work. end end Link to comment
Saml1er Posted April 20, 2014 Share Posted April 20, 2014 local weaponID = getWeaponIDFromName(weapon) for i=1,46 do local a = Cart.weapons[i] if a == weaponID then table.remove(Cart.weapons, a) --How do I remove weaponID value from the table? This didn't work. end end local weaponID = getWeaponIDFromName(weapon) for i=1,46 do local a = Cart.weapons[i] if a == weaponID then Cart.weapons[i] = nil -- This will become nil and when it becomes nil then it will be replaced by the row or value next to it. end end Link to comment
Drakath Posted April 20, 2014 Author Share Posted April 20, 2014 Thank you very much. It worked Link to comment
Saml1er Posted April 20, 2014 Share Posted April 20, 2014 Thank you very much. It worked You're welcome. 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