Seeing that there are not very many good open sourced inventory/backpack systems, I decided to write one.
The current default Roblox backpack has a max of 10 slots.
What if you wanted to change the quantity or aesthetic of these slots?
A custom inventory requires a lot of boilerplate code. If it’s your first time writing an inventory system, you’re bound to run into some annoying bugs as well.
That’s where this module comes in!
Here’s how it works:
These ObjectValues will get auto-assigned tools as their values when the user picks up tools.
Whenever the user presses a key, if there is an ObjectValue with the same name under this folder, that ObjectValue’s tool will be equipped. Note: The order in which these are inserted matters. The first inserted value will be the value that gets a tool assigned first, the second inserted will get assigned second, etc.
Use the module’s function on the folder
This will return an Equipped ObjectValue.
The Equipped ObjectValue will always be set to the currently equipped Slot ObjectValue.
For clarification, doing EquippedVal.Value.Value would return a tool the player would be holding.
Once these elements are set up, you are free to do the graphical side however you’d like!
(There’s a GUI that comes with the module which you can use as a base template)
You can also program things to adjust the slot position of tools.
The following, for example, is a function that would switch the items in two slots:
function switchSlots(slot1,slot2)
local slot1tool = slot1.Value
slot1.Value = slot2.Value
slot2.Value = slot1tool
return true
end
I tried to make this post as simple and detailed as possible, however, don’t worry if this post doesn’t make sense to you at first glance. The asset comes with an example backpack GUI that you can play around with and use however you’d like.
I like your inventory system you made! I don’t know if you can but you can make like a system that if the player have 2 tools of the same type (like 2 swords) in the slot will show one sword and at the top there will be a text saying x2, meaning that you have 2 swords.
This inventory system might be really helpful in a really big number of games, but i was wondering if this allows you to change the UI of the tools slots ? If so, that would be really amazing.
I’ve had that warning before. I don’t remember the exact details, but I do remember that putting in a wait() before trying to change the parent made the warning go away. Something to do with trying to change the parent on the same frame that roblox code is trying to set it I think.
In regards to your Side Notes section involving the reparent warning – This is easily resolvable.
In case you are unaware – This “unexpected parent change” warning is thrown to prevent event spamming to flood the event queue.
One example of code that this warning protects against is:
local p0 = workspace.Object0
local p1 = workspace.Object1
p0.DescendantAdded:Connect(function (d)
d.Parent = p1
end)
p1.DescendantAdded:Connect(function (d)
d.Parent = p0
end)
Parent something to p0 or p1 and you suddenly have something spamming the ever living crap out of Roblox’s backend event system with DescendantAdded event calls.
When this warning displays, Roblox automatically yields until the next event pipeline flush (effectively identical to calling wait()).
If I read your code correctly, you can resolve this issue by going to line 101, adding a newline into that condition, and before calling removeToolFromSlots(tool), add wait(). The code here runs on AncestryChanged (when the parent is changed) and removeToolFromSlots() instantly changes its parent again.
I’ve fixed the unexpected parent change bug and another minor bug.
It was happening because the custom Backpack and the default Roblox Backpack were conflicting.
If anyone finds any bugs please DM me.
You might be confused because Roblox Studio’s explorer arranges the values alphabetically, but it’s the order in which u insert the objects that matters. I’ll admit this isn’t the best way to do ordering, but it’s pretty simple/intuitive. I don’t see it breaking any time soon. It’s mainly for knowing which slot to auto-assign tools to. When a tool is given to the player, it goes to the first available slot in this order.
The names correspond to whatever Enum.Keycode you want for activation (I said this in Setup step 2).
By naming it “Equals”, you bind that slot to Enum.Keycode.Equals
It is shown as slot 12 on the GUI.
For example, if you were to rename it to “Tab”, then the activation key would become the Tab key.
In you next inventory system could you teach us how to implement touch and controller support - I am familiar with enum.Keycode but I have no clue on how to scroll through the inventory using LB and RB