Returning a value inside an If statement in a function

Hello devs, I’m a bit stuck on an issue that I’ve been trying to solve for the past hour. Currently Im trying to create a function that will loop untill it finds the top model. This was for the issue of models inside models and me wanting to get the Parent of the all. The issue here is that the return statement is returning Nil to the other piece of code. I have tried looking through all the Forums I could find that were semi related but none of them seemed to be the issue that I was facing

Here is my code:

function parentCheck(model)
	local model = model
	if model:FindFirstAncestorOfClass("Model") then
		print("If: ", model)
		model = model:FindFirstAncestorOfClass("Model")
		parentCheck(model)
	else
		print("Else: ", model)
		return model
	end
end

Any help would be much appreciated whether its a redirection or an explanation!

1 Like

If I understand correctly, that function would return nil to the initial function call in all of the cases where the first condition is true (where the model passed into the function has an Ancestor which is also a model) because that would result in the function calling itself again.

As a result, when the code in the else statement runs, it’s returning the model to the most recent function call (which happened inside of the function the previous time it ran) instead of the initial function call from somewhere else in the script.


You can test that this is happening by passing a Model into the function that has been placed directly into the Workspace and has no Ancestors which are also Models – in this case, the first condition would be false, causing it to immediately return the model to the initial function call, as it didn’t need to call the function again to look for the next Ancestor with the Model ClassName.

(If my explanation was too confusing, please let me know and I’ll try to explain it further!)


Here’s an example revision of that function that utilizes a loop (avoiding having to call it over and over again from within the function itself) to find the highest-level parent Model:

Example Revision

local function parentCheck(model)
    local topModel = model

    for count = 0, math.huge do
        local ancestorModelCheck = topModel:FindFirstAncestorOfClass("Model")
        if ancestorModelCheck then
            topModel = ancestorModelCheck
        else
            break
        end
    end

    return topModel
end
3 Likes

Wow! Thank you so much. I cant believe the solution was as simple as this but I would never have thought of in the first place.
I haven’t really used math.huge before but if I am assuming correctly that for loop will run for math.huge’s number which is a long enough time for the script to find the top model?
Then it will break if there is no model found higher in the hiearchy and return the top model.

Sorry if I wasted your time on something like this, your explanation was great!

1 Like

Precisely! math.huge is essentially infinity, so defining that as the end value for the loop is a way of guaranteeing that the loop would continue for enough iterations until it finds the top model.

No worries! For me, responding to threads on the Developer Forum is never a waste of time since no matter what, there’s always something that can be learned from it (and, best case scenario is that my responses help out a whole bunch of* people who end up reading it).

Edit: Fixed typo

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.