You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Roblox自定义加载UI无法显示,请求问题排查帮助

Roblox自定义加载屏不显示问题

问题详情

尝试替换Roblox游戏的加载屏,运行时仅显示默认加载屏最长5秒,即便游戏仍在加载,自定义加载屏也无法显示。使用的GUI名为“Loading Screen”,附原LocalScript代码:

local replicatedFirst = game:GetService("ReplicatedFirst")
local contentProvider = game:GetService("ContentProvider")
local tweenService = game:GetService("TweenService")
local players = game:GetService("Players")

local player = players.LocalPlayer
local playerGui = player.WaitForChild("PlayerGui")
local loadingScreen = script:WaitForChild("Loading Screen")

replicatedFirst:RemoveDefaultLoadingScreen()

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

local assets = game:GetDescendants()

local clonedLoadingScreen = loadingScreen:Clone()
clonedLoadingScreen.Parent = playerGui

tweenService:Create(clonedLoadingScreen.Background.GameTitle, TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, -1, true), {Position = UDim2.fromScale(clonedLoadingScreen.Background.GameTitle.Position.X.Scale, clonedLoadingScreen.Background.GameTitle.Position.Y.Scale + 0.02)}):Play()

for i = 1, #assets do
    local asset = assets[i]
    local percentage = math.round(i/ #assets * 100)
    
    contentProvider:PreloadAsync({asset})
    
    clonedLoadingScreen.Background.DisplayPercentage.Text = percentage.."%"
    clonedLoadingScreen.Background.DisplayAssetsLoaded.Text = "Loading Assest: "..i.."/"..#assets
    
    tweenService:Create(clonedLoadingScreen.Background.BarBackground.Bar, TweenInfo.new(0.2,Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Size = UDim2.fromScale(percentage/100, 1)}):Play()
    
    if i % 25 == 0 then
        task.wait()
    end
    
    if i == #assets then
        task.wait(1)
    end
end

for i, v in pairs(clonedLoadingScreen:GetDescendants()) do
    if v:IsA("Frame") then
        tweenService:Create(v, TweenInfo.new(0.5), {BackgroundTransparency = 1}):Play()
    elseif v:IsA("TextLabel") then
        tweenService:Create(v, TweenInfo.new(0.5), {TextTransparency = 1}):Play()
    end
end

问题原因与修复方案

核心问题

  1. 加载屏创建时机滞后:原代码先等待game:IsLoaded()完成才创建自定义加载屏,此时默认加载屏已经消失,导致玩家看不到自定义界面。
  2. 预加载范围不合理:game:GetDescendants()会遍历所有实例(包括服务、脚本等无需预加载的内容),不仅效率极低,还可能因无效实例引发加载错误。
  3. 拼写错误:代码中Loading Assest应为Loading Assets(不影响功能,但属于规范问题)。

修改后的代码

local replicatedFirst = game:GetService("ReplicatedFirst")
local contentProvider = game:GetService("ContentProvider")
local tweenService = game:GetService("TweenService")
local players = game:GetService("Players")

local player = players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local loadingScreen = script:WaitForChild("Loading Screen")

-- 先移除默认加载屏并立即显示自定义加载屏
replicatedFirst:RemoveDefaultLoadingScreen()
local clonedLoadingScreen = loadingScreen:Clone()
clonedLoadingScreen.Parent = playerGui

-- 播放标题动画
tweenService:Create(clonedLoadingScreen.Background.GameTitle, TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, -1, true), {
    Position = UDim2.fromScale(
        clonedLoadingScreen.Background.GameTitle.Position.X.Scale,
        clonedLoadingScreen.Background.GameTitle.Position.Y.Scale + 0.02
    )
}):Play()

-- 等待游戏核心资源加载完成
repeat task.wait() until game:IsLoaded()

-- 过滤需要预加载的资源类型,避免无效加载
local validAssetTypes = {
    "Texture", "Sound", "MeshPart", "ImageLabel", "ImageButton",
    "Animation", "ParticleEmitter", "Trail"
}
local assets = {}
for _, instance in ipairs(game:GetDescendants()) do
    if table.find(validAssetTypes, instance.ClassName) then
        table.insert(assets, instance)
    end
end

-- 预加载资源并更新进度,添加错误捕获防止流程中断
for i, asset in ipairs(assets) do
    local percentage = math.round(i / #assets * 100)
    
    -- 捕获预加载错误,避免单个资源失败导致整个加载流程卡住
    local success, err = pcall(function()
        contentProvider:PreloadAsync({asset})
    end)
    if not success then
        warn("Failed to preload asset: " .. asset.Name .. ", Error: " .. err)
    end
    
    clonedLoadingScreen.Background.DisplayPercentage.Text = percentage .. "%"
    clonedLoadingScreen.Background.DisplayAssetsLoaded.Text = "Loading Assets: " .. i .. "/" .. #assets
    
    tweenService:Create(clonedLoadingScreen.Background.BarBackground.Bar, TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {
        Size = UDim2.fromScale(percentage / 100, 1)
    }):Play()
    
    if i % 25 == 0 then
        task.wait()
    end
end

-- 等待1秒后淡出加载屏,完成后销毁界面释放内存
task.wait(1)
for _, v in pairs(clonedLoadingScreen:GetDescendants()) do
    if v:IsA("Frame") then
        tweenService:Create(v, TweenInfo.new(0.5), {BackgroundTransparency = 1}):Play()
    elseif v:IsA("TextLabel") then
        tweenService:Create(v, TweenInfo.new(0.5), {TextTransparency = 1}):Play()
    end
end
task.wait(0.5)
clonedLoadingScreen:Destroy()

关键修改说明

  • 提前创建并挂载自定义加载屏,确保默认加载屏移除后立即显示
  • 过滤预加载资源类型,只处理需要加载的媒体资源,提升效率
  • 用pcall包裹预加载逻辑,避免单个资源加载失败导致整个流程终止
  • 修复拼写错误,规范文本内容
  • 加载完成后销毁加载屏,避免内存冗余

内容的提问来源于stack exchange,提问作者printAmerigo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 21:24:58