generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Inventory System Help

Previous Thread :: Next Thread 
TheBoyOnTheBlock is not online. TheBoyOnTheBlock
Joined: 09 Nov 2013
Total Posts: 341
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
MidnightTraceur is not online. MidnightTraceur
Joined: 23 Oct 2013
Total Posts: 434
28 Jun 2014 09:06 AM
HELP HIM! So he can halp me ;o

~ Lua, not LUA ~
Report Abuse
PerpetualMovement is not online. PerpetualMovement
Joined: 04 Dec 2012
Total Posts: 1001
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
TheBoyOnTheBlock is not online. TheBoyOnTheBlock
Joined: 09 Nov 2013
Total Posts: 341
28 Jun 2014 09:09 AM
Oh. What if I run a coroutine that runs a while loop that updates it automatically.
Report Abuse
TheBoyOnTheBlock is not online. TheBoyOnTheBlock
Joined: 09 Nov 2013
Total Posts: 341
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
PerpetualMovement is not online. PerpetualMovement
Joined: 04 Dec 2012
Total Posts: 1001
28 Jun 2014 09:11 AM
Thet would be less efficientl. Just put it right after the additem and removeitem
Report Abuse
PerpetualMovement is not online. PerpetualMovement
Joined: 04 Dec 2012
Total Posts: 1001
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
TheBoyOnTheBlock is not online. TheBoyOnTheBlock
Joined: 09 Nov 2013
Total Posts: 341
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
PerpetualMovement is not online. PerpetualMovement
Joined: 04 Dec 2012
Total Posts: 1001
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
TheBoyOnTheBlock is not online. TheBoyOnTheBlock
Joined: 09 Nov 2013
Total Posts: 341
28 Jun 2014 09:24 AM
I thought tables updated dynamically in Lua?
Report Abuse
MidnightTraceur is not online. MidnightTraceur
Joined: 23 Oct 2013
Total Posts: 434
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 is not online. 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 is not online. Bebee2
Joined: 17 May 2009
Total Posts: 3985
28 Jun 2014 09:30 AM
Also note how you run "additems" once.
Report Abuse
Bebee2 is not online. Bebee2
Joined: 17 May 2009
Total Posts: 3985
28 Jun 2014 09:31 AM
currentitems never updates.
Report Abuse
Bebee2 is not online. 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
TheBoyOnTheBlock is not online. TheBoyOnTheBlock
Joined: 09 Nov 2013
Total Posts: 341
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
TheBoyOnTheBlock is not online. TheBoyOnTheBlock
Joined: 09 Nov 2013
Total Posts: 341
28 Jun 2014 09:58 AM
Thank you man. I owe you one.
Report Abuse
TheBoyOnTheBlock is not online. TheBoyOnTheBlock
Joined: 09 Nov 2013
Total Posts: 341
28 Jun 2014 10:11 AM
Is it possible for me to update the table using global functions without resetting the table?
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image