Hello developers, I was just casually working on my game, and then I was working on an inventory gui. I was scripting it when I got an error I couldn’t fix.
So here’s the script (Line with error at the end of the post)
local tweenService = game:GetService("TweenService")
local starterGui = game:GetService("StarterGui")
local userInputService = game:GetService("UserInputService")
local debris = game:GetService("Debris")
local players = game:GetService("Players")
local player = players.LocalPlayer
local backpack = player:WaitForChild("Backpack")
local character = player.Character or player.CharacterAdded:Wait()
starterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false)
local screenGui = script.Parent
local hotbarFrame = screenGui:WaitForChild("Hotbar"); hotbarFrame.Visible = true
local inventoryFrame = screenGui:WaitForChild("Inventory"); inventoryFrame.Visible = false
local itemDescriptionFrame = inventoryFrame:WaitForChild("ItemDescription")
local itemNameText = itemDescriptionFrame:WaitForChild("Name")
local itemDescriptionText = itemDescriptionFrame:WaitForChild("Description")
local equipNotification = inventoryFrame:WaitForChild("EquipNotification")
local s1 = hotbarFrame:WaitForChild("Slot1")
local s2 = hotbarFrame:WaitForChild("Slot2")
local s3 = hotbarFrame:WaitForChild("Slot3")
local s4 = hotbarFrame:WaitForChild("Slot4")
local s5 = hotbarFrame:WaitForChild("Slot5")
local s6 = hotbarFrame:WaitForChild("Slot6")
local s7 = hotbarFrame:WaitForChild("Slot7")
local s8 = hotbarFrame:WaitForChild("Slot8")
local s9 = hotbarFrame:WaitForChild("Slot9")
local s10 = hotbarFrame:WaitForChild("Slot10")
local slots = {s1,s2,s3,s4,s5,s6,s7,s8,s9,s10}
local hotbarItems = {}
local itemsScrollFrame = inventoryFrame:WaitForChild("Items")
local inventorySampleItem = itemsScrollFrame:WaitForChild("Sample")
local closeButton = inventoryFrame:WaitForChild("CloseButton")
local equipButton = inventoryFrame:WaitForChild("EquipButton")
local selectedItem = nil
local openButton = screenGui:WaitForChild("OpenButton")
local openPosition = UDim2.new(0.5,0,0.5,0)
local closePosition = UDim2.new(0.5,0,-0.5,0)
inventoryFrame.Position = closePosition
local function openInventory()
if inventoryFrame.Position ~= closePosition then
return
end
local info = TweenInfo.new(
1,
Enum.EasingStyle.Quad,
Enum.EasingDirection.Out,
0,false,0
)
local props = {
Position = openPosition;
}
local tween = tweenService:Create(inventoryFrame,info,props)
inventoryFrame.Visible = true
tween:Play()
end
local function closeInventory()
if inventoryFrame.Position ~= openPosition then
return
end
local info = TweenInfo.new(
1,
Enum.EasingStyle.Sine,
Enum.EasingDirection.In,
0,false,0
)
local props = {
Position = closePosition;
}
local tween = tweenService:Create(inventoryFrame,info,props)
tween:Play()
tween.Completed:Wait()
inventoryFrame.Visible = false
end
openButton.MouseButton1Up:Connect(function()
if inventoryFrame.Position == openPosition then
closeInventory()
else
openInventory()
end
end)
closeButton.MouseButton1Up:Connect(closeInventory)
local function updateSelected(toSelectImageButton,toolData)
selectedItem = toolData.Items[1].Name
itemNameText.Text = toSelectImageButton.Name
itemNameText.Text = toolData.Items[1].Name
itemDescriptionText.Text = toolData.Items[1].ToolTip
end
local function removeItems()
for i,itemButton in pairs(itemsScrollFrame:GetChildren()) do
if itemButton.ClassName == "ImageButton" and itemButton.Name ~= "Sample" then
itemButton:Destroy()
end
end
end
local function scanItems()
local items = {}
for i,tool in pairs(backpack:GetChildren()) do
if tool.ClassName == "Tool" then
table.insert(items,tool)
end
end
for i,tool in pairs(character:GetChildren()) do
if tool.ClassName == "Tool" then
table.insert(items,tool)
end
end
local inventoryItems = {}
for i,tool in pairs(items) do
if inventoryItems[tool.Name] then
inventoryItems[tool.Name].Count += 1
table.insert(inventoryItems[tool.Name].Items,tool)
else
inventoryItems[tool.Name] = {
Count = 1;
Items = {tool};
}
end
end
return inventoryItems
end
local function updateDisplay()
removeItems()
local inventoryItems = scanItems()
for toolName,toolData in pairs(inventoryItems) do
local itemButton = inventorySampleItem:Clone()
itemButton.Parent = itemsScrollFrame
itemButton.Name = toolData.Items[1].Name
itemButton:FindFirstChild("CountDisplay").Text = toolData.Count.."x"
itemButton.Image = toolData.Items[1].TextureId
itemButton.Visible = true
itemButton.MouseButton1Up:Connect(function()
updateSelected(itemButton,toolData)
end)
end
for i,slot in pairs(slots) do
slot.Image = ""
end
for slotFrame,itemData in pairs(hotbarItems) do
--
--
--
--
--
slotFrame.Image = itemData.Items[1].TextureId -- line with error
--
--
--
--
--
end
end
local equipButtonDebounce = false
local function equip()
if selectedItem == nil or equipButtonDebounce == true then return end
equipButtonDebounce = true
equipNotification.Visible = true
local input,gameProcessedEvent = userInputService.InputBegan:Wait()
local keyToSlots = {
[Enum.KeyCode.One] = s1;
[Enum.KeyCode.Two] = s2;
[Enum.KeyCode.Three] = s3;
[Enum.KeyCode.Four]= s4;
[Enum.KeyCode.Five] = s5;
[Enum.KeyCode.Six] = s6;
[Enum.KeyCode.Seven] = s7;
[Enum.KeyCode.Eight] = s8;
[Enum.KeyCode.Nine] = s9;
[Enum.KeyCode.Zero] = s10
}
local chosenSlot = nil
for keyCode,slot in pairs(keyToSlots) do
if input.KeyCode == keyCode then
chosenSlot = slot
break
end
end
if chosenSlot == nil then
local oldText = equipNotification.Text
local oldColor3 = equipNotification.BackgroundColor3
equipNotification.Text = "Invalid Key"
equipNotification.BackgroundColor3 = Color3.new(0,0,0)
equipNotification.Visible = true
task.wait(1)
equipNotification.Visible = false
equipNotification.Text = oldText
equipNotification.BackgroundColor3 = oldColor3
else
hotbarItems[chosenSlot] = selectedItem
print(hotbarItems)
end
print(hotbarItems)
equipButtonDebounce = false
end
equipButton.MouseButton1Up:Connect(equip)
task.spawn(function()
while true do
task.wait(0.1)
updateDisplay()
end
end)
Line with error:
slotFrame.Image = itemData.Items[1].TextureId
Thank you so much for helping me with this error.