Trouble finding parent of an object using instances

Hey there!
I’m having a little problem with locating an object holder
I need to get the actual model of the prompt it holds,
but the prompt is inside several other parts which makes it harder to find as not all models are sharing the same the path

EXAMPLE

Path 1

Path 2

image

is possible to do without collecting all descendants from workspace somehow?

		PPS.PromptShown:Connect(function(PromptInstance)
		if CS:HasTag(PromptInstance, "Loot") then

i really want to make my script cleaner (using instances) without using extra lines x)
Happy new year btw! : ]

1 Like

The simplest way I can think of would be to parent an ObjectValue under the proximity prompt and set its Value to the model. Then, to get the model, you can use:

-- "ModelReference" is a decent name for the ObjectValue
local model = proximityPrompt.ModelReference.Value

:FindFirstAncestorWhichIsA('Model') will search for an ancestor of the instance which is of the class Model

1 Like

Assuming the highlighted instance in each example is the one you desire, then @Content_Corrupted019’s solution is best

There is no desire to consider subclasses of “Model”. Use Instance:FindFirstChildOfClass instead

Oh, I never knew the difference between both methods, FindFirstAncestorOfClass will work better and faster

1 Like

Just use prints to find it … or show where you are.

Yeah! It goes unnoticed by many. One method is built on Instance.ClassName and the other Object:IsA. The latter’s API reference puts it into perspective:

“IsA returns true if the object’s class is equivalent to or a subclass of a given class. This function is similar to the instanceof operators in other languages, and is a form of type introspection. To ignore class inheritance, test the ClassName property directly instead. For checking native Lua data types (number, string, etc) use the functions type and typeof.”

This allows you to match either the exact class or anything that descends from a class. A useful example is Instance:IsA("BasePart"), where BasePart is the root class of all physical instances. We can use IsA to filter for anything and everything physical without having to match each class

1 Like

OP’s looking for a dynamic solution for deriving a particular instance in code

1 Like

Alright, back with update!
Thanks @sponguswastaken for useful documentaion by the way. i already tried to use this instance before and got a bit confused why it didn’t work

Thing is i tried to reach the point where it doesn’t matter in what order and in how many parts the prompt part is, it’d still reach the first model it belongs to, but ig its just impossible to make it with instances…
easiest solution is to organize paths perhabs… which is im bad at, but we all have something to learn on ig ; ]

here’s new code piece reference if someone might need!

PPS.PromptShown:Connect(function(PromptInstance)
if CS:HasTag(PromptInstance:FindFirstAncestorWhichIsA("BasePart").Parent, "Loot") then

Thank ya all for trying to help me!!
Happy 2025 everyone! :partying_face:

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