Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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

This discussion is preserved as it was. New answers, comments, and votes are closed.