So I have an inventory system but there’s a problem. Whenever I press the equip button, it prints out this:
Error Line
slotF.Image = itemData.Items[1].TextureId
"Full Code
--Player
local player = game.Players.LocalPlayer
local backpack = player.Backpack
local char = player.Character or player.CharacterAdded:Wait()
-- Services
local TS = game:GetService("TweenService")
local SG = game:GetService("StarterGui")
local UIS = game:GetService("UserInputService")
local Debris = game:GetService("Debris")
local Players = game:GetService("Players")
-- Disabling Backpack GUI
SG:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
-- Gui
local gui = script.Parent
local items = {}
local inventoryF = gui.Inventory; inventoryF.Visible = false
local hotbarF = gui.HotBar
local itemDescriptionF = inventoryF.ItemDescription
local itemNameT = itemDescriptionF:WaitForChild("Name")
local itemDescT = itemDescriptionF.Description
local itemImage = itemDescriptionF.ItemImage
local itemDmg = itemDescriptionF.Damage
local itemCooldwn = itemDescriptionF.Cooldown
local itemAbility = itemDescriptionF.Ability
-- Slots
local s1 = hotbarF.SlotF1.Slot1
local s2 = hotbarF.SlotF2.Slot2
local s3 = hotbarF.SlotF3.Slot3
local s4 = hotbarF.SlotF4.Slot4
local s5 = hotbarF.SlotF5.Slot5
local slots = {s1,s2,s3,s4,s5}
local hotbarItems = {}
local itemsSF = inventoryF.Items
local equipB = inventoryF.ItemDescription.EquipButton
local invSampleItem = script.Sample
local openB = script.Parent.Parent.ButtonsGui.Buttons.OpenButtonINV
local closeB = script.Parent.Inventory.CloseButtonF.CloseButton
openB.MouseButton1Click:Connect(function()
inventoryF.Visible = not inventoryF.Visible
end)
closeB.MouseButton1Click:Connect(function()
inventoryF.Visible = false
end)
-- Reference Variables
local selectedItem = nil
-- Scanning for Items
local function scanitems()
-- Item List
local items = {}
-- Scanning Backpack
for i,tool in pairs(backpack:GetChildren()) do
if tool.ClassName == "Tool" then
table.insert(items,tool)
end
end
-- Scanning Character
for i, tool in pairs(char:GetChildren()) do
if tool.ClassName == "Tool" then
table.insert(items, tool)
end
end
-- Creating New List
local invItems = {}
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
-- Updating Currently Selected Items
local function updateSelected(toSelectIB, toolData)
-- Selecting New
selectedItem = toolData.Items
-- Changing Display
itemNameT.Text = toolData.Items[1].Name
itemDescT.Text = toolData.Items[1].ToolTip
itemImage.Image = toolData.Items[1].TextureId
itemDmg.Text = toolData.Items[1]:GetAttribute("Damage")
itemAbility.Text = toolData.Items[1]:GetAttribute("Ability")
itemCooldwn.Text = toolData.Items[1]:GetAttribute("Cooldown")
end
-- Removing Current Inventory Items
local function removeItems()
for i, itemButton in pairs(itemsSF:GetChildren()) do
if itemButton.ClassName == "ImageButton" and itemButton.Name ~= "Sample" then
itemButton:Destroy()
end
end
end
-- Updating Display
local function updateDisplay()
-- Clearing Out Current Inventory
removeItems()
-- Getting Inventory Tools
local invItems = scanitems()
for toolName, toolData in pairs(invItems) do
-- Cloning Sample
local itemButton = invSampleItem:Clone()
itemButton.Parent = itemsSF
itemButton.Name = toolData.Items[1].Name
-- Setting Display
itemButton.ItemName.Text = toolName
itemButton.Image = toolData.Items[1].TextureId
itemButton.Visible = true
-- Connecting Buttons
itemButton.MouseButton1Click:Connect(function()
updateSelected(itemButton, toolData)
end)
end
-- Hotbar Slots
for i, slotF in pairs(slots) do
slotF.Image = ""
end
for slotF, itemData in pairs(hotbarItems) do
slotF.Image = itemData.Items[1].TextureId
end
end
-- Loop
task.spawn(function()
while true do
task.wait(1)
updateDisplay()
end
end)
-- Equip Button
local equipButtonDB = false
local function equip()
-- Choosing slot
local chosenSlot = nil
for i, slotF in pairs(slots) do
if hotbarItems[slotF] == nil then
chosenSlot = slotF
end
end
if chosenSlot then
-- Equipping to slot
for slotF, itemData in pairs(hotbarItems) do
if itemData.Items[1].Name == selectedItem.Items[1].Name then
hotbarItems[slotF] = nil
end
end
hotbarItems[chosenSlot] = selectedItem
updateDisplay()
end
end
-- Connecting Equip
equipB.MouseButton1Click:Connect(function()
equip()
end)
The GUI hierarchy:
If you can help, it would be greatly appreciated! Thanks!