I'm working on my Inventory system for my Roblox survival game and I can't figure out a way to fix an issue I've been having that you can see in this GIF I've made: https://imgur.com/BwLYrmG
I believe I've found what's been causing this bug but I can't find a solution to it. I believe the cause is that when the script runs the function updateDisplay() every second, it checks through all the hotbar and inventory slots and if one of them is empty, it puts the item there. However, if there is only one item in the player's Backpack, the script says the same tool is in slot1 and also puts it in slot2. I'm wondering how I can implement the check for this.
the script is down below:
local StarterGui = game:GetService("StarterGui")
local Players = game:GetService("Players")
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
local player = Players.LocalPlayer
local backpack = player.Backpack
local char = player.Character
local GUI = script.Parent.Parent.Inventory
local hotbar = GUI.Hotbar
local inventory = GUI.Inventory
local items = inventory.Items
local slot1 = inventory.Items.InventorySlot1
local slot2 = inventory.Items.InventorySlot2
local slot3 = hotbar.Frame.HotbarSlot1
local slot4 = hotbar.Frame.HotbarSlot2
local invSlotData = {
[slot1] = nil,
[slot2] = nil,
[slot3] = nil,
[slot4] = nil
}
local function scanItems()
local items = {}
--checks backpack and character for items and adds them to the Items table
for i, tool in pairs(backpack:GetChildren()) do
if tool.ClassName == "Tool" then
table.insert(items, tool)
end
end
for i, tool in pairs(char:GetChildren()) do
if tool.ClassName == "Tool" then
table.insert(items, tool)
end
end
local invItems = {}
--[[looks through Items table, and if a tool has the same name as an index in invItems table, add to the index's count and insert
the tool's name into the index's Items table. else, create a new index with the tool's name and set all it's tables to default --]]
for i, tool in pairs(items) do
if invItems[tool.Name] then
invItems[tool.Name].Count += 1
table.insert(invItems[tool.Name].Items, tool)
else
invItems[tool.Name] = {
Count = 1,
Items = {tool}
}
end
end
return invItems
end
local function resetItems()
for i, button in pairs(items:GetChildren()) do
if button.ClassName == "ImageButton" then
button.Image = ""
button.CountDisplay.Text = ""
button.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
end
end
for i, button in pairs(hotbar.Frame:GetChildren()) do
if button.ClassName == "ImageButton" then
button.Image = ""
button.CountDisplay.Text = ""
button.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
end
end
end
local function updateDisplay()
resetItems()
local invItems = scanItems()
for toolName, toolData in pairs(invItems) do
if invSlotData[slot1] == nil then
invSlotData[slot1] = toolData
slot1.CountDisplay.Text = toolData.Count
slot1.Image = toolData.Items[1].TextureId
elseif invSlotData[slot2] == nil then
invSlotData[slot2] = toolData
slot2.CountDisplay.Text = toolData.Count
slot2.Image = toolData.Items[1].TextureId
elseif invSlotData[slot3] == nil then
invSlotData[slot3] = toolData
slot3.CountDisplay.Text = toolData.Count
slot3.Image = toolData.Items[1].TextureId
elseif invSlotData[slot4] == nil then
invSlotData[slot4] = toolData
slot4.CountDisplay.Text = toolData.Count
slot4.Image = toolData.Items[1].TextureId
end
end
print(invSlotData)
end
task.spawn(function()
while true do
task.wait(1)
updateDisplay()
end
end)

2 comments
I don't know if this is possible or and this maybe a possible fix can't you updateDisplay when a item is added to the inventory instead of every second??
thanks for the idea! i'll try implementing it when i have time. if it does work, i'll contact you and you can post this as an answer so i can mark it as correct.