-- LocalScript in StarterPlayer > StarterPlayerScripts.
-- Documentation-reviewed; not executed in Studio for this guide.
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = Players.LocalPlayer
local remote = ReplicatedStorage:WaitForChild("CoinShopPractice")
local playerGui = player:WaitForChild("PlayerGui")
if playerGui:FindFirstChild("CoinShopPracticeGui") then return end
local gui = Instance.new("ScreenGui")
gui.Name = "CoinShopPracticeGui"
gui.ResetOnSpawn = false
local panel = Instance.new("Frame")
panel.Size = UDim2.new(0.85, 0, 0, 280)
panel.Position = UDim2.new(0.5, 0, 0.5, 0)
panel.AnchorPoint = Vector2.new(0.5, 0.5)
panel.BackgroundColor3 = Color3.fromRGB(26, 38, 48)
panel.Visible = false
panel.Parent = gui
local sizeLimit = Instance.new("UISizeConstraint")
sizeLimit.MaxSize = Vector2.new(400, 280)
sizeLimit.Parent = panel
local corner = Instance.new("UICorner")
corner.CornerRadius = UDim.new(0, 16)
corner.Parent = panel
local balance = Instance.new("TextLabel")
balance.Size = UDim2.new(1, -32, 0, 44)
balance.Position = UDim2.fromOffset(16, 8)
balance.BackgroundTransparency = 1
balance.Text = "Coins: loading"
balance.Font = Enum.Font.GothamBold
balance.TextSize = 22
balance.TextColor3 = Color3.fromRGB(255, 222, 148)
balance.Parent = panel
local status = Instance.new("TextLabel")
status.Size = UDim2.new(1, -32, 0, 64)
status.Position = UDim2.fromOffset(16, 198)
status.BackgroundTransparency = 1
status.TextWrapped = true
status.Font = Enum.Font.Gotham
status.TextSize = 16
status.TextColor3 = Color3.fromRGB(230, 238, 241)
status.Parent = panel
local toggle = Instance.new("TextButton")
toggle.Size = UDim2.fromOffset(160, 44)
toggle.Position = UDim2.new(0.5, 0, 1, -24)
toggle.AnchorPoint = Vector2.new(0.5, 1)
toggle.Text = "Shop"
toggle.Font = Enum.Font.GothamBold
toggle.TextSize = 18
toggle.BackgroundColor3 = Color3.fromRGB(255, 222, 148)
toggle.TextColor3 = Color3.fromRGB(26, 38, 48)
toggle.Parent = gui

local rows, busy = {}, false
local function render(response)
    balance.Text = "Coins: " .. response.coins
    status.Text = response.message
    for _, item in ipairs(response.items) do
        local button = rows[item.id]
        if button then
            button:SetAttribute("Owned", item.owned)
            button.Text = item.owned and (item.id .. " / Owned")
                or ("Buy " .. item.id .. " / " .. item.price .. " Coins")
            button.Active = not item.owned
            button.AutoButtonColor = not item.owned
        end
    end
end
local function send(itemId)
    if busy then return end
    busy = true
    status.Text = "Checking with server..."
    local success, response = pcall(function() return remote:InvokeServer(itemId) end)
    busy = false
    if success and type(response) == "table" and type(response.coins) == "number"
        and type(response.items) == "table" then
        render(response)
    else
        status.Text = "Shop unavailable. Close and reopen to retry."
    end
end
for index, itemId in ipairs({ "Lantern", "Compass" }) do
    local id = itemId
    local button = Instance.new("TextButton")
    button.Size = UDim2.new(1, -32, 0, 56)
    button.Position = UDim2.fromOffset(16, 62 + (index - 1) * 66)
    button.Text = id .. " / loading"
    button.Font = Enum.Font.GothamBold
    button.TextSize = 18
    button.BackgroundColor3 = Color3.fromRGB(161, 220, 218)
    button.TextColor3 = Color3.fromRGB(26, 38, 48)
    button.Parent = panel
    rows[id] = button
    button.Activated:Connect(function()
        if not button:GetAttribute("Owned") then send(id) end
    end)
end
toggle.Activated:Connect(function()
    panel.Visible = not panel.Visible
    toggle.Text = panel.Visible and "Close shop" or "Shop"
    if panel.Visible then send(nil) end
end)
gui.Parent = playerGui
