Why playerGui:GetDescendants walks half way

Good evening, by making a script that allows to find a Ui directly without necessarily making variable, I told myself that it will be simple because it is necessary to take the descendants of playerGui except that I think that my code has itself a problem but I would not identify it

--Module
local manageModule = {}

	function manageModule:FindUi(objectName)
		for _, UiObject in playerGui:GetDescendants() do
			print(UiObject)
			if UiObject.Name == objectName then
				return UiObject
			else
				return 
			end
		end
	end

return manageModule
--local 
local frameFind = manageModule:FindUi("ScrollingObjectif")
--Output
FreeCam

Thanks for Helping

This already exists with :FindFirstChild(objectName, true).
FindFirstChild Documentation

1 Like

The return statement inside the loop seems to be terminating the loop prematurely. If the first UiObject does not match the objectName, the loop will exit without checking any other objects.

-- Module
local manageModule = {}

function manageModule:FindUi(playerGui, objectName)
    for _, UiObject in ipairs(playerGui:GetDescendants()) do
        if UiObject.Name == objectName then
            return UiObject
        end
    end
    return nil -- If the object is not found
end
2 Likes

Thanks for your answer, it’s not against you but you didn’t understand what I want to do

unfortunately it’s not his, because when his FreeCam print his print via the modulescript

normaly its print all descendants