Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would I solve this inventory system dilemma?

So I've been wanting to make an action-adventure game for a couple of years now, and now that I feel confident enough in my skills as a programmer, I've started the process of making it. I decided that the only way to truly know if I were actually capable would be to try, so I decided to start making the inventory system, as I want it to be a core feature.

I started by putting this into a Script.

Inventory = {

	KeyItems = { --stuff the player uses that is important; keys and stuff; can't be deleted
		
	},
	Consumables = { --stuff the player uses and causes it to be gone or semi-depleted; potions/food/etc.
		
	},
	Equipment = { --stuff your player can equip; swords and stuff
		
	},
	Misc = { --stuff that doesn't go in above categories; possibly sell-items like treasure or jewelery
		
	},
	Gold = { --literally just money
		
	}
}

I want to run a sanity check to see if this really works, and I was just about to, when something crossed my mind--how in the world am I going to add items to this thing?

This is the standstill. I'm not sure how I'm going to be able to add items to my inventory. I was thinking of using a ModuleScript with the function AddItem. Here's something I had in mind:

local module = {

AddItem = function(item,amount)
	print("Added " .. amount .. " " .. item .. " to player inventory")
end

}

return module

The "amount" parameter would be an integer and the "item" parameter would be a string. I would use the value of "item" to determine the type and location of the thing I would put into the inventory. How would I pull this off?

More importantly, is there a better method than what I thought of?

This game is intended to be a single-player game, so you don't need to worry very much about client-server relations, as there will be only one Player that needs to see anything.

1 comment

0

What DataStores? If things happen on the client side, saving data would be an issue

1 answer

Try changing Gold to a regular value, then try this

local InventoryFolder = Instance.new(“Folder”)
InventoryFolder.Name = “Inventory”
Inventory.Parent = -- Make a player variable over here

for name,value in pairs(Inventory) do -- What creates the folders
      if type(value) == “table” then
            local Folder = Instance.new(“Folder”)
            Folder.Name = name
            Folder.Parent = Inventory
      end
end

Then add inventory slots with a pair loop again like this :

local MaxSlots = 10
for _,Folder in pairs(InventoryFolder:GetChildren())
      for i = 1,MaxSlots,1 do
            local Slot = Instance.new(“Folder”) -- Create an Inventory Slot using folder so I can store more info inside of it
             Slot.Name = “Slot”..i
             Slot.Parent = Folder
      end
end

1 comment

0

You can add whatever info you want inside of the inventory slots

This discussion is preserved as it was. New answers, comments, and votes are closed.