(Solved) Limit command to location?

How would I limit a command to an X,Y,Z location? Like /buy can only be used in ammunitions.

I would say set the xyz where you want it. Then get the distance from the player to the xyz pos. If the player is <= 10.0 then

–put you code in here… maybe idk I’m not at my pc so it’s hard to show on my phone.

maybe… like this, idk :slight_smile:

MY_PED = GetPlayerPed(-1)
AREA = {}
AREA.x1 = 1700.0 -- max
AREA.y1 = 500.0 -- max
AREA.z1 = 40.0 -- max
AREA.x2 = 1500.0 -- min
AREA.y2 = 400.0 -- min
AREA.z2 = 20.0 -- min

RegisterNetEvent("BuyAmmo")
AddEventHandler("BuyAmmo", function()
	if IsEntityInArea(MY_PED,  AREA.x1,  AREA.y1,  AREA.z1,  AREA.x2,  AREA.y2,  AREA.z2) then
		--code
	else
		--code
	end
end)

I have never used the IsEntityInArea but I would do it this way so you can modify the distance allowed.

local ply = GetPlayerPed(-1)

local coords = {
	Coord_X = 1700.0,
	Coord_Y = 1700.0,
	Coord_Z = 40.0
}

RegisterNetEvent("purchaseeAmmo")
AddEventHandler("purchaseAmmo", function()
	local plyCoords = GetEntityCoords(ply, )
	local shopDistance = GetDistanceBetweenCoords(coords.Coord_X, coords.Coord_Y, coords.Coord_Z,  plyCoords["x"], plyCoords["y"], plyCoords["z"], true)

	if shopDistance <= 10.0 then
		-- Alow to buy or use UI
	else
		-- Maybe pass a message here yo remove the else to return nothing --
	end
end)

NOTE: THIS IS NOT TESTED JUST AN EXAMPLE

1 Like