How to make my loading bar load objects with defined materials?

Hi, I have a problem about loading some parts with the script depending on its material, but it doesn’t and gives an error, the script looks for all the parts and looks for the parts with the defined material and it should fill a loading bar. The error is given to me in the part of “for index, descendant in pairs (LoadTrees) do

local ThreeDesc = workspace:WaitForChild("Arboles"):GetDescendants()
local TreeEnabled = false

local function FireBarBg(plr, Position, Text) --changed
	if tostring(plr.Name) == tostring(game.Players.LocalPlayer.Name) then
		if ValorFin < 2 then
			if CircularDirection.Value == true then
				CircularDirection.Value = false
			end
			if ValorFin < 1 then
				for index, descendant in pairs(LoadTrees) do
					if descendant:IsA("BasePart") then
						if {descendant.Material == Enum.Material.Wood and Enum.Material.Grass} or 
							{descendant.Material == Enum.Material.Plastic} then
							loadingScreen.Background.BarBG.Bar.Size = UDim2.new(math.clamp(index/ LoadTrees, 0, 0.35), 0, 1, 0)
							loadingScreen.Background.BarBG.Bar:TweenSize((Position), nil, nil, 0.6)
							contenProvider:PreloadAsync({descendant})
						end
					end
				end
			end
		end
	end
end

ScreenBarBgGraphicsFire.OnClientEvent:Connect(FireBarBg)

Can you give us the code of where you defined the variable LoadTrees? And the actual error that the code outputs.

Without knowing any more, at the moment I have two ideas.:

  1. The error is “invalid argument #1 to 'pairs' (table expected, got Instance)
    That means you are trying to call pairs on an instance instead of a table.
    Maybe you meant to say LoadTrees:GetChildren() or LoadTrees:GetDescendants()?

  2. The error is “invalid argument #1 to 'pairs' (table expected, got nil)
    You never even defined LoadTrees to begin with–its a variable assigned to nil.

local LoadTrees = #ThreeDesc
sorry.

local ThreeDesc = workspace:WaitForChild("Arboles"):GetDescendants()
local LoadTrees = #ThreeDesc
local TreeEnabled = false

local function FireBarBg(plr, Position, Text) --changed
	if tostring(plr.Name) == tostring(game.Players.LocalPlayer.Name) then
		if ValorFin < 2 then
			if CircularDirection.Value == true then
				CircularDirection.Value = false
			end
			if ValorFin < 1 then
				for index, descendant in pairs(LoadTrees) do
					if descendant:IsA("BasePart") then
						if {descendant.Material == Enum.Material.Wood and Enum.Material.Grass} or 
							{descendant.Material == Enum.Material.Plastic} then
							loadingScreen.Background.BarBG.Bar.Size = UDim2.new(math.clamp(index/ LoadTrees, 0, 0.35), 0, 1, 0)
							contenProvider:PreloadAsync({descendant})
						end
					end
				end
			end
		end
	end
end

ScreenBarBgGraphicsFire.OnClientEvent:Connect(FireBarBg)

The issue is that you’re trying to call pairs on a number, not an array.

Do this instead:

for index, descendant in pairs(ThreeDesc) do

since ThreeDesc is the array with the descendants.

1 Like