Using for loops in childadded doesnt work?

So i was trying to make this loop that checks if the model is added to the camera, and runs a script if it does. While loops are bad so i decided to use childadded instead, i managed to print out the model name but when i tried getting everything inside that model it didnt print out anything.

Here is the code:

workspace.CurrentCamera.ChildAdded:Connect(function(i)
    -- Model is added on the camera:
    
    print(i.Name) -- prints "Model" (thats the name of the model)
    for i,x in pairs(i:GetDescendants()) do -- attempt to get everything inside model
        print(i.Name) -- prints nothing
        print(x.Name) -- prints nothing
    end
end)

If anyone could tell me if im doing something wrong or if theres a workaround i could do it would be appreciated.

1 Like

Try changing the i in the loop to something else, since there was an “i” defined at the beginning of the function

i got the same results, only printed model part and nothing else

image

1 Like

Just tested this out, I’m pretty sure the issue is that you’re trying to print the name of an index

workspace:WaitForChild("Camera").ChildAdded:Connect(function(i)
	-- Model is added on the camera:

	print(i.Name)
	for index,x in pairs(i:GetDescendants()) do
		print(x.Name)
	end
end)

Tell me if this works

2 Likes

Well maybe that works, but i added task.wait(0/1) inside the childadded loop and it fixed it for me.

1 Like