How to check if object is in array, using name of object

so i got array using :GetTouchingParts() and i want to check if there are part called “Wall”. How do i do that?

2 Likes

Here’s an example.

local GetTocuhingParts = (object):GetTocuhingParts()

for _, TocuhingPart in ipairs(GetTouchingParts)
{
       if (TocuhingPart.Name == "Wall") then
       {
                print("Found Wall!!")
       }
}
1 Like

curly braces are not valid syntax in lua

1 Like

Thanks you so much mister chatgpt🤓

Well, you can try this:

local touchingParts = object:GetTouchingParts()
if table.find(touchingParts, "Wall") then
    print("Wall found!")
end

I tried that, but that not worked, because “Wall” is a string, not object, thats the problem

Oh, I see.
Try this:

local touchingParts = {}
for _, part in pairs(object:GetTouchingParts()) do
    table.insert(touchingParts, v.Name)
end

if table.find(touchingParts, "Wall") then
    print("Wall found!")
end
1 Like

The phrase (object) was created to indicate that it is an object. object is Instance.

They mean that all the { and } you were using wouldn’t run in Lua. Lua uses an end to signal the end of a statement, unlike other languages which use that curly bracket method.

Thanks, that worked! Tell me if you have more optimised way

2 Likes

That works, but it spends unnecessary more memory for creating and filling another table. It also performs two iterations. The only thing Ramsa_Ajeossi’s response is missing is correct syntax, the correct logic is in place.

for _,part in object:GetTouchingParts() do
	if part.Name == "Wall" then
		print("Found a wall")
	end
end
2 Likes

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