Get distance to a prop

I’ve got an idea I want to play around with that would involve getting within a radius of a certain prop on the map and having is do whatever it is i tell it to at that point(in my case a gas pump)

Is there a way to go about getting the distance to a prop? For example what im wanting to do is if you are doing the smoking emote near a gas pump you will have a chance of it blowing up.

Any help is welcomed. I’ve searched the forums but haven’t found a solution.

Pretty much @xander1998 's work just a bit changed.

local table = { -- Gas stations
	{x = 1,y = 1,z = 1},
	{x = 2,y = 2,z = 2},
	{x = 3,y = 3,z = 3}
}

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(0)

		for k in pairs(table) do

			local plyCoords = GetEntityCoords(GetPlayerPed(-1), false)
			local dis = Vdist(plyCoords.x, plyCoords.y, plyCoords.z, table[k].x, table[k].y, table[k].z)

			if dist <= 1.2 then
				if CODE FOR EMOTE BEING PLAYED? -- e.g if beingPlayed('smoking') then
					DO SOMETHING -- Math.random for random chance of gas station blowing up
				end
			end
		end
	end
end)
2 Likes

Ahh ok looks like I’m going to have to add all the locations. I wasn’t sure if what I was looking for was possible it’s probably not.

Thanks for this!

1 Like

No problem, if you look at a fuel script they probably have all the locations in there somewhere.

1 Like

So i guess im still a big noob… Do you know how i would go about seeing if a certain animation os being played?. Lol.

Ummm. Not sure, but I am at school right now lol. I will doublce check though.

1 Like

You could integrate it into the emote script itself, so if they press the button or do the command for smoking. It does this:

for k in pairs(table) do

	local plyCoords = GetEntityCoords(GetPlayerPed(-1), false)
	local dis = Vdist(plyCoords.x, plyCoords.y, plyCoords.z, table[k].x, table[k].y, table[k].z)

	if dist <= 1.2 then
		do the explosion here
	end
1 Like

You could always do big loops with many if’s without adding all the prop coordinates, but that would mean a bigger impact on the server’s resources. Try to find a good way of adding the prop coordinates so it would not make you a hassle doing it. When you find one, you save up server resources and can easier manage things.

2 Likes

So i realized that the smoking animation is actually a scenario.

Now i’ve got this native “IsPedUsingScenario” but i cant get it to work how i want.

Now ive put aside the whole it going off near the Gas station locations but im focusing on getting the server to do something (Trigger an Event in my case right now). This is where im at and its not working.

> Citizen.CreateThread(function() --This triggers a Server Event, when you press the specified Key
> 	while true do
> 		Citizen.Wait(0)
> 		if IsPedUsingScenario(ped, "WORLD_HUMAN_SMOKING") then
> 			TriggerServerEvent("FIRE", PlayerId())
> 		end
> 	end
> end)

Edit: I really do not want it to be apart of the emote script itself either if possible and im fine with using the locations in an x. y .z table. I just want to be able to get it to do SOMETHING when the ped is using a scenario.

Show the server side code. The event that you registered on the server side.

1 Like

The server side code shouldnt matter i am using code from a firescript i found in the forums which had it working through a key press. That worked.

So i must be using the IsPedUsingScenario wrong.
But here anyways.

RegisterServerEvent("FIRE") --This registers the Server Event, so the client can trigger it

AddEventHandler("FIRE", function(player) --This is the actual Event
	TriggerClientEvent("FireForAll", -1, player) --This triggers a Event on every client, so the fire is visible for everyone.
end)

The reason for the server part is for it to sync with other players in the server

Well if you just copied code from somewhere and did not predefine ped variable, then of course it will not work.

Use PlayerId(), just like in the TriggerServerEvent function.

EDIT: Sorry, not PlayerId(), but GetPlayerPed(-1).

1 Like

oh my gosh. Im too tried for this lol. Thank you that worked perfectly

Apparently me too…

Been awake since 5 AM…

1 Like

Soooo im stuck again lol… There is now something wrong with getting the distance between the ped and the gastation.

local table = { -- Gas stations
	{x = 1183, y = -336, z = 69}
}

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(0)

		for k in pairs(table) do

			local plyCoords = GetEntityCoords(GetPlayerPed(-1), false)
			local dist = Vdist(plyCoords.x, plyCoords.y, plyCoords.z, table[k].x, table[k].y, table[k].z)
			local ped = GetPlayerPed(-1)

			if dist < 1000000000 then
				if IsPedUsingScenario(ped, "WORLD_HUMAN_SMOKING") then -- e.g if beingPlayed('smoking') then
					TriggerServerEvent("FIRE", PlayerId()) -- Math.random for random chance of gas station blowing u
				end
			end
		end
	end
end)
RegisterNetEvent("FireForAll") --This registers the Client Event, so the server can trigger it
AddEventHandler("FireForAll", function(player) --This is the actual Event
	local playerPos = GetEntityCoords(GetPlayerPed(player))
	StartScriptFire (playerPos.x, playerPos.y, playerPos.z, 25, true)
end)

This is what ive got so far. If got it working when you go so smoke without the if dist < 1000000000 then part in it. But when i got to put that part back in it just doesn work. And im standing at the exact location of the one Gas station xyz.

EDIT

Also the if dis < 1000000000 then part i have been testing that seeing if that was the problem with no luck.

local table = { -- Gas stations
	{x = 1183, y = -336, z = 69}
}

Citizen.CreateThread(function()
	while true do
		Citizen.Wait(0)

		for k in pairs(table) do

			local plyCoords = GetEntityCoords(GetPlayerPed(-1), false)
			local dist = Vdist(plyCoords.x, plyCoords.y, plyCoords.z, table[k].x, table[k].y, table[k].z)
			local ped = GetPlayerPed(-1)

			if dist <= 1000000000 and IsPedUsingScenario(ped, "WORLD_HUMAN_SMOKING") then
				TriggerServerEvent("FIRE", PlayerId()) -- Math.random for random chance of gas station blowing u
			end
		end
	end
end)
RegisterNetEvent("FireForAll") --This registers the Client Event, so the server can trigger it
AddEventHandler("FireForAll", function(player) --This is the actual Event
	local playerPos = GetEntityCoords(GetPlayerPed(player))
	StartScriptFire (playerPos.x, playerPos.y, playerPos.z, 25, true)
end)
1 Like

Hmm still not working. Thanks for the help though im going to just put it aside for now it will probably be much easier to figure out when im not dead tired…

1 Like

@Scott_UK That’s pretty much the same code. You just made the checks in the same block/line and not in separate ones.

Vector3 GET_ENTITY_COORDS(Entity entity, BOOL alive);
p1 = !IS_ENTITY_DEAD

local plyCoords = GetEntityCoords(GetPlayerPed(-1), false)

The last parameter in GetEntityCoords as you can see has something to do with the entity being alive. Since your code has false, it might be that the GetEntityCoords function might only get the entity coords according if the entity either is alive or not. In your case, the function will probably get coordinates only if the entity is dead. You would need to change it to true so the function would get an entity’s coordinates if it’s alive. But someone might put a cigarette in your mouth and light it up even when you are dead, so… Hehehe. You can just not use the last parameter.

			local ped = GetPlayerPed(-1)
			local plyCoords = GetEntityCoords(ped)
			local dist = Vdist(plyCoords.x, plyCoords.y, plyCoords.z, table[k].x, table[k].y, table[k].z)

So i tried that too and tried some other stuff… But no success. Makes no sense to my WHY it wont listen to the distance… Must not be destiny. :stuck_out_tongue:

I think I am convinced that the Vdist function is not working as it supposed to. Or it should not work in this situation for some reason… Because you said yourself that when you remove the Vdist function, the code works just fine. There is another Vdist function called Vdist2.
https://runtime.fivem.net/doc/reference.html#_0x2A488C176D52CCA5
So try renaming Vdist function to Vdist2 and see what happens. If nothing works, try out another function - GetDistanceBetweenCoords.
https://runtime.fivem.net/doc/reference.html#_0xF1B760881820C952