Why is my asset loading bar stopping at random numbers?

Hi! So I currently have a loading bar, pretty self explanatory, however, it keeps stopping before reaching 100% (stops at 90, 96, 84, etc). Code is located below! :wink:

local Players = game:GetService('Players')
local Lighting = game:GetService('Lighting')

local Player = Players.LocalPlayer
local PlayerGui = Player:WaitForChild('PlayerGui')

local Screen = PlayerGui:WaitForChild('Intro')

local Container = Screen.Container
local Bar_Container = Container.Bar_Container
local Bar_Effects = Bar_Container.Bar_Effects
local Bar = Bar_Effects.Bar
local Text = Bar_Effects.Status

local ContentProvider = game:GetService('ContentProvider')

local FirstRef = tonumber(ContentProvider.RequestQueueSize)
warn('firstref: '..FirstRef)

while ContentProvider.RequestQueueSize > 0 do
	if FirstRef ~= 0 then
		Percent = 100 - math.floor((tonumber(ContentProvider.RequestQueueSize) / FirstRef) * 100)
		warn('loaded: '..Percent)
		Text.Text = string.format('Loading.. [%s%s]', Percent, '%')
		Bar:TweenSize(UDim2.new(Percent * .01, 0, 1, 0), 1, 5, 0, true)
	end

	wait()
end

Anyone have an answer? I’ve been looking at other threads and can’t seem to find an answer. :sad:

It stops completely, or just pauses before continuing?

It stops indefinitely (atleast for 2 minutes is what I’ve tried)

Updated topic a bit! (300CHARS)

Because of this:

while ContentProvider.RequestQueueSize > 0 do

Since it is ā€œgreater thanā€ you will never get the last run through the loop where it will be 100%. You need to make it >= and add a separate if statement in the loop that will break on == 0 (or add another tween after the loop to move it to 100%)

Hey, I tried this out and it may just be it is getting loaded before I actually load in, however, I don’t think this is the case because I still see the prints happening!

Please feel free to correct my code if I made any further mistakes :wink:;

local Players = game:GetService('Players')
local Lighting = game:GetService('Lighting')

local Player = Players.LocalPlayer
local PlayerGui = Player:WaitForChild('PlayerGui')

local Screen = PlayerGui:WaitForChild('Intro')

local Container = Screen.Container
local Bar_Container = Container.Bar_Container
local Bar_Effects = Bar_Container.Bar_Effects
local Bar = Bar_Effects.Bar
local Text = Bar_Effects.Status

local ContentProvider = game:GetService('ContentProvider')

local FirstRef = tonumber(ContentProvider.RequestQueueSize)
warn('firstref: '..FirstRef)

while ContentProvider.RequestQueueSize >= 0 do
	if FirstRef ~= 0 then
		Percent = 100 - math.floor((tonumber(ContentProvider.RequestQueueSize) / FirstRef) * 100)
		warn('loaded: '..Percent)
		Text.Text = string.format('Loading.. [%s%s]', Percent, '%')
		Bar:TweenSize(UDim2.new(Percent * .01, 0, 1, 0), 1, 5, 0, true)
	end
	
	if ContentProvider.RequestQueueSize == 0 then
		Screen.Enabled = false
	end

	wait()
end

You might just not be waiting long enough for the tween to finish. If you are seeing the print to 100% but the loading bar doesn’t make it that far, you should add a wait before disabling the screen. And add a break to stop the loop.

	if ContentProvider.RequestQueueSize == 0 then
                wait(2)
		Screen.Enabled = false
                break
	end