-- Regular Script in ServerScriptService. Use a spare practice place.
-- Session Coins only: no Robux, purchases with money, or saved progress.
-- Documentation-reviewed; not executed in Studio for this guide.
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
assert(not ReplicatedStorage:FindFirstChild("CoinShopPractice"), "Use one shop server Script")
local remote = Instance.new("RemoteFunction")
remote.Name = "CoinShopPractice"
local catalog = {
    { id = "Lantern", price = 100 },
    { id = "Compass", price = 75 },
}
local byId, templates = {}, {}
for _, item in ipairs(catalog) do
    byId[item.id] = item
    local tool = Instance.new("Tool")
    tool.Name = item.id
    tool.RequiresHandle = false -- Nonphysical placeholder, no light/navigation behavior.
    tool.CanBeDropped = false
    tool:SetAttribute("CoinShopPracticeTool", true)
    tool.Parent = ServerStorage
    templates[item.id] = tool
end
local profiles, lastRequest, characterConnections = {}, {}, {}

local function snapshot(player, ok, message)
    local profile = profiles[player]
    if not profile then return { ok = false, message = "Session unavailable" } end
    local items = {}
    for _, item in ipairs(catalog) do
        table.insert(items, {
            id = item.id, price = item.price, owned = profile.owned[item.id] == true,
        })
    end
    return { ok = ok, message = message, coins = profile.coins, items = items }
end

local function giveTool(player, itemId)
    local backpack = player:FindFirstChildOfClass("Backpack")
    if not backpack then return false end
    local character = player.Character
    local existing = backpack:FindFirstChild(itemId)
        or (character and character:FindFirstChild(itemId))
    if existing then
        return existing:IsA("Tool") and existing:GetAttribute("CoinShopPracticeTool") == true
    end
    local tool = templates[itemId]:Clone()
    tool.Parent = backpack
    return true
end

local function setup(player)
    if profiles[player] or player.Parent ~= Players then return end
    profiles[player] = { coins = 150, owned = {} }
    characterConnections[player] = player.CharacterAdded:Connect(function(character)
        task.defer(function()
            local backpack = player:WaitForChild("Backpack", 10)
            if not backpack or player.Parent ~= Players or player.Character ~= character then return end
            local profile = profiles[player]
            if not profile then return end
            for itemId in pairs(profile.owned) do giveTool(player, itemId) end
        end)
    end)
end
Players.PlayerAdded:Connect(setup)
Players.PlayerRemoving:Connect(function(player)
    profiles[player], lastRequest[player] = nil, nil
    if characterConnections[player] then characterConnections[player]:Disconnect() end
    characterConnections[player] = nil
end)
for _, player in ipairs(Players:GetPlayers()) do setup(player) end

remote.OnServerInvoke = function(player, itemId)
    if player.Parent ~= Players or not profiles[player] then
        return { ok = false, message = "Session unavailable" }
    end
    if itemId == nil then return snapshot(player, true, "Choose an item") end
    local now = os.clock()
    if now - (lastRequest[player] or -math.huge) < 0.25 then
        return snapshot(player, false, "Please wait before trying again")
    end
    lastRequest[player] = now
    if typeof(itemId) ~= "string" or #itemId > 32 or not byId[itemId] then
        return snapshot(player, false, "Unknown item")
    end
    local profile, item = profiles[player], byId[itemId]
    if profile.owned[itemId] then return snapshot(player, false, "Already owned") end
    if profile.coins < item.price then return snapshot(player, false, "Not enough Coins") end
    local humanoid = player.Character and player.Character:FindFirstChildOfClass("Humanoid")
    if not humanoid or humanoid.Health <= 0 then
        return snapshot(player, false, "Wait for your character to be ready")
    end
    if not giveTool(player, itemId) then return snapshot(player, false, "Backpack is not ready or item name is in use") end
    -- No yielding operations between validation and these session-state updates.
    profile.coins = profile.coins - item.price
    profile.owned[itemId] = true
    return snapshot(player, true, "Purchased " .. itemId)
end
remote.Parent = ReplicatedStorage
