Help with inventory system

hi, im a bit lost here, im making an inventory system and im currently trying to make it so you can stack items

for _, slot in inventoryGui.MainFrame.Slots:GetChildren() do
	if slot:FindFirstChild("Item").Value.Name == item.Name then
		print(slot)
		break
	elseif slot:FindFirstChild("Item").Value == nil then
		print(slot)
		break
	end
end

and so basically i pick up and item and its supposed to check the slots if the item in them has the same name (then we stack) or if it doesnt have anything in it yet (then we place in new slot)

however, the issue is if the stacked item is in the 3rd slot and the first slot is empty, then its gonna put it in the first slot, and not the 3rd where the item is. this is because the loop starts with the first slot and since that slot is empty, the first if statement isnt gonna be true, and then its gonna move on the elseif part, and that one is true.

and so what i need help with is how do i make all of the slots check with the first if statement before moving on to the elseif part, rather than checking both the if and the elseif statements for one slot and then doing the other slots afterwards

1 Like

You can use flags or iterate:

local foundSlot = nil

for _, slot in inventoryGui.MainFrame.Slots:GetChildren() do
    local itemValue = slot:FindFirstChild("Item").Value

    if itemValue and itemValue.Name == item.Name then
        foundSlot = slot -- Store the slot where the item is found
        break
    end
end

if foundSlot then
    print("Stacking item in slot:", foundSlot)
    -- Logic to stack the item in foundSlot
else
    -- If no matching item was found, look for an empty slot
    for _, slot in inventoryGui.MainFrame.Slots:GetChildren() do
        if slot:FindFirstChild("Item").Value == nil then
            print("Placing item in empty slot:", slot)
            -- Logic to place the item in this empty slot
            break
        end
    end
end

so basically you would want to first find if the slot item exist by looping in the slots, When the certain slot is found then return it, this will be in our condition to basically let us know that we already have this item…

local function ReturnItemFromSlot(itemName)
	for _, slot in inventoryGui.MainFrame.Slots:GetChildren() do
		if slot:FindFirstChild("Item").Value.Name ~= itemName then continue end -- this will continue the code with out error
		if slot:FindFirstChild("Item").Value.Name == itemName then
			print(slot)
			return slot:FindFirstChild("Item")
		end
	end
	return nil
end

Then we want to go to our main function where we will be setting up our conditions like so:

-- im guessing ur using proximty prompt sooo yeah
if ReturnItemFromSlot(item.Name) then
	print("item found! .. u can now stack!")
	local Item_ = ReturnItemFromSlot(item.Name) -- refers to this | slot:FindFirstChild("Item")
	Item_.Value += 1
elseif not ReturnItemFromSlot(item.Name) == nil then
	warn("item was not found!")
end

this is basically how u find if you already have said item, then u can stack based on how ur code is set up!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.