-- Regular Script in ServerScriptService, in a spare practice place.
-- Session-only example: no purchases, rewards, trading, or persistence.
-- Documentation-reviewed; not executed in Roblox Studio for this guide.
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")

assert(not ReplicatedStorage:FindFirstChild("InventoryPractice"), "Use one inventory practice server Script")
local remotes = Instance.new("Folder")
remotes.Name = "InventoryPractice"
local request = Instance.new("RemoteEvent")
request.Name = "EquipRequest"
request.Parent = remotes
remotes.Parent = ReplicatedStorage

local templates = Instance.new("Folder")
templates.Name = "InventoryPracticeTemplates"
templates.Parent = ServerStorage
local catalog = { Lantern = true, Compass = true, Relic = true }
for itemId in pairs(catalog) do
    local tool = Instance.new("Tool")
    tool.Name = itemId
    tool.RequiresHandle = false
    tool.CanBeDropped = false
    tool:SetAttribute("InventoryPracticeTool", true)
    tool.Parent = templates
end

local owned = {} -- Server authority. Replicated UI values are only a view.
local equippedTools = {}
local lastRequest = {}
local characterConnections = {}

local function clearPracticeTools(container)
    if not container then return end
    for _, child in ipairs(container:GetChildren()) do
        if child:IsA("Tool") and child:GetAttribute("InventoryPracticeTool") == true then
            child:Destroy()
        end
    end
end

local function setup(player)
    if owned[player] or player.Parent ~= Players then return end
    assert(not player:FindFirstChild("InventoryPracticeView"), "Practice view name is already in use")
    owned[player] = { Lantern = true, Compass = true }
    player:SetAttribute("InventoryPracticeEquipped", "")
    local view = Instance.new("Folder")
    view.Name = "InventoryPracticeView"
    for itemId in pairs(owned[player]) do
        local entry = Instance.new("BoolValue")
        entry.Name = itemId
        entry.Value = true
        entry.Parent = view
    end
    view.Parent = player
    characterConnections[player] = player.CharacterAdded:Connect(function()
        equippedTools[player] = nil
        player:SetAttribute("InventoryPracticeEquipped", "")
    end)
end

Players.PlayerAdded:Connect(setup)
Players.PlayerRemoving:Connect(function(player)
    owned[player] = nil
    equippedTools[player] = nil
    lastRequest[player] = nil
    if characterConnections[player] then
        characterConnections[player]:Disconnect()
        characterConnections[player] = nil
    end
end)
for _, player in ipairs(Players:GetPlayers()) do setup(player) end

request.OnServerEvent:Connect(function(player, itemId)
    if player.Parent ~= Players or not owned[player] then return end
    local now = os.clock()
    if now - (lastRequest[player] or -math.huge) < 0.25 then return end
    lastRequest[player] = now
    if typeof(itemId) ~= "string" or #itemId > 32 then return end
    local inventory = owned[player]
    if not inventory or catalog[itemId] ~= true or inventory[itemId] ~= true then return end
    local character = player.Character
    local humanoid = character and character:FindFirstChildOfClass("Humanoid")
    local backpack = player:FindFirstChildOfClass("Backpack")
    if not humanoid or humanoid.Health <= 0 or not backpack then return end

    local current = equippedTools[player]
    local alreadyEquipped = current and current.Parent == character and current.Name == itemId
    equippedTools[player] = nil
    clearPracticeTools(backpack)
    clearPracticeTools(character)
    player:SetAttribute("InventoryPracticeEquipped", "")
    if alreadyEquipped then return end

    local tool = templates[itemId]:Clone()
    tool.Parent = backpack
    equippedTools[player] = tool
    tool.Unequipped:Connect(function()
        if equippedTools[player] == tool then
            equippedTools[player] = nil
            player:SetAttribute("InventoryPracticeEquipped", "")
        end
    end)
    humanoid:EquipTool(tool)
    if tool.Parent == character then
        player:SetAttribute("InventoryPracticeEquipped", itemId)
    else
        equippedTools[player] = nil
        tool:Destroy()
        warn("Practice Tool did not equip for", player.Name)
    end
end)
