This script does not transparent all the children?

So I have this scirpt that should transparent all the children in the model, but it only transparent’s 1? Any help?

local Player = game.Players.LocalPlayer

game.Workspace.BackpackGuiPart.SurfaceGui.Watches.PurpleWatch.MouseButton1Click:connect(function()

local dog = game.Workspace.PurpleWatch:GetChildren()

dog.Transparency = 0

local r = game.Workspace.RedWatch:GetChildren()

r.Transparency = 1

local b = game.Workspace.BlueWatch:GetChildren()

b.Transparency = 1

local bl = game.Workspace.BlackWatch:GetChildren()

bl.Transparency = 1

local w = game.Workspace.WhiteWatch:GetChildren()

w.Transparency = 1

end)

print("success")

:GetChildren returns a table
have to go through each item and set transparency

essentially just for loop through the children checking to see if its a part

local w = game.Workspace.WhiteWatch:GetChildren()

for i in pairs(w) do
  if w[i]:isA("BasePart") and w[i].Name ~= "Watch" then
    w[i].Transparency = 1
  end
end
1 Like

Alright I will test this, thank you :slight_smile:

You can also structure the for loop a little differently to get a value directly without having to reference the table with the index/iterator:

for i, v in pairs(w) do
   if v:IsA("BasePart") then
      v.Transparency = 1
   end
end

Probably just boils down to a design choice

P.S Don’t forget the ‘:’ to invoke the IsA method
https://developer.roblox.com/en-us/recipes/How-to-use-IsA-to-Check-a-Part-s-Class

1 Like

I am getting an error in the isA part.

See my comment above, should fix you up

Yes, I have seen this usage before. I am still getting used to Lua.
I am assuming it is syntax alternate and doesn’t affect performance, would that assumption be correct?

Silent, would there be any way to skip over one of these parts, which is just named watch?

add "and v.Name~=“Watch” to the if statement

if w[i]:IsA("BasePart") and w[i].Name~="Watch" then

This skips over the part named watch?

yes, it will only do what is in the if block if the child is both a part and not named watch

Wouldn’t it be and not v.name?

Indexxing it manually like you were doing is probably slightly slower, but the difference is so minor it doesn’t matter.

1 Like

Its not skipping over the watch :confused:

did you mix my code and silentude’s?
i edited my original code and tested; it should work

It is (part.Name ~= “Watch”) because you want it to evaluate false to stop the transparency from changing when the parts name is “Watch”

anything AND false -> false

NOT is a negator
turns false -> true and true -> false

(part.Name) is true if the part has a Name not equal to nil
(not part.Name) is true for parts that have nil in their name property

2 Likes

Indexxing it manually like you were doing is probably slightly slower, but the difference is so minor it doesn’t matter.

Since he is indexing it three times vs just once in the other for loop structure (lua indexing implicitly)

2 Likes