Inventory system a bit buggy creating duplicate buttons

Hello so I have this inventory system that I made sorry if it sucks lol, but there’s a bug I encountered and can’t seem to find a workaround

So first of all whenever the player opens the inventory is when I request to get the data and obviously I think this is bad because every time the player opens the inventory I invoke server and so it can be spammed oof

local toggleInventory = screenGui.toggleInventory
toggleInventory.MouseButton1Click:Connect(function()
	data = getData:InvokeServer() --server returns all the players data 
	profileFrame.Visible = not profileFrame.Visible
end)

This part basically waits until data ~= nil so the data is there and then it just makes a button for each item inside the data inventory table however an issue I had was If I needed to add new items so I simply just made an addItem event that I use when i need to make a new button (for my shop for example after a play buys an item) however I found out if the player buys an item from the shop and then opens the inventory well the inventory is gonna make duplicates of the items because it’s still on the repeat

repeat wait() until data ~= nil

--Inventory (creates a button for each item inside the inventory table)
for _,item in pairs(data.inventory) do
	createButton(item)
end

--Equipped Items
for slot,item in pairs(data.equipped) do --this is the equipped data so the players currently equipped items if any
	--print(item)
	--if item ~= "" then --there is an item in the slot (I only store the name of the item)
	--print(item,slot)
	
	--This part will update the equipment slots to show if stuff is equipped (because we save the staff in the data so ye
	for _,slotUI in pairs(equipmentSlots:GetChildren()) do --this is the equipmentSlot buttons so the ui
		if string.lower(slotUI.Name) == slot and item ~= "" then --there IS something equipped in this slot so change the SlotUI text to it
			slotUI.Text = item
		end

		slotUI.MouseEnter:Connect(function()
			if slotUI.Name ~= slotUI.Text then
				if deb == false then
					deb = true
					displayStats(slotUI.Text)	
					deb = false
				end
			end
		end)

		slotUI.MouseLeave:Connect(function()
			itemInfoFrame.Visible = false
		end)
		
		--Unequip
		slotUI.MouseButton1Click:Connect(function()
			if deb == false then
				deb = true
				local scuessfullyUnequip = unequip:InvokeServer(slotUI.Text, slotUI.Name) --do some checks on the server to make sure it's all legit
				if scuessfullyUnequip then
					createButton(slotUI.Text)
					slotUI.Text = slotUI.Name	
				else
					warn("Couldn't unequip item")
				end
				deb = false
			end
		end)
	end			
	--end
end

addItem.OnClientEvent:Connect(function(item)
	createButton(item)
end)

So if anyone is able to help fix this issue that would be great thanks :slight_smile:

You should only get the Data once the player joins/ its updated but just make the frame visible / not visible and it will be fine

1 Like
function checkDuplicate(ItemName)
if frame:FindFirstChild(ItemName) then return true end
end

if checkDuplicate('itemname') then
return
else
addbutton()
end

Basically this works for either problem you’re having even if it isn’t what you mean. You simply just figure out a way to check if there’s the same button, if there is you simply return instead of adding.

Yeah I’ve been struggling to do this anytime I try to send the data when they join I always get an error with how data for the player doesn’t exist which I assume is because the data didn’t load in yet.

Edit: nvm so I used my eyes a bit and realized I was doing it in the wrong part of my code anyways I changed some stuff around and now everything seems to work this issue I had doesn’t seem to occur