Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there a way of finding an instance that is descendant of a service?

Asked by 7 years ago

For example

Part = game.Workspace.Sword.Part

Can I access Part through game?

Part = Part:FindFirstChildOfClass(game) ?

I don't know

0
FindFirstChild has an optional recursive search arg. User#5423 17 — 7y

1 answer

Log in to vote
0
Answered by
RubenKan 3615 Moderation Voter Administrator Community Moderator
7 years ago

You can't do this on game, because it has some RobloxLocked instances. Attempting to index these will error.

You can however run the following function i made, over workspace and other instances. It will return everything inside a single table.

local function GetDescendants(o)

    local AllObjects = {}

    local function FindChildren(Object)
       for _,v in pairs(Object:GetChildren()) do
            table.insert(AllObjects,v)
            FindChildren(v)
        end
    end

    if o then 
        if typeof(o) == "Instance" then
            FindChildren(o)
        else
            warn("Object is not an Instance.")
        end
    else
        warn("No object given.")
    end
    if #AllObjects == 0 then
        return nil
    end
    return AllObjects
end


local AllTheInstances = GetDescendants(workspace)
Ad

Answer this question