Get Distance between players

Greetings All,

I’ve been working on a function which is players giving each other money when they’re near, but I can’t seem to get it right,
here is my code:
Server Side

TriggerClientEvent('givemoney', playerID)

Client Side


RegisterNetEvent('givemoney') 

AddEventHandler('givemoney', function()

TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0}, PlayerPedId())

--local player = GetPlayerFromServerId(PlayerId())

local ped = GetPlayerPed(-1) <---- From

local ped2 = PlayerPedId() <----- To

local playerCoords = GetEntityCoords(ped)

local playerCoords2 = GetEntityCoords(ped2)

if( GetDistanceBetweenCoords( playerCoords, playerCoords2["x"], playerCoords2["y"], playerCoords2["z"] ) < 10)then
TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0}, "1")
else
TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0}, "2")
end
end)

this code always return “2” when I tried to give money to someone, could someone help me please? Thank you!

if GetDistanceBetweenCoords(playerCoords.x, playerCoords.y, playerCoords.z, playerCoords2.x, playerCoords2.y, playerCoords2.z, 1) < 10.0

1 Like

Try this instead…

if(Vdist(playerCoords.x, playerCoords.y, playerCoords.z, playerCoords2.x, playerCoords2.y, playerCoords2.z) < 10) then
      TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0}, "1")
else
      TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0}, "2")
end

The actual issue is because you did not specify the x/y/z in your GetDistanceBetweenCoords() with playerCoords. Vdist is just another way to get the distance.

FYI GetDistanceBetweenCoords() with the last flag as 1 is the same as Vdist. Not sure how this might impact performance.

Thank you guys so much, but I’ve encountered a new problem, the code now always return “1”, no matter how far me and the other player are, is there anything wrong with passing the PlayerID from the code? Thank you!

Here is the full Server code

AddEventHandler('chatMessage', function(source, n, message) 
  local args = stringsplit(message, " ") 

  if (args[1] == "/givemoney") then 
    CancelEvent() 

    if (args[2] ~= nil) then 
      local playerID = tonumber(args[2]) 
      end
      TriggerClientEvent('givemoney', playerID)
end
end)

Clien Code now

RegisterNetEvent('givemoney') 

AddEventHandler('givemoney', function()

TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0}, PlayerPedId())

--local player = GetPlayerFromServerId(PlayerId())

local ped = GetPlayerPed(-1) <---- From

local ped2 = PlayerPedId() <----- To

local playerCoords = GetEntityCoords(ped)

local playerCoords2 = GetEntityCoords(ped2)

if(Vdist( playerCoords.x, playerCoords.y, playerCoords.z, playerCoords2.x, playerCoords2.y, playerCoords2.z) < 10.0)then
TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0}, "1")
else
TriggerEvent('chatMessage', 'SYSTEM', {255, 0, 0}, "2")
end
end)
2 Likes

You forgot to pass the argument in AddEventHandler, right now you’re checking the distance between the player and themselves.

1 Like

Thank you @Scott, @Apocalyptikz for your helps! I got the distance working!

@tantalum Thank you for pointing out the problem man! got it working now! I am so stupid lol, I didn’t notice that, big big thanks to you all!

What do you mean by passing the argument in AddEventHandler ? Cause I want to get the distance for a cuff system :slight_smile:

Thanks ! :slight_smile:

Here is the code :

– client side handcuff script

local handCuffed = false – store wether we are handcuffed or not

RegisterNetEvent(‘mHandCuff’) – register that this is a valid event

– register a function to be called when we recieve the event
AddEventHandler(‘mHandCuff’, function(PlayerPedId)
local ped = GetPlayerPed(-1) ---- From
local ped2 = PlayerPedId() ----- To
local playerCoords = GetEntityCoords(ped)
local playerCoords2 = GetEntityCoords(ped2)

if(Vdist( playerCoords.x, playerCoords.y, playerCoords.z, playerCoords2.x, playerCoords2.y, playerCoords2.z) < 2.0)then
– set handCuffed equal to the oposite of its current value (true or false)
handCuffed = not handCuffed
end
end)

Citizen.CreateThread(function() – create a thread
while true do – loop through this code infinitely
Citizen.Wait(1) – required in an infinite loop or it will crash

if (handCuffed == true) then -- we are handcuffed
  RequestAnimDict('mp_arresting') -- tell the game to load this animation dictionary
  -- check if the animation dictionary has loaded, if not, wait
  while not HasAnimDictLoaded('mp_arresting') do
    Citizen.Wait(0)
  end
  local myPed = PlayerPedId() -- get our ped identifier
  local animation = 'idle' -- animation to play
  local flags = 49 -- only play the animation on the upper body
  -- play the animation
  TaskPlayAnim(myPed, 'mp_arresting', animation, 8.0, -8, -1, flags, 0, 0, 0, 0)
end

end
end)

1 Like

What did you do finally ? :slight_smile: Need that distance thingy :slight_smile:

did you find the solution ?

No, noone answered me…

Hey guys, does Vdist and GetDistanceBetweenCoords works on server side ?

Use #(vector1 - vector2) https://docs.fivem.net/docs/scripting-reference/runtimes/lua/functions/vector3/ it’s faster and works on both client and server. for next time don’t bump a 4 year old topic.