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

Searching for closest attachment doesn’t work for the first attachment but it does for second?

Hello,

I am trying to make a wiring system and I’ve encountered a problem.

When you click on the ground or somewhere a Attachment gets created in a Part in a folder called WireSystem.

I want the attachment to automatically search for closest Attachment to position to. It works for the second attachment but not the first attachment.

Code:

if Attachment1 == nil and Attachment2 == nil and Target ~= nil then
    Hit, Target, _ =
        Mouse:ProjectMouseRay({localPlayer.Character, Attachment1, Attachment2, rodConstraint, PartConnector})
    PartConnector = Instance.new("Part", workspace.WireSystem)
    PartConnector.Name = "Wire"
    PartConnector.Size = Vector3.new(0.5, .5, .5)
    PartConnector.Transparency = 1
    PartConnector.CanCollide = false
    PartConnector.Anchored = true
    PartConnector.CFrame = Hit

    local newAttachment = Instance.new("Attachment", PartConnector)
    newAttachment.Name = "First"
    newAttachment.Visible = true

    closestAttachment = nil
    for _, wires in pairs(workspace.WireSystem:GetChildren()) do -- This is the for loop that checks for closest attachment
        for _, attachment in pairs(wires:GetChildren()) do
            if not attachment:IsA("RodConstraint") and attachment:IsA("Attachment") then
                local magnitude = (attachment.WorldPosition - Hit.p).magnitude
                if magnitude <= 1 then
                    closestAttachment = attachment
                    break
                end
            end
        end
    end

    if closestAttachment ~= nil then
        newAttachment.WorldPosition = closestAttachment.WorldPosition
    elseif closestAttachment == nil then
        newAttachment.WorldPosition = Hit.p
    end

    Attachment1 = newAttachment
    closestAttachment = nil

    return true
end

It is the same for the second attachment too.

But it only works for the second attachment.

Thanks in advance.

1 answer

I THINK that it is Attachment0 and attachment1, not attachment 2.

1 comment

0

Attachment1 and 2 aint the ones, they are just variables. It would've errored; And I would have said that in the Original Post

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