Why do my numbers start as negatives?

Hey all. So i have a loading gui, that makes use of the ContentProvider. And there are two values. One is the total amount of assets that need to be loaded in. And the other value is how many assets has loaded in. And when i run the game, the number that counts how many assets have loaded in starts at a negative number. Why is this? Some help will be very much appreciated!

Local Script:

ReplicatedFirst = game:GetService("ReplicatedFirst")

ReplicatedFirst:RemoveDefaultLoadingScreen()

local mouse = game.Players.LocalPlayer:GetMouse()

WalkSpeed = 10

script.Parent.MainFrame.Visible = true

wait(1)

P = game.Players.LocalPlayer

H = P.Character:WaitForChild("Humanoid")

H.WalkSpeed = 0

H.JumpPower = 0

local cp = game:GetService("ContentProvider")

local partsToLoad = cp.RequestQueueSize

script.Parent.MainFrame.LoadingFrame.LoadingText2.Text = "Loading Map"

print("Loading Map")

spawn(function()

cp:PreloadAsync({workspace})

end)

script.Parent.MainFrame.LoadingFrame.LoadingText2.Text = "Loading GUIs"

print("Loading GUIs")

spawn(function()

cp:PreloadAsync({game:GetService("GuiService")})

end)

while cp.RequestQueueSize > 0 do

wait(0.1)

print("Loading Assets: "..partsToLoad-cp.RequestQueueSize.." / "..partsToLoad)

script.Parent.MainFrame.LoadingFrame.LoadingText2.Text = "Loading Assets: "..partsToLoad-cp.RequestQueueSize.." / "..partsToLoad

end

This is because you calculate partsToLoad before you call PreloadAsync which adds more assets to the request queue. You should calculate partsToLoad after you call preloadAsync so that this number is more accurate.

1 Like

i moved local partsToLoad = cp.RequestQueueSize right on top of while cp.RequestQueueSize > 0 do and i still have the same results.

spawn runs the function on the next frame, so you would have to either run this using coroutines or wait before calculating partsToLoad

1 Like

Oh, never knew spawn could do that. Thanks for your help!