GetDistanceBetweenCoords don't work

Im using ESX_rpchat and if someone sends /me (or something) and I’m within 20 meters, I see the message. If I’m within about 50 meters, I don’t see the message. If I’m about 200 meters (estimated) I see the message again.

elseif GetDistanceBetweenCoords(sourceCoords.x, sourceCoords.y, sourceCoords.z, targetCoords.x, targetCoords.y, targetCoords.z, true) < 20 then..... If I’m roughly 200 meters or more away, sourceCoords (sender) has the same coordinates as me. Whats wrong?

CLIENT
RegisterNetEvent('esx_rpchat:sendDo')
AddEventHandler('esx_rpchat:sendDo', function(playerId, title, message, color)
	local source = PlayerId()
	local target = GetPlayerFromServerId(playerId)

	local sourcePed, targetPed = PlayerPedId(), GetPlayerPed(target)
	local sourceCoords, targetCoords = GetEntityCoords(sourcePed), GetEntityCoords(targetPed)

	if target == source then
		TriggerEvent('chat:addMessage', {
      template = '<div style="padding: 0.45vw; margin: 0.05vw; background-color: rgba(220, 173, 17, 0.6); border-radius: 10px;"><i class="fas fa-users"></i> {1}(({0}))</div>',
        args = { title, message }
    })
	elseif GetDistanceBetweenCoords(sourceCoords.x, sourceCoords.y, sourceCoords.z, targetCoords.x, targetCoords.y, targetCoords.z, true) < 20 then
    TriggerEvent('chat:addMessage', {
    template = '<div style="padding: 0.45vw; margin: 0.05vw; background-color: rgba(220, 173, 17, 0.6); border-radius: 10px;"><i class="fas fa-user-street-view"></i> {1}(({0}))</div>',
        args = { title, message }
    })
	end
end)

SERVER
RegisterCommand('do', function(source, args, rawCommand)
  if source == 0 then
    print('esx_rpchat: you can\'t use this command from rcon!')
    return
  end

  args = table.concat(args, ' ')
  local name = GetPlayerName(source)
  xPlayer = ESX.GetPlayerFromId(source)
  if Config.EnableESXIdentity then name = GetCharacterName(source) end

  TriggerClientEvent('esx_rpchat:sendDo', -1, source, xPlayer.getName(), args, { 255, 198, 0 })
end)

Don’t use GetDistanceBetweenCoords anymore.

use math

like this:

#(vector3(sourceCoords.x, sourceCoords.y, sourceCoords.z) - vector3(targetCoords.x, targetCoords.y, targetCoords.z))

No change, still the same problem

local dist = #(vector3(sourceCoords.x, sourceCoords.y, sourceCoords.z) - vector3(targetCoords.x, targetCoords.y, targetCoords.z))
if dist <= 20 then...

Here try this, I didn’t test this but this should work.

Also: Always use #(coord1 - coord2) instead of the native, it is faster & more optimized, also you could simply loop through the players on the server, get the players in distance of the player then return that table. That may be a bit complex if you don’t understand lua, so if you would like an example let me know.

RegisterNetEvent('esx_rpchat:sendDo')
AddEventHandler('esx_rpchat:sendDo', function(playerId, title, message, color)
	local source = PlayerId()
	local target = GetPlayerFromServerId(playerId)

	local sourcePed, targetPed = PlayerPedId(), GetPlayerPed(target)
	local sourceCoords, targetCoords = GetEntityCoords(sourcePed), GetEntityCoords(targetPed)

	if target == source then
		return TriggerEvent('chat:addMessage', {
        template = '<div style="padding: 0.45vw; margin: 0.05vw; background-color: rgba(220, 173, 17, 0.6); border-radius: 10px;"><i class="fas fa-users"></i> {1}(({0}))</div>',
        args = { title, message }
        })
	end

    local coords = {p = vec3(sourceCoords.xyz), t = vec3(targetCoords.xyz)}
    local dist = #(coords.p - coords.t)

    if (dist < 20) then
        TriggerEvent('chat:addMessage', {
            template = '<div style="padding: 0.45vw; margin: 0.05vw; background-color: rgba(220, 173, 17, 0.6); border-radius: 10px;"><i class="fas fa-user-street-view"></i> {1}(({0}))</div>',
            args = { title, message }
        })
    end
end)

It’s because of onesync, you need to ensure that your target is not -1