Detect if there is anything from a table inside workspace

I’m trying to make a script to detect if there is anything from the table inside workspace

if game.Workspace:FindFirstChild() then

--My code here

end

I think I should use FindFirstChild and a table, but im not sure how to lemme give a better example of what I want

In a table there is: “Part1”, “Part2” and “Part3”
And I want to use FindFirstChild on Workspace to check if I can find one of them in there

Thanks :slight_smile:

use a loop and run :IsDescendantOf(workspace) on each item until one of them satisfies that condition

Something like this

--returns the first instance in the table that is under workspace
local function inWorkspace(t: {Instance}): Instance?
	for _, v in t do
		if v:IsDescendantOf(workspace) then return v end
	end
	return nil
end
1 Like
local tableParts = {"Part1", "Part2", "Part3"}

for i, part in pairs(workspace:GetChildren()) do
if table.find(tableParts, part.Name) then
— code here
end
end

that helped me, but I still with a question: IsDescendantOf would help if I could use to all values in the table at the same time

I want: If there is any of them (Part1, Part2 or Part3) the code will execute
I think your script i’ll use if on only 1 instance instead of all of them (Sorry if im wrong, but I want to understand it better if i’m wrong)

The function will return the first instance it finds in the table that is under workspace. If it doesn’t find any, it returns nil. You can change it to just return true and toss it into another if statement to do other things

For example, if you want to print out the table if anything inside of it is under workspace:

local function inWorkspace(t: {Instance}): boolean
	for _, v in t do
		if v:IsDescendantOf(workspace) then return true end
	end
	return false
end

local t = {workspace.Part1, workspace.Part2, workspace.Part3}
if inWorkspace(t) then
	print(t)
end

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