Thanks! That code snippet at the bottom helped, here is the conversion to lua which I used:

[lua]

local math = math

local abs = math.abs

local SMALL_NUM = 0.00000001

local function dist3D_Segment_to_Segment(segment1P1, segment1P2, segment2P1, segment2P2)

local u = segment1P2 - segment1P1

local v = segment2P2 - segment2P1

local w = segment1P1 - segment2P1

local a = u:Dot(u) // always >= 0

local b = u:Dot(v)

local c = v:Dot(v) // always >= 0

local d = u:Dot(w)

local e = v:Dot(w)

local D = a*c - b*b // always >= 0

local sc,sN,sD = D, D, D, D // sc = sN / sD, default sD = D >= 0

local tc,tN,tD = D, D, D, D // tc = tN / tD, default tD = D >= 0

```
// compute the line parameters of the two closest points
if (D < SMALL_NUM) then // the lines are almost parallel
sN = 0.0 // force using point P0 on segment S1
sD = 1.0 // to prevent possible division by 0.0 later
tN = e
tD = c
else // get the closest points on the infinite lines
sN = (b*e - c*d)
tN = (a*e - b*d)
if (sN < 0.0) then // sc < 0 => the s=0 edge is visible
sN = 0.0
tN = e
tD = c
elseif (sN > sD) then // sc > 1 => the s=1 edge is visible
sN = sD
tN = e + b
tD = c
end
end
if (tN < 0) then // tc < 0 => the t=0 edge is visible
tN = 0
// recompute sc for this edge
if (-d < 0) then
sN = 0
elseif (-d > a) then
sN = sD
else
sN = -d
sD = a
end
elseif (tN > tD) then // tc > 1 => the t=1 edge is visible
tN = tD
// recompute sc for this edge
if ((-d + b) < 0) then
sN = 0
elseif ((-d + b) > a) then
sN = sD
else
sN = (-d + b)
sD = a
end
end
// finally do the division to get sc and tc
sc = ((abs(sN) < SMALL_NUM) and 0) or (sN/sD)
tc = ((abs(tN) < SMALL_NUM) and 0) or (tN/tD)
// get the difference of the two closest points
local dP = w + (sc * u) - (tc * v) // = S1(sc) - S2(tc)
return dP:Length() // return the closest distance
```

end

[/lua]