I'm looking for a function/roblox function that will check if an instance is a descendant of an instance with a classname "x".
I have created the function as such however I do not know if this is the best way for the system...
Confirmation or an alternative would be great.
-- This function assumes that the user deems the Instance's class as irrelevant local function DescendantOfInstanceWithClass(Instance,Classname) repeat wait() Instance = Instance.Parent if Instance:IsA(Classname) then return true end until Instance.Parent = game return false end
There is object:FindFirstAncestorOfClass(className)
and object:FindFirstAncestorWhichIsA(className)
which return the ancestor of class className
. Returns nil
if no ancestor of that class was found.
local function DescendantOfClass(object, className) return object:FindFirstAncestorOfClass(className) and true or false end print(DescendantOfClass(game:GetService("Players"), "DataModel")) --> true
My function returns true if ancestor was found, false otherwise. I used the Players service and the DataModel (aka game
) as a quick example.