Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

For example

Part = game.Workspace.Sword.Part

Can I access Part through game?

Part = Part:FindFirstChildOfClass(game) ?

I don't know

1 comment

0

FindFirstChild has an optional recursive search arg.

1 answer

✓ Accepted answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.