-- LocalScript in StarterPlayer > StarterPlayerScripts.
-- Documentation-reviewed; not executed in Roblox Studio for this guide.
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local view = player:WaitForChild("InventoryPracticeView")
view:WaitForChild("Lantern")
view:WaitForChild("Compass")
local request = ReplicatedStorage:WaitForChild("InventoryPractice"):WaitForChild("EquipRequest")

if playerGui:FindFirstChild("InventoryPracticeGui") then return end
local gui = Instance.new("ScreenGui")
gui.Name = "InventoryPracticeGui"
gui.ResetOnSpawn = false
local panel = Instance.new("Frame")
panel.Name = "Panel"
panel.Size = UDim2.new(0.85, 0, 0, 226)
panel.Position = UDim2.new(0.5, 0, 0.5, 0)
panel.AnchorPoint = Vector2.new(0.5, 0.5)
panel.BackgroundColor3 = Color3.fromRGB(19, 31, 45)
panel.Visible = false
panel.Parent = gui
local limit = Instance.new("UISizeConstraint")
limit.MaxSize = Vector2.new(380, 226)
limit.Parent = panel
local corner = Instance.new("UICorner")
corner.CornerRadius = UDim.new(0, 16)
corner.Parent = panel
local title = Instance.new("TextLabel")
title.Size = UDim2.new(1, -32, 0, 40)
title.Position = UDim2.fromOffset(16, 10)
title.BackgroundTransparency = 1
title.Text = "Your items"
title.TextColor3 = Color3.fromRGB(225, 245, 238)
title.TextSize = 20
title.Font = Enum.Font.GothamBold
title.Parent = panel
local status = Instance.new("TextLabel")
status.Size = UDim2.new(1, -32, 0, 28)
status.Position = UDim2.fromOffset(16, 182)
status.BackgroundTransparency = 1
status.TextColor3 = Color3.fromRGB(173, 190, 206)
status.TextSize = 14
status.Font = Enum.Font.Gotham
status.Parent = panel

local toggle = Instance.new("TextButton")
toggle.Name = "InventoryToggle"
toggle.Size = UDim2.fromOffset(160, 44)
toggle.Position = UDim2.new(0.5, 0, 1, -24)
toggle.AnchorPoint = Vector2.new(0.5, 1)
toggle.Text = "Inventory"
toggle.TextSize = 18
toggle.Font = Enum.Font.GothamBold
toggle.BackgroundColor3 = Color3.fromRGB(225, 245, 238)
toggle.TextColor3 = Color3.fromRGB(19, 31, 45)
toggle.Parent = gui
toggle.Activated:Connect(function()
    panel.Visible = not panel.Visible
    toggle.Text = panel.Visible and "Close inventory" or "Inventory"
end)

local buttons = {}
local function render()
    local equipped = player:GetAttribute("InventoryPracticeEquipped") or ""
    status.Text = equipped == "" and "Nothing equipped" or ("Equipped: " .. equipped)
    for itemId, button in pairs(buttons) do
        local entry = view:FindFirstChild(itemId)
        local available = entry and entry:IsA("BoolValue") and entry.Value
        button.Visible = available == true
        button.Text = (equipped == itemId and "Unequip " or "Equip ") .. itemId
    end
end

for index, itemId in ipairs({ "Lantern", "Compass" }) do
    local id = itemId
    local button = Instance.new("TextButton")
    button.Name = id
    button.Size = UDim2.new(1, -32, 0, 48)
    button.Position = UDim2.fromOffset(16, 58 + (index - 1) * 60)
    button.BackgroundColor3 = Color3.fromRGB(105, 225, 181)
    button.TextColor3 = Color3.fromRGB(15, 40, 34)
    button.TextSize = 18
    button.Font = Enum.Font.GothamBold
    button.Parent = panel
    local rounding = Instance.new("UICorner")
    rounding.CornerRadius = UDim.new(0, 10)
    rounding.Parent = button
    button.Activated:Connect(function()
        request:FireServer(id) -- Request an ID, never an ownership grant.
    end)
    buttons[id] = button
end

-- Bind to the existing view; this fixed-catalog exercise does not add/remove item IDs.
for _, entry in ipairs(view:GetChildren()) do
    if entry:IsA("BoolValue") then entry:GetPropertyChangedSignal("Value"):Connect(render) end
end
player:GetAttributeChangedSignal("InventoryPracticeEquipped"):Connect(render)
render()
gui.Parent = playerGui
