repeat task.wait() until game:IsLoaded()

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

local DOMAIN = "zerxen.pages.dev"

local ROOT_URL = "https://" .. DOMAIN .. "/"
local SCRIPTS_URL = ROOT_URL .. "scripts/"
local GAMES_URL = ROOT_URL .. "games/"

-- Load supported games list from /scripts/supported.lua
local success, SUPPORTED_GAMES = pcall(function()
    return loadstring(game:HttpGet(SCRIPTS_URL .. "supported.lua", true))()
end)

if not success or type(SUPPORTED_GAMES) ~= "table" then
    LocalPlayer:Kick("[Zerxen Hub] Failed to load game list.")
    return
end

-- Get script filename from supported.lua
local scriptFile = SUPPORTED_GAMES[game.PlaceId]

if not scriptFile then
    LocalPlayer:Kick("Zerxen Hub does not support this game.\nPlaceId: " .. tostring(game.PlaceId))
    return
end

-- Remove leading slash if accidentally present
scriptFile = tostring(scriptFile):gsub("^/+", "")

-- Game scripts are inside /games/
local scriptUrl = GAMES_URL .. scriptFile

-- Download game script
local ok, content = pcall(function()
    return game:HttpGet(scriptUrl, true)
end)

if not ok or not content or content:sub(1, 1) == "<" then
    LocalPlayer:Kick("Failed to load script. Please re-execute.")
    return
end

-- Compile and run
local fn, err = loadstring(content)

if not fn then
    LocalPlayer:Kick("Compile error: " .. tostring(err))
    return
end

pcall(fn)
