Is it possible to get only 2 children?

I am making a backpack and inventory system, where you can equip stuff in your inventory but the rest goes into your backpack. I want to fill the inventory when they join, but how do i do that?
I didn’t fin anything related to this topic online.

Part of my script
			local children = h:GetChildren()
			local count = #children
			if count < 6 then
            local remaining= 6 - count
            end

I don’t know how to continue.

2 Likes

There is no way to get only 2 children using GetChildren. But you can check if there is already x items in your inventory.

You can loop through your inventory and check if there is only x items. (x means any number)

local children = h:GetChildren()
local twochilds = {children[1], children[2]}

It’s just that simple :slight_smile:

You can use the unpack function for this
Giving it 6 and the number of elements in the array will returns all elements in the array from 6 to #array
I’m assuming your inventory can hold only 5 items

for i = 1, math.min(#children, 5) do
 local item = children[i]
 -- put item in inventory
end
if #children >= 6 then
 local leftover = table.pack(unpack(children, 6, #children))
end

math.min(5, #children) will return 5 if the number of items exceed 5, otherwise it will use the actual number of items
You can then fill the leftover elements into the backpack