-- LocalScript in ReplicatedFirst, in a spare practice place.
-- Documentation-reviewed and mock-checked; not executed in Roblox Studio for this guide.
-- Selected-image preloading only. Closing this overlay does not prove gameplay readiness.
local Players = game:GetService("Players")
local ReplicatedFirst = game:GetService("ReplicatedFirst")
local ContentProvider = game:GetService("ContentProvider")
local IMAGE_IDS = script:GetAttribute("ImageIds") -- Optional comma-separated image IDs.
if IMAGE_IDS == nil then IMAGE_IDS = "" end
local TIMEOUT_SECONDS = 20
local playerGui = Players.LocalPlayer:WaitForChild("PlayerGui", 10)
if not playerGui then warn("Loading screen: PlayerGui did not arrive") return end
if playerGui:FindFirstChild("PracticeLoadingScreen") then warn("Keep one loading-screen controller") return end
local ids, seen = {}, {}
if type(IMAGE_IDS) ~= "string" then
    warn("ImageIds must be a String attribute; no custom images selected")
    IMAGE_IDS = ""
end
for id in IMAGE_IDS:gmatch("[^,%s]+") do
    local digits = id:match("^rbxassetid://(%d+)$")
    if digits and tonumber(digits) and tonumber(digits) > 0 then
        if not seen[id] then seen[id] = true table.insert(ids, id) end
    else
        warn("Ignored invalid image ID; use rbxassetid:// followed by a positive numeric ID")
    end
end
local gui = Instance.new("ScreenGui")
gui.Name = "PracticeLoadingScreen"
gui.ResetOnSpawn = false
gui.DisplayOrder = 20
local backdrop = Instance.new("Frame")
backdrop.Name = "Backdrop"
backdrop.Size = UDim2.fromScale(1, 1)
backdrop.BackgroundColor3 = Color3.fromRGB(28, 36, 63)
backdrop.BorderSizePixel = 0
backdrop.Parent = gui
local panel = Instance.new("ScrollingFrame")
panel.Name = "Panel"
panel.Size = UDim2.new(1, -32, 1, -32)
panel.Position = UDim2.fromScale(0.5, 0.5)
panel.AnchorPoint = Vector2.new(0.5, 0.5)
panel.BackgroundColor3 = Color3.fromRGB(28, 36, 63)
panel.BorderSizePixel = 0
panel.CanvasSize = UDim2.fromOffset(0, 0)
panel.AutomaticCanvasSize = Enum.AutomaticSize.Y
panel.ScrollBarThickness = 4
panel.Parent = backdrop
local limit = Instance.new("UISizeConstraint")
limit.MaxSize = Vector2.new(520, 330)
limit.Parent = panel
local padding = Instance.new("UIPadding")
padding.PaddingTop = UDim.new(0, 20)
padding.PaddingBottom = UDim.new(0, 20)
padding.PaddingLeft = UDim.new(0, 20)
padding.PaddingRight = UDim.new(0, 20)
padding.Parent = panel
local layout = Instance.new("UIListLayout")
layout.Padding = UDim.new(0, 12)
layout.SortOrder = Enum.SortOrder.LayoutOrder
layout.Parent = panel
local order = 0
local function label(name, text, size, height)
    order = order + 1
    local item = Instance.new("TextLabel")
    item.Name = name
    item.LayoutOrder = order
    item.Size = UDim2.new(1, 0, 0, height)
    item.AutomaticSize = Enum.AutomaticSize.Y
    item.BackgroundTransparency = 1
    item.TextColor3 = Color3.fromRGB(244, 245, 255)
    item.Font = Enum.Font.Gotham
    item.TextSize = size
    item.TextWrapped = true
    item.Text = text
    item.Parent = panel
    return item
end
label("Title", "Preparing your first scene", 24, 36)
local progress = label("Progress", "Checking selected images...", 18, 40)
local status = label("Status", "Waiting for initial game data...", 16, 44)
label("Note", "This checks selected images, not the whole world or server game state.", 14, 44)
order = order + 1
local skip = Instance.new("TextButton")
skip.Name = "Skip"
skip.LayoutOrder = order
skip.Size = UDim2.new(1, 0, 0, 44)
skip.BackgroundColor3 = Color3.fromRGB(220, 191, 255)
skip.TextColor3 = Color3.fromRGB(28, 36, 63)
skip.Font = Enum.Font.Gotham
skip.TextSize = 16
skip.Text = "Skip this screen"
skip.Parent = panel
local closed, checked, unconfirmed = false, 0, 0
local attemptsDone = #ids == 0
local loadedConnection
local function close(reason)
    if closed then return end
    closed = true
    if loadedConnection then loadedConnection:Disconnect() end
    gui:Destroy()
    print("Loading overlay closed:", reason)
end
local function refresh()
    if closed then return end
    progress.Text = #ids == 0 and "No custom images selected" or
        string.format("Checked %d / %d image IDs; %d unconfirmed", checked, #ids, unconfirmed)
    if not attemptsDone then
        status.Text = "Checking the selected image list..."
    elseif not game:IsLoaded() then
        status.Text = "Waiting for initial game data..."
    elseif unconfirmed > 0 then
        status.Text = "Some images were not confirmed. Check Output or continue without them."
        skip.Text = "Continue without images"
    else
        close("initial data and selected image checks finished")
    end
end
skip.Activated:Connect(function() close("continued manually") end)
loadedConnection = game.Loaded:Connect(refresh)
gui.Parent = playerGui -- Prepare our replacement before removing the default screen.
ReplicatedFirst:RemoveDefaultLoadingScreen()
task.delay(TIMEOUT_SECONDS, function() close("timeout; readiness not established by this overlay") end)
refresh()
task.spawn(function()
    for _, id in ipairs(ids) do
        if closed then return end
        local image = Instance.new("ImageLabel")
        image.Image = id
        local succeeded, badStatus = false, false
        local ok, err = pcall(function()
            ContentProvider:PreloadAsync({image}, function(_, result)
                if result == Enum.AssetFetchStatus.Success then succeeded = true else badStatus = true end
            end)
        end)
        image:Destroy()
        if closed then return end -- An in-flight request may return after skip/timeout.
        checked = checked + 1
        if not ok or not succeeded or badStatus then
            unconfirmed = unconfirmed + 1
            warn("Image not confirmed:", id, err or "inspect asset status, permissions and Output")
        end
        refresh()
    end
    attemptsDone = true
    refresh()
end)
