I'm having problems with :GetDescendants

so i’m making a game in the style of tower of hell, but when i change the color of a level, it happens, the problem is that it doesn’t change the color of a model inside, i don’t know the reason for this to happen, if someone can help me I appreciate it


image
image

this part that I marked that doesn’t change color

1 Like

Try adding a print in the for loop to see if it runs, and then show the results here. This script seems to be fine.

can you explain which part should I put the print?

First, add a print, make sure your loop IS running. Second, I would use BasePart. Underneath the for loop line, just add:

 print(v.Name)
if v:IsA("BasePart") then 

You might be using parts that lack BrickColor, so instead of BrickColor, do

v.Color = Color3.fromRGB(255,105,180)

This filters out part types that lack BrickColor and ensures all parts are shown, not just normal ones.

See more:
BasePart

Actually I found the problem. You are using pairs instead of ipairs, but GetDescendants returns a number-indexed table.

for i,v in ipairs(Floor1:GetDescendants()) do
    if v:IsA("Part") then
        v.BrickColor = BrickColor.new("Pink")
    end
end

I’m not sure that matters, they don’t need to refer to the index of the loop. Roblox uses pairs in their example code.

According to the LUA manual: pairs () and ipairs () are functions that can be used with a for loop to go through each element of an array or dictionary without needing to set starting or ending points. pairs () is used with dictionaries, and ipairs () is used with arrays.

image

Both pairs and ipairs work exactly the same when it comes to numerical keys in tables.

@CheekySquid and @BenMactavsin, I do not believe you are correct from my understanding. pairs does not index an array, while ipairs does.

@DonPedro_II the solution to your problem is to replace the code with:

for i,v in ipairs(Floor1:GetDescendants()) do
    if v:IsA("Part") then
        v.BrickColor = BrickColor.new("Pink")
    end
end

Basically, GetDescendants returns an array, not a table. So, pairs will not work, but ipairs will.

The index doesn’t matter here. As long as it loops through the order in which it does so as no effect on the outcome of the program. There is no required order here as long as it hits the parts.

I’ll test it right now, you could be right I’m not 100% familiar with ipairs haha


still not working

Array is a type of table though.

1 Like

Here is the final, tested solution.

for i,v in pairs(Floor1:GetDescendants()) do
    if v:IsA("BasePart") then
        v.BColor = Color3.fromRGB(255, 105, 180)
    end
end

pairs vs ipairs does NOT matter.

The issue was that wedges are not parts, they fall under BaseParts.

3 Likes

I just tested it. The issue was not in iPairs, it was because it needed to be BasePart not part.

thanks to everyone who helped <3

1 Like

Glad you got it working! Be sure to mark my solution so that people know it’s been solved :stuck_out_tongue:

1 Like