[Lua] Vdist2 between two coords gives a huge value

Hi, I’m using the Vdist2 native between two values; my pickup that appears when you kill a specific NPC and all the players in the world. What I’m trying to do is whichever player was closest to the pickup when it was picked up is theoretically the player carrying the pickup. This player will have a marker drawn on top of them and they need to deliver the pickup to a specific location. This is all fine except that the vdist2 native returns a stupidly high number for two coords that are so close together.

	while true do
	Citizen.Wait(0)
	MissionText("Collect the ~g~package.", 1000000)
	
		local minimum, maximum = GetModelDimensions(GetEntityModel(GetPickupObject(pickup1)))
	pickuppos = GetEntityCoords(GetPickupObject(pickup1))
	DrawMarker(20, pickuppos.x, pickuppos.y, pickuppos.z+maximum.z+0.5, 0.0, 0.0, 0.0, 0.0, 180.0, 0.0, 0.5, 0.5, 0.5, 0, 128, 0, 50, 1, 1, 2, 0, 0)	
		if HasPickupBeenCollected(pickup1) then
		
		
		for _, ped in ipairs(GetGamePool('CPed')) do -- Enumerate all peds
		if IsPedAPlayer(ped) and DoesEntityExist(ped) and not IsEntityDead(ped) then
		local pedpos = GetEntityCoords(ped)
		local distanceCheck = Vdist2(pickuppos.x, pickuppos.y, pickuppos.z, pedpos.x, pedpos.y, pedpos.z)
		Citizen.Trace(distanceCheck)
		        if distanceCheck <= 5.0 then -- Check if this player is closest when the pickup was collected
				Citizen.Trace("Found Player") -- 
            playerpickup = ped
				break -- end for loop once player found
		end
		end
		end
		
		
		RemoveBlip(blippickup)
		PlaySoundFrontend(a_0, "CHECKPOINT_NORMAL", "HUD_MINI_GAME_SOUNDSET", 1)
		ClearPrints()
		break
		end
	end

Video showcasing the problem (near the end)
Streamable

use #(vec1-vec2) instead

1 Like

Why not use lua to calculate the distances?
local distance = #(vector1 - vector2)

i assume that the vdist native is not the problem and your way to find a ped is wrong somewhere

Ok I’ll try this

1 Like

The value is smaller but still on the large side

	while true do
	Citizen.Wait(0)
	MissionText("Collect the ~g~package.", 1000000)
	
		local minimum, maximum = GetModelDimensions(GetEntityModel(GetPickupObject(pickup1)))
	pickuppos = GetEntityCoords(GetPickupObject(pickup1))
	DrawMarker(20, pickuppos.x, pickuppos.y, pickuppos.z+maximum.z+0.5, 0.0, 0.0, 0.0, 0.0, 180.0, 0.0, 0.5, 0.5, 0.5, 0, 128, 0, 50, 1, 1, 2, 0, 0)	
		if HasPickupBeenCollected(pickup1) then
		
		
		for _, ped in ipairs(GetGamePool('CPed')) do -- Enumerate all peds
		if IsPedAPlayer(ped) and DoesEntityExist(ped) and not IsEntityDead(ped) then
		local pedpos = GetEntityCoords(ped)
		local distanceCheck = #(pickuppos - pedpos)
		Citizen.Trace(distanceCheck)
		        if distanceCheck <= 5.0 then -- Check if this player is closest when the pickup was collected
				Citizen.Trace("Found Player") -- 
            playerpickup = ped
				break -- end for loop once player found
		end
		end
		end
		
		
		RemoveBlip(blippickup)
		PlaySoundFrontend(a_0, "CHECKPOINT_NORMAL", "HUD_MINI_GAME_SOUNDSET", 1)
		ClearPrints()
		break
		end
	end

Use print(pickuppos, pedPos) to display both values in the console, what are the outcomes?

Could try changing it to an int since it looks like a float.

Think I found the problem…seems like referring to the coordinates of the pickup after it has been collected gives it a null aka 0,0,0 value. I call the getentitycoords before the HasPickupBeenCollected native though so I dont understand why getentitycoords would give me a 0,0,0 value.

EDIT: Including the code that actually spawns the pickup itself

local deadped = GetSafePickupCoords(GetEntityCoords(boss1))
	pickuphash = GetHashKey("PICKUP_PORTABLE_PACKAGE")
	pickup1 = CreatePickupRotate(pickuphash, deadped.x, deadped.y, deadped.z, 0.0, 0.0, 0.0, 8, 1, 1, true, pickuphash)
blippickup = AddBlipForPickup(pickup1)
	SetBlipSprite(blippickup, 51)
	SetBlipColour(blippickup, 2)
	AddTextEntry('BLIPPICKUP', 'Package')
	BeginTextCommandSetBlipName('BLIPPICKUP')
	EndTextCommandSetBlipName(blippickup)	
SetPedAsNoLongerNeeded(boss1)

	while true do
	Citizen.Wait(0)
	MissionText("Collect the ~g~package.", 1000000)
	
		local minimum, maximum = GetModelDimensions(GetEntityModel(GetPickupObject(pickup1)))
	pickuppos = GetEntityCoords(GetPickupObject(pickup1))
	DrawMarker(20, pickuppos.x, pickuppos.y, pickuppos.z+maximum.z+0.5, 0.0, 0.0, 0.0, 0.0, 180.0, 0.0, 0.5, 0.5, 0.5, 115, 195, 108, 50, 1, 1, 2, 0, 0)	
		if HasPickupBeenCollected(pickup1) then
		
		
		for _, ped in ipairs(GetGamePool('CPed')) do -- Enumerate all peds
		if IsPedAPlayer(ped) and DoesEntityExist(ped) and not IsEntityDead(ped) then
		local pedpos = GetEntityCoords(ped)
		local distanceCheck = #(pickuppos - pedpos)
		print(pickuppos, pedpos)
		        if distanceCheck <= 5.0 then -- Check if this player is closest when the pickup was collected
				Citizen.Trace("Found Player") -- 
            playerpickup = ped
				break -- end for loop once player found
		end
		end
		end
		
		
		RemoveBlip(blippickup)
		PlaySoundFrontend(a_0, "CHECKPOINT_NORMAL", "HUD_MINI_GAME_SOUNDSET", 1)
		ClearPrints()
		break
		end
	end

SOLVED

Instead of getting the current position of the pickup, I simply used the spawn location of the pickup as these are one and the same. Should have thought of that first lol

deadped is the spawn location of the pickup when the ped (boss1) dies.

Code for ref:

deadped = GetSafePickupCoords(GetEntityCoords(boss1)) -- REMOVED LOCAL TO MAKE IT GLOBAL
	pickuphash = GetHashKey("PICKUP_PORTABLE_PACKAGE")
	pickup1 = CreatePickupRotate(pickuphash, deadped.x, deadped.y, deadped.z, 0.0, 0.0, 0.0, 8, 1, 1, true, pickuphash)
blippickup = AddBlipForPickup(pickup1)
	SetBlipSprite(blippickup, 51)
	SetBlipColour(blippickup, 2)
	AddTextEntry('BLIPPICKUP', 'Package')
	BeginTextCommandSetBlipName('BLIPPICKUP')
	EndTextCommandSetBlipName(blippickup)	
SetPedAsNoLongerNeeded(boss1)

	while true do
	Citizen.Wait(0)
	MissionText("Collect the ~g~package.", 1000000)
	
		local minimum, maximum = GetModelDimensions(GetEntityModel(GetPickupObject(pickup1)))
	pickuppos = GetEntityCoords(GetPickupObject(pickup1))
	DrawMarker(20, pickuppos.x, pickuppos.y, pickuppos.z+maximum.z+0.5, 0.0, 0.0, 0.0, 0.0, 180.0, 0.0, 0.5, 0.5, 0.5, 115, 195, 108, 50, 1, 1, 2, 0, 0)	
		if HasPickupBeenCollected(pickup1) then
		
		
		for _, ped in ipairs(GetGamePool('CPed')) do -- Enumerate all peds
		if IsPedAPlayer(ped) and DoesEntityExist(ped) and not IsEntityDead(ped) then
		local pedpos = GetEntityCoords(ped)
		local distanceCheck = #(deadped - pedpos)
		        if distanceCheck <= 5.0 then -- Check if this player is closest when the pickup was collected
				
            playerpickup = ped
				break -- end for loop once player found
		end
		end
		end
		
		
		RemoveBlip(blippickup)
		PlaySoundFrontend(a_0, "CHECKPOINT_NORMAL", "HUD_MINI_GAME_SOUNDSET", 1)
		ClearPrints()
		break
		end
	end

Also should mention… Vdist2 returns the distance before square root; so Vdist is what you wanted.