Loading bar not changing size

I want to change a loading bar’s size depending on how much things have loaded into the game.

The problem is that the loading bar isn’t changing size at all in the script.

I tried setting the size to 0 and to 100 inside the script but it doesn’t work, however when I change it in ReplicatedFirst the size changes to what I changed it to perfectly.

for i, assetsToLoad in assets do
	contentprovider:PreloadAsync({assetsToLoad})
	local value = i/maxassets
	value = value * 100
	value = math.floor(value)
	ui:WaitForChild("Frame1"):WaitForChild("Percent").Text = value.."%"
	ui:WaitForChild("Frame1"):WaitForChild("TextLabel").Text = i.."/"..maxassets
	
	script.Loading.Frame1.Frame2.FinishBar.Size = UDim2.new(value/100,0,1,0)
	
end

image

If anybody knows what might be the problem I would be grateful!

For reference:


Here I changed the size to 0.1 in ReplicatedFirst, but the size doesn’t change in the script

Hello!

To iterate through tables in lua, you use for index, value in pairs(table) do.

Unrelated to your question, I suggest using :WaitForChild() only once as a variable. Continuously calling it can be inefficient since the script will repeatedly search for the child.

I take it that the largest size for FinishBar is Udim2.new(1, 0, 1, 0), if so, you can directly place the ratio inside it. Here’s an updated code:

local percent = ui:WaitForChild("Frame1"):WaitForChild("Percent")
local textLabel = ui:WaitForChild("Frame1"):WaitForChild("TextLabel")

for i, assetsToLoad in ipairs(assets) do
    local ratio = index / #maxassets
	contentprovider:PreloadAsync({assetsToLoad})
	percent.Text = `{math.floor((ratio) * 100)}%`
	textLabel.Text = i.."/".. #maxassets

	script.Loading.Frame1.Frame2.FinishBar.Size = UDim2.new(ratio,0,1,0)
end

Let me know if you still have any issues or have any questions, I hope this helps!

Hello! Thank you for the little tips! Unfortunately the size of the FinishBar stays the same.

I see that I made some small errors in my previous code, so I am sorry about that.
Could you show me what assets look like so I can better test it?

Of course!
Here is the complete code I have currently:

local contentprovider = game:GetService("ContentProvider")
local rs = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local ui = script:WaitForChild("Loading"):Clone()
local sound = rs:WaitForChild("LoadingScreenReload")

repeat wait() until game:IsLoaded()

local assets = game:GetDescendants()
local maxassets = #assets

local plr = game.Players.LocalPlayer
local plrgui = plr:WaitForChild("PlayerGui")
local percent = ui:WaitForChild("Frame1"):WaitForChild("Percent")
local textLabel = ui:WaitForChild("Frame1"):WaitForChild("TextLabel")

ui.Parent = plrgui
for i, assetsToLoad in ipairs(assets) do
	contentprovider:PreloadAsync({assetsToLoad})
	local ratio = i/maxassets
	value = ratio * 100
	value = math.floor(value)
	percent.Text = value.."%"
	textLabel.Text = i.."/"..maxassets
	script.Loading.Frame1.Frame2.FinishBar.Size = UDim2.new(ratio,0,0.8,0)
	
end

The number of assets and the max number of assets is displayed correctly and the value is also correct. But the FinishBar is not changing it’s size

After recreating the setup from your attached image, I have now fixed the issue for you. The code is slightly different from what I originally showed you, so feel free to ask if you have any questions about it :slight_smile:

local ContentProvider = game:GetService("ContentProvider")

local ui = script.ScreenGui
local assets = game:GetDescendants()

local frame2 = ui:WaitForChild("Frame1"):WaitForChild("Frame2")

local percent = frame2:WaitForChild("Percent")
local textLabel = frame2:WaitForChild("TextLabel")
local loadingBar = frame2.FinishBar

for i, assetsToLoad in ipairs(assets) do
	local ratio = i / #assets
	ContentProvider:PreloadAsync({assetsToLoad})
	percent.Text = `{math.floor((ratio) * 100)}%`
	textLabel.Text = i.."/".. #assets

	loadingBar.Size = UDim2.new(ratio,0,1,0)
end

image
image

image

1 Like

Yes, this does indeed work! So all I had to do was declare the loadingBar outside of the for loop! Thanks a lot!