Manage distance between

Hello ,

I have a problem I create a menu but I would like that when the distance between the player and the coordinates that I defined are greater than 2 the menu closes. Currently I realize this small code but the menu closes instantly because it is at two different coordinates.

if IsPlayerWantedLevelGreater(GetPlayerIndex(),0) == false and IsPedInAnyVehicle(LocalPed(), true) == false and GetDistanceBetweenCoords(coffeeshopcatalogue[1], coffeeshopcatalogue[2], coffeeshopcatalogue[3],GetEntityCoords(LocalPed())) < 2 then
	drawTxt('Press E to buy',0,1,0.5,0.8,0.6,255,255,255,255)
	if(IsControlPressed(1, 38)) then
	Citizen.Wait(100)
		coffeeshop.currentmenu = "main"
		coffeeshop.opened = true
		coffeeshop.selectedbutton = 0
	end
elseif GetDistanceBetweenCoords(coffeeshopcatalogue[1], coffeeshopcatalogue[2], coffeeshopcatalogue[3],GetEntityCoords(LocalPed())) > 2 then
	CloseMenu()
end

If you know how to fix this problem it would be super nice to help me

Regards,
Proxene

if condition1 and condition2 and (coordsdistance < 2 or coordsdistance2 < 2) then code code code elseif coordsdistance >= 2 and coordsdistance2 >= 2 then close menu end

Super simple outline of how you can do it. Just check if the player is far away from both coordinates at the same time. Don’t use two separate pieces of code for each like I assume you are doing now.

Ok thank you for your answer but I think the problem with my code comes from

for i = 1, #coffeeshop_locations do
			coffeeshopcatalogue = coffeeshop_locations[i]

Because in the first shop I can not open my menu by cons in the second shop which is the last of the list, everything is ok. The problem would arise

GetDistanceBetweenCoords (coffeeshopcatalogue[1], coffeeshopcatalogue[2], coffeeshopcatalogue[3], GetEntityCoords (LocalPed ()))

Use the coordinates of the last shop as reference and not shop 1 and 2 at the same time.

You’re using the functions as follows:

float GetDistanceBetweenCoords( float, float, float, vector );

There exists no such declaration. The proper (and only) declaration of this function is:

float GetDistanceBetweenCoords( float, float, float, float, float, float, bool );

So you need to pass the function the x, y and z coordinates separately for both points. The last bool is whether or not you want to take height in to account. Setting this to false will make the calculation slightly faster but definitely set this to true if there’s something above or below the waypoint (or likely for people to fly over).

Okey, thank you for your advice :slight_smile: ,
I patch my code but the problem persists :confused:

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(0)
		for i = 1, #coffeeshop_locations do
			coffeeshopcatalogue = coffeeshop_locations[i]
			
			local pos = GetEntityCoords(GetPlayerPed(-1))
			local distance = GetDistanceBetweenCoords(pos.x, pos.y, pos.z, coffeeshopcatalogue[1], coffeeshopcatalogue[2], coffeeshopcatalogue[3], true)
			
			if IsPlayerWantedLevelGreater(GetPlayerIndex(),0) == false and IsPedInAnyVehicle(LocalPed(), true) == false and distance < 20 then
				DrawMarker(1,coffeeshopcatalogue[1], coffeeshopcatalogue[2], coffeeshopcatalogue[3],0,0,0,0,0,0,2.001,2.0001,0.4001,0,155,255,200,0,0,0,0)
			end
			
			if IsPlayerWantedLevelGreater(GetPlayerIndex(),0) == false and IsPedInAnyVehicle(LocalPed(), true) == false and distance < 2 then
				drawTxt('Press E to buy',0,1,0.5,0.8,0.6,255,255,255,255)
				if(IsControlPressed(1, 38)) then
					Citizen.Wait(100)
					coffeeshop.currentmenu = "main"
					coffeeshop.opened = true
					coffeeshop.selectedbutton = 0	
				end
			end
			
			if distance > 2 then
				CloseMenu()
			end
			
		end
	end
end)

An idea to resolve my problem ?