GetChildren() not working

Hi! I have a load assets screen. Some reason, it’s not showing the correct number of children. It’s supposed to preload 11 items but it only says one needs preloading. Can you help fix this?

Script in ReplicatedFirst:

game.ReplicatedFirst:RemoveDefaultLoadingScreen()

local gui = script.Loading
gui.Parent = game.Players.LocalPlayer.PlayerGui

local ConProv = game:GetService("ContentProvider")

local toLoad = game.Workspace:GetChildren()
local total = #toLoad

for i, v in pairs(toLoad) do
	gui.Frame.BarFrame.AssetsLoaded.Text = "Loading Assets: "..i.."/"..total
	gui.Frame.BarFrame.Bar.Size = UDim2.new(i/total, 0, 1, 0)
	ConProv:PreloadAsync({v})
end

wait(1)
gui:Destroy()

NOTE: It used to work but now it doesn’t.

1 Like

You may need to use
game.Workspace:GetDescendants()
Instead depending on what your trying to do, get descendants gets every instance that is inside of workspace
get children only gets the first layer of instances

try waiting about 0.5 seconds for the workspace to load in or just add a :WaitForChild(“Workspace”)

Since the script is in the ReplicatedFirst, you need to wait until the game is loaded to use the workspace:GetChildren().

Here is a version of your script that should work :

game.ReplicatedFirst:RemoveDefaultLoadingScreen()

local gui = script.Loading
gui.Parent = game.Players.LocalPlayer.PlayerGui

local ConProv = game:GetService("ContentProvider")

while not game:IsLoaded() do
	wait(1)
end

local toLoad = game.Workspace:GetChildren()
local total = #toLoad

for i, v in pairs(toLoad) do
	gui.Frame.BarFrame.AssetsLoaded.Text = "Loading Assets: "..i.."/"..total
	gui.Frame.BarFrame.Bar.Size = UDim2.new(i/total, 0, 1, 0)
	ConProv:PreloadAsync({v})
end

wait(1)
gui:Destroy()