How to get attachment at some position?

Hi,
I have problem, that i need script, that return attachment on given vector 3. How to do it?

Please be more specific, are you looking to find the Vector3 position of the attachment within the world?

I’m unsure what you’re trying to achieve based on information received. Try to include snippets of code you’ve attempted, perhaps it’ll help me understand what you need.

I am trying to find all attachments on given vector3.

Well, you would achieve this by looping through all objects that may contain attachments, and then checking each attachments position, and comparing it with the Vector3 you’re searching for.

Example:

local PartsWithAttachment = workspace:WaitForChild("FolderWithParts")

local function FindAttachmentsOnVector3(Vec3)
    local List = {}
    
    for _,v in pairs(PartsWithAttachment:GetChildren()) do
        local Attachment = v:FindFirstChildOfClass("Attachment")
        if Attachment and Attachment.Position == Vec3 then
            table.insert(List, Attachment)
        end
    end

    return List
end

However, Vector3’s can be 1.001, 2.002, 3.333 for example, so checking within a specific area might be of use. Say you are 0.001 studs off with your given Vector3, it won’t find the attachment with my example code. You can fix this with Region3 checking (getting all parts within a region), or by a simple magnitude check.

If each item can contain more than one attachment, or attachments are in more than one area of the game, a recursive method (or :GetDescendants() rather than :GetChildren()) would be better, but more taxing performance wise and shouldn’t be performed as often.

Let me know if you need more assistance. Knowing the structure of your parts and attachments within Workspace could help me write code more oriented to your use case.

1 Like

I am using it for grid snaping building system, so its ok without tolerance.

As long as you’re using grid snapping with both the parts, and the attachments themselves, and the Vector3s are just even numbers without decimals, then you’re all good there.

You should be able to modify the example code I gave to suite your needs, if you need any specific help, just let me know!

1 Like