Custom inventory help

So I’ve never made a custom inventory before, because they are just a pain to make.

Would it be better to have item slots already in the gui, or every time the player gets an item remove all item slots, then create new slots for each item?

2 Likes

Yes, I agree they are a pain to make. And I’m pretty sure it would be easier to have item slots already in the GUI.

1 Like

Alright, one other thing. How would I detect if a player gets a tool they don’t already have?

1 Like

Sorry mate, I only have 4 months of scripting experience. I wish I could help, But I currently can’t.

It’s ok. [[[[30 characters]]]]

I’ve done exactly this very recently! It’s not too difficult, but there is an edge case you need to watch out for.

When a player equips a tool, the tool is added to their Character model. That means that you need to check both the player’s Backpack and their Character model for a tool if you want to see if they have it.

All in all, here’s what that would look like.

local player = -- The Player object for the player you're checking.
local character = player.Character or player.CharacterAdded:Wait()
-- In case you're not familiar, the 'or player.CharacterAdded' is used here
--  to prevent issues in a case where the player's character doesn't exist yet.

-- This function will return if the player has the given item.
local function PlayerHasItem(itemName)
  -- First, check the player's backpack for the item.
  if player.Backpack:FindFirstChild(itemName) then
    return true -- The player has the item in their backpack.
  end  
  if character:FindFirstChild(itemName) then
    return true -- The player has the item equipped.
  end  
  return false -- The item wasn't anywhere on the player. Return false.
end

I encourage you to, instead of copy-pasting the above code for use in your project, to write it out by hand yourself and consider how each part works, as it’ll improve your ability to work with Tools and the player’s Backpack in the future.

If you have any further questions, especially about items, feel free to ask me! My current project has item collection as a major focus so I know a lot about that kind of thing if you need any help.

2 Likes

Thank you. I’ll try this when I’m done working on tool selection, and slot highlighting.

2 Likes

Do you remove every slot in the inventory, then create new slots for each item, or do you have slots already in the gui?

The easier solution would be to recreate all UI elements. If you have a relatively small number of items given to the player, I would recommend recreating the elements.

However, if you have a lot of items, recreating the UI will cause lag and possibly a flicker depending on your implemenation. Having slots already present would be better in that case.

I have maximum 10 slots, is that too much?

10 is a fine number. You should be good simply deleting and recreating all the elements.

I’d estimate that, once you reach 20 or so is when recreating the slots starts becoming a problem. So you should be fine with 10.

However, if you intend to add more items, it might be better to go with the harder predefined slots solution just so you don’t have to redo any code.

Thank god, I spent a couple days working on the inventory.

1 Like

If you encounter any additional issues or have any further questions I check the dev forum very often, so feel free to ask them here.

Good luck on your project!

I suggest making slots when the player gets an item. you don’t need to recreate all. just create a slot when needed and leave the others except if there is an update for that particular slot item

Yeah, I’ll try that. But if it’s too hard to make then I’m gonna revert to recreating all slots.

1 Like

It’s actually not :slight_smile:
just tell me the details of your inventory. do you will allow duplicate items? do you will support stackable items?

No stackables, although yes duplicates. Which I just thought of as an issue.

1 Like

so you’ll make a frame which will hold your slots, add to it a UIGridLayout or UIListLayout depending on your inventory. create a slot template in the gui. then parent it to your local script. whenever there is an item added. you’ll clone the slot template and edit it’s text into the item name
Am not sure about your level of experience, so let me know if you don’t understand soomething

Yeah, I already have a UIListLayout. And because of duplicates I’m probably gonna use ObjectValues so then I would be able to differentiate between tools.

1 Like

you can name your slots in an ID manner.
you’ll have a variable called ID like this:
local ID = 0
and whenever you create a slot, you should increase the id
ID = ID + 1
and then set it’s name to the ID
Slot.Name = ID