Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

how to iterate through each child of an instance in workspace?

Asked by 4 years ago
Edited 4 years ago

ok so in workspace there are multiple Models called "Chicken" and every Chicken has a CharId that has a unique Value

what i need to do is iterate through each Chicken and print its CharId Value

i tried this but nothing was printed in console

local charId = game.Workspace.Chicken.CharId

for i,v in pairs(workspace:GetChildren()) do
    if v.Name == "Chicken" then
        for i,v in pairs(workspace.Chicken:GetChildren()) do 
            if v.Name == "CharId" then 
                for i,v in pairs(charId:GetChildren()) do 
                    print(charId.Value)
                end
            end
        end
    end
end

i also tried this but all it did was print the CharId value from only one Chicken in workspace multiple times

for i,v in pairs(workspace:GetChildren()) do
    if v.Name == "Chicken" then 
        print(workspace.Chicken.CharId.Value)
    end
end

can anyone help out?

1 answer

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
for _,Chicken in pairs(workspace:GetChildren) do
    if (Chicken:IsA("Model") and Chicken.Name == "Chicken") then
        local CharId = Chicken:FindFirstChild("CharId")
        if (CharId) then
            print(CharId.Value)
        end
    end
end
Ad

Answer this question