I’m in the middle of creating an inventory system and have come across an issue that I can’t seem to figure out.
Currently, as the player is added, I’m reading from my data store and applying any items inside said data store into the inventory GUI I created. However, it seems to want to create a double of one item that should be stacked, and I’m not sure what is causing it and how I could fix it.
Currently, the code is as follows:
print("time to load inventory")
for _,item in pairs(data.Inventory) do
print("iterating through data")
if item.Amount == 1 then
print("found item with amount 1: ".. item.Name)
itemPanel = folderTemplate.item:Clone()
itemPanel.Parent = playerGui:WaitForChild("ScreenGui"):WaitForChild("inventory"):WaitForChild("itemList")
itemPanel.Name = item.Name
itemPanel.itemname.Text = item.Name
itemPanel.itemamt.Text = item.Amount
itemPanel.itemimg.Image = item.Image
itemPanel.Visible = true
elseif item.Amount >= 2 then
print("found item with amount 2+: ".. item.Name)
for _,v in playerGui:WaitForChild("ScreenGui"):WaitForChild("inventory"):WaitForChild("itemList"):GetChildren() do
if item.Name == v.Name then
v.itemamt.Text = item.Amount
else
itemPanel = folderTemplate.item:Clone()
itemPanel.Parent = playerGui:WaitForChild("ScreenGui"):WaitForChild("inventory"):WaitForChild("itemList")
itemPanel.Name = item.Name
itemPanel.itemname.Text = item.Name
itemPanel.itemamt.Text = item.Amount
itemPanel.itemimg.Image = item.Image
itemPanel.Visible = true
end
end
end
end
I added print
to specific sections for debugging purposes, so the output logs are:
02:23:30.376 time to load inventory - Server - DataService:56
02:23:30.376 iterating through data - Server - DataService:58
02:23:30.376 found item with amount 2+: Cute Face - Server - DataService:69
02:23:30.376 ▶ iterating through data (x2) - Server - DataService:58
02:23:30.376 found item with amount 2+: Scary Face - Server - DataService:69
Everything looks fine, but the loop somehow doubles up when iterating through the data, which I believe is what’s causing the output of two items when there should be a single, stacked item. Again, not sure why or how this is happening, but I’m hoping someone here has an explanation and can help.
EDIT: Okay, the reason for the double iteration at the end is because one of the values in the table contains an amount less than 1, so it’s not considered at all. However, still no clue why the script is continuing to create two guis for items that should be stacked.
EDIT 2: Figured out through more debugging that the script somehow calls for an item twice, somehow, even when specifying in the if,else
statement that items not matching the name of existing inventory spots will have to create a new spot. No clue why that’s happening.
02:37:06.930 ▶ Scary Face is not in this inventory! create! (x2) - Server - DataService:75