|
| 28 Jun 2014 09:04 AM |
inventory = {} maxItems = 10 -- change this to how many items you can store currentItems = #inventory
function addItem() if (currentItems < maxItems) then local item = table.insert(inventory, "item" .. math.random(1, 100)) print('Item added.') else print('Inventory already full.') end end
function removeItem() if (currentItems < 1) then print('Nothing to remove') else for i = 1, #maxItems do table.remove(inventory, maxItems[i]) print('Item removed.') end end end
addItem() print(table.concat(inventory, ' '))
It seems to be not adding any new values to the table. I tested it and the inventory never makes note of being full. It seems to replace the old value. It only prints one item instead of multiple items. |
|
|
| Report Abuse |
|
|
|
| 28 Jun 2014 09:06 AM |
HELP HIM! So he can halp me ;o
~ Lua, not LUA ~ |
|
|
| Report Abuse |
|
|
|
| 28 Jun 2014 09:07 AM |
Put this right after the additem function.
currentItems = #inventory
It will keep adding because current items will stay 0 and you need to update it. |
|
|
| Report Abuse |
|
|
|
| 28 Jun 2014 09:09 AM |
| Oh. What if I run a coroutine that runs a while loop that updates it automatically. |
|
|
| Report Abuse |
|
|
|
| 28 Jun 2014 09:10 AM |
| Could you add it into my script for me. I'm not sure where to put it. |
|
|
| Report Abuse |
|
|
|
| 28 Jun 2014 09:11 AM |
| Thet would be less efficientl. Just put it right after the additem and removeitem |
|
|
| Report Abuse |
|
|
|
| 28 Jun 2014 09:13 AM |
function addItem() currentItems = #inventory if (currentItems < maxItems) then local item = table.insert(inventory, "item" .. math.random(1, 100)) print('Item added.') else print('Inventory already full.') end end
Do the same for removeitems |
|
|
| Report Abuse |
|
|
|
| 28 Jun 2014 09:17 AM |
Still the same result.
inventory = {} maxItems = 10 -- change this to how many items you can store currentItems = #inventory
function addItem() currentItems = #inventory if (currentItems < maxItems) then local item = table.insert(inventory, "item" ..(currentItems + 1)) --If I didn't do this the items would be called item 0-9, and we don't want that. print('Item added.') else print('Inventory already full.') end end
function removeItem() currentItems = #inventory if (currentItems < 1) then print('Nothing to remove') else for i = 1, #maxItems do table.remove(inventory, maxItems[i]) print('Item removed.') end end end
addItem() print(table.concat(inventory, ' '))
|
|
|
| Report Abuse |
|
|
|
| 28 Jun 2014 09:22 AM |
local item = table.insert(inventory,currentItems+1, "item" ..(currentItems + 1))
You didn't put the position of the item. |
|
|
| Report Abuse |
|
|
|
| 28 Jun 2014 09:24 AM |
| I thought tables updated dynamically in Lua? |
|
|
| Report Abuse |
|
|
|
| 28 Jun 2014 09:25 AM |
| Can there be a script to hide the default inventory on the bottom? I'm wanting to replace it with a GUI inventory, but I can't seem to make a script to hide the default inventory. |
|
|
| Report Abuse |
|
|
Bebee2
|
  |
| Joined: 17 May 2009 |
| Total Posts: 3985 |
|
|
| 28 Jun 2014 09:29 AM |
| table.insert is a C function I believe that automatically adds it into the table without triggering metamethods. |
|
|
| Report Abuse |
|
|
Bebee2
|
  |
| Joined: 17 May 2009 |
| Total Posts: 3985 |
|
|
| 28 Jun 2014 09:30 AM |
| Also note how you run "additems" once. |
|
|
| Report Abuse |
|
|
Bebee2
|
  |
| Joined: 17 May 2009 |
| Total Posts: 3985 |
|
|
| 28 Jun 2014 09:31 AM |
| currentitems never updates. |
|
|
| Report Abuse |
|
|
Bebee2
|
  |
| Joined: 17 May 2009 |
| Total Posts: 3985 |
|
|
| 28 Jun 2014 09:34 AM |
inventory = {} maxItems = 10 -- change this to how many items you can store currentItems = #inventory
function addItem() if (currentItems < maxItems) then local item = table.insert(inventory, "item" .. math.random(1, 100)) print('Item added.') currentItems = #inventory else print('Inventory already full.') end end
function removeItem() if (currentItems < 1) then print('Nothing to remove') else for i = 1, #maxItems do table.remove(inventory, maxItems[i]) print('Item removed.') end currentItems = #inventory end end
addItem() print(table.concat(inventory, ' ')) |
|
|
| Report Abuse |
|
|
|
| 28 Jun 2014 09:55 AM |
| oh, so that must be the problem. I disable and enable the script, so it must reset the table. |
|
|
| Report Abuse |
|
|
|
| 28 Jun 2014 09:58 AM |
| Thank you man. I owe you one. |
|
|
| Report Abuse |
|
|
|
| 28 Jun 2014 10:11 AM |
| Is it possible for me to update the table using global functions without resetting the table? |
|
|
| Report Abuse |
|
|