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