Distance between coords on server side

Hey guys,

Does GetDistanceBetweenCoords and Vdist functions work on server side-- if not, how can i get this distance on server side? I’m using javascript

Thanks a lot !

1 Like

Use Lua vector math instead for both the client and server as it’s a lot faster then native calls.

-- language faster equivalent:
local firstVec = vector3(0.0, 0.0, 0.0)
local secondVec = vector3(5.0, 5.0, 5.0)

local dist = #(firstVec - secondVec) -- Use Z
local dist = #(firstVec.xy - secondVec.xy) -- Do not use Z
1 Like

Thanks for the response, i saw that explanation on the Docs page but i was hopeful to hear that those functions works on both sides, what i ended doing is created and exported a helper.lua class.
Then on the javascript side i just call this exported function…

Example:

Lua class helper.lua

local DistanceBetweenCoords = function(coordsA, coordsB, useZ)
    -- language faster equivalent:
    local firstVec = vector3(coordsA.x, coordsA.y, coordsA.z)
    local secondVec = vector3(coordsB.x, coordsB.y, coordsB.z)
    if useZ then
        return #(firstVec - secondVec)
    else 
        return #(firstVec.xy - secondVec.xy)
    end
end

exports('DistanceBetweenCoords', DistanceBetweenCoords )

resource manifest:

server_exports {
    ...... ,
    'DistanceBetweenCoords'
}

Then in javascript file:

global.exports.resourceName.DistanceBetweenCoords( coordsA, coordsB, true )

This way works as expected.