How would I make a loading bar for a map's assets?

I want to make a loading screen in my game for whenever a map is loading for a client. While loading the map itself functions absolutely fine, I’m having trouble with displaying the progress bar of the loading process.

local ContentProvider = game:GetService("ContentProvider")
local Players = game:GetService("Players")

local localPlayer = Players.LocalPlayer
local playerGui = localPlayer:WaitForChild("PlayerGui")
local screenGui = Instance.new("ScreenGui")
screenGui.Parent = playerGui

-- create a basic loading bar
local frame = Instance.new("Frame")
frame.Size = UDim2.new(0.5, 0, 0.1, 0)
frame.Position = UDim2.new(0.5, 0, 0.5, 0)
frame.AnchorPoint = Vector2.new(0.5, 0.5)
frame.Parent = screenGui

local bar = Instance.new("Frame")
bar.Size = UDim2.new(0, 0, 1, 0)
bar.Position = UDim2.new(0, 0, 0, 0)
bar.BackgroundColor3 = Color3.new(0, 0, 1)
bar.Parent = frame

local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://9120386436"
local sound2 = Instance.new("Sound")
sound2.SoundId = "rbxassetid://9120385974"

local assets = {
	sound,
	sound2,
}

task.wait(3)

for i = 1, #assets do
	local asset = assets[i]
	ContentProvider:PreloadAsync({ asset }) -- 1 at a time, yields
	local progress = i / #assets
	bar.Size = UDim2.new(progress, 0, 1, 0)
end

print("loading done")

This is Roblox’s way of making a loading bar, listed on the Creator Documentation. While this works fine for loading just a handful of assets, this doesn’t work at all for loading maps since they typically contain thousands of assets. All this does is artificially increase load times and causes immense lag.

I was thinking that to achieve as lag-free of a loading process as possible and display its progress in a bar, I would divide the amount of descendants in the map by the amount of the descendants that have loaded and apply that to the X scale of the loading bar. Is there any way to do this reliably?

wait for each object using workspace:WaitForChild() then increase a certain value (used for the loading process)

How would I do that while using ContentProvider since that’s how I’m loading the maps?

Never mind; I figured out a solution on my own by loading each asset individually but putting a task.wait for certain assets.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.