Help needed with inventory system auto adding to inventory

  1. What do you want to achieve?
    I am currently making an inventory system and i have a function to add items to the hotbar, but when the hotbar is full i want to auto add the remaining items to the inventory.

  2. What is the issue?
    So this is my script. The wait is there cause it kept infinitely looping.

function inventory.AddItem(player, loc, name, quantity)
	if player == nil or name == nil then return end
	quantity = setting.NumberCheck(quantity) or 1
	
	local firstFrame
	local currentQuantity
	local newQuantity
	
	local function createButton(q)
		firstFrame.Name = name
		firstFrame.ImageBackground.BackgroundColor3 = setting.OccupiedColor
		firstFrame.ImageBackground.IconImage.Image =  "rbxassetid://"..setting.ImageIDS[firstFrame.Name]
		firstFrame.Quantity.Text = q
		firstFrame.Quantity.Visible = true
	end
	wait(.2)
	
	for _, button in pairs(loc:GetChildren()) do
		if button:IsA("GuiButton") then
			print(button.Name, name)
			if button.Name ~= "" and button.Name == name then
				if tonumber(button.Quantity.Text) == setting.MaxStackAmount then
					continue
				end
				
				firstFrame = button
			end
			
			firstFrame = button
			
			currentQuantity = tonumber(firstFrame.Quantity.Text) or 0
			newQuantity = quantity + currentQuantity
			print(quantity, currentQuantity, newQuantity)
			
			if setting.EnableMaxStack and newQuantity > setting.MaxStackAmount then
				createButton(setting.MaxStackAmount)
				inventory.AddItem(player, loc, name, newQuantity - setting.MaxStackAmount)
				return
			end
			
			return firstFrame
		end
	end
	
	if firstFrame == nil then
		print(loc, "is full")
		return
	end
	
	if newQuantity < 2 then
		firstFrame.Quantity.Visible = false
	end
	
	createButton(newQuantity)
	
end

And as you can see i have a little check to see if there are no more items in the hotbar. And i tried this first

if firstFrame == nil then
		print(loc, "is full")
		local button = inventory.AddItem(player, loc.Parent.Inventory, name, quantity)
		
		if button then
			print(button)
		end
		return
	end

But this didnt work and i dont know how i would make it work so if anyone has any ideas they’ll be appreciated :slight_smile:


1 Like