I have a pivot issue

So I got this code here and in studio it looks fine but when I go into testing the unionWire moves and goes funny.

I’m not what in my code in my code I forgotten. Any suggestion will be really helpful! :slight_smile:


Before


After


function createRegularWire()
    if not mouse.Target then
        return
    end

    local targetParentName = mouse.Target.Parent.Name
    local regularPoleName = targetParentName:gsub("Ground$", "")

    if not draggablePoleInfo[regularPoleName] then
        return
    end

    local currentWirePt = wirePts[1] and 2 or 1
    wirePts[currentWirePt] = mouse.Target.Parent

    createSelectionBox(mouse.Target.Parent)

    if wirePts[1] and wirePts[2] then
        local connectorCount = draggablePoleInfo[regularPoleName]
        local parent = wirePts[1]
        local wiresagFolder = Instance.new("Model", parent)
        wiresagFolder.Name = "WireSag"

        local tempFolder = Instance.new("Folder", game.Workspace)
        tempFolder.Name = "TempUnionFolder"

        local allSegments = {}

        for i = 1, connectorCount do
            local connector1 = wirePts[1]:FindFirstChild(`Connector{i}`)
            local connector2 = wirePts[2]:FindFirstChild(`Connector{i}`)

            if connector1 and connector2 then
                local P1 = connector1.Position
                local P2 = connector2.Position
                local V = P2 - P1
                local L = V.Magnitude
                if L == 0 then
                    continue
                end
                local num_segments = 25
                local sag = L * 0.01
                local a = 4 * sag / (L * L)
                local thickness = targetParentName:find("HiTensionPole") and 0.15 or 0.05

                for seg = 1, num_segments do
                    local t1 = (seg - 1) / num_segments
                    local t2 = seg / num_segments
                    local x1 = t1 * L
                    local x2 = t2 * L
                    local dip1 = a * x1 * (L - x1)
                    local dip2 = a * x2 * (L - x2)
                    local pos1 = P1 + t1 * V - Vector3.new(0, dip1, 0)
                    local pos2 = P1 + t2 * V - Vector3.new(0, dip2, 0)
                    local segV = pos2 - pos1
                    local segL = segV.Magnitude
                    local mid = (pos1 + pos2) / 2
                    local look = segV.Unit
                    local perp = look:Cross(Vector3.new(0, 1, 0))
                    if perp.Magnitude < 0.001 then
                        perp = look:Cross(Vector3.new(0, 0, 1))
                    end
                    perp = perp.Unit
                    local up = perp:Cross(look).Unit

                    local wireSeg = Instance.new("Part")
                    wireSeg.Shape = Enum.PartType.Cylinder
                    wireSeg.Material = Enum.Material.SmoothPlastic
                    wireSeg.Size = Vector3.new(segL, thickness, thickness)
                    wireSeg.CFrame = CFrame.fromMatrix(mid, look, perp, up)
                    wireSeg.Color = Color3.new(0, 0, 0)
                    wireSeg.Anchored = true
                    wireSeg.CanCollide = false
                    wireSeg.Parent = tempFolder
                    table.insert(allSegments, wireSeg)
                end
            end
        end

        if #allSegments > 0 then
            local baseSegment = allSegments[1]
            local remainingSegments = {}
            for j = 2, #allSegments do
                table.insert(remainingSegments, allSegments[j])
            end

            local unionedWire = baseSegment:UnionAsync(remainingSegments)
            if unionedWire then
                unionedWire.Name = "Union"
                unionedWire.Material = Enum.Material.SmoothPlastic
                unionedWire.Color = Color3.new(0, 0, 0)
                unionedWire.Anchored = true
                unionedWire.CanCollide = false
                unionedWire.Parent = wiresagFolder
            else
                warn("Union failed for all wires")
                for _, seg in ipairs(allSegments) do
                    seg.Parent = wiresagFolder
                end
            end

            for _, seg in ipairs(allSegments) do
                seg:Destroy()
            end
            tempFolder:Destroy()
        end

        resetWirePointsAndSelectionBoxes()
    end
end
1 Like

I fixed the code by adjusting the CFrame position.

1 Like

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