How to skip a preload for loop

I want to add a Skip button on my loading screen, but not sure how to break the for loop when a bindable event has been fired.

local Player = game:GetService("Players").LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ContentProvider = game:GetService("ContentProvider")
local RunService = game:GetService("RunService")
local Events = ReplicatedStorage:WaitForChild("Events", 60)
local ClearUiEvent = Events.ClearLoadingUI
local StoredAssets = require(ReplicatedStorage:WaitForChild("Assets", 60))
local LoadingGui = Player.PlayerGui:WaitForChild("LoadingGui", 60)
local LoadingBar = LoadingGui.Background.Loading.Main.Bar
local AssetCountLabel = LoadingGui.Background.Loading.AssetCount

if RunService:IsStudio() then return end

LoadingGui.Enabled = true
local Assets = {}
for _, Asset in ipairs(StoredAssets.Ids) do
	table.insert(Assets, #Assets+1, Asset)
	print(Asset)
	wait()
end

for _, Object in ipairs(workspace:GetDescendants()) do
	if Object:IsA("MeshPart") then
		if string.match(Object.MeshId, "^rbxgameasset://") or Object.MeshId == "rbxassetid://111" or Object.MeshId == "rbxassetid://1" then
			return
		end
		table.insert(Assets, #Assets+1, Object)
		wait()
	end
end

local TotalAssetsLoaded = 0
AssetCountLabel.Text = "Loaded Assets: " .. TotalAssetsLoaded .. "/" .. #Assets
for _, Asset in ipairs(Assets) do
	local Success
	while not Success do
		Success = pcall(function()
			ContentProvider:PreloadAsync({Asset})
		end)
		wait()
	end
	if Success then
		TotalAssetsLoaded = TotalAssetsLoaded + 1
	
		LoadingBar.Size = UDim2.fromScale(TotalAssetsLoaded/#Assets, 1)
		AssetCountLabel.Text = "Loaded Assets: " .. TotalAssetsLoaded .. "/" .. #Assets
	end
end

if TotalAssetsLoaded == #Assets then
	ClearUiEvent:Fire()
end

Can’t you just make it so when the skip button is clicked, the ClearUiEvent is fired?

1 Like

Yes, but the loading will still happen.

I’ve decided that I will do this.

Maybe make the skip button appear after a bit of loading. The game still has to load, so inserting a skip button will do nothing. The player will have to wait the same time.