Check if a vehicle is in an area

Hello everybody,
For my future server, I need to check if a vehicle is present in a given position before I can spawn a new one (the positions correspond to parking spaces).
After doing some research on this topic, I found a few proposals like GetClosestVehicle, IsAreaOccupied or isPositionOccupied but none of them work.

I have also heard of Raycast but have no idea how to use it at all and am not even sure if it can be useful for me.

Could a good person help me on this?

Thanks in advance,

If you got the time. You could use a polyzone per parking spot and have a list of them and if they contain an entity or vehicle prior to store or save.

Could you elaborate.

A Ray Cast (a.k.a. a Shape Test) is basically shooting a laser from point A to point B, and then it telling you what it hit on the way there; in your case, you would want it to know if it hit any cars. Whether or not it is appropriate for your use case depends on your code. Can you show some?

                for o = 1, #POLICE_STATIONS.parking_slot_general do
                    -- It's here that i want to test if a car is on the parking slot or not.
                    if ParkingSlotIsEmpty then
                        -- Get the nearest parking slot available
                        if GetDistanceBetweenCoords(POLICE_STATIONS.parking_slot_general[o][1],POLICE_STATIONS.parking_slot_general[o][2],POLICE_STATIONS.parking_slot_general[o][3],xA,yA,zA,false) < nearestDistance then
                            nearestDistance = GetDistanceBetweenCoords(POLICE_STATIONS.parking_slot_general[o][1],POLICE_STATIONS.parking_slot_general[o][2],POLICE_STATIONS.parking_slot_general[o][3],xA,yA,zA,false)
                            nearestPoint = o

I tried to place the GetClosestVehicle, IsAreaOccupied and isPositionOccupied instead of ParkingSlotIsEmpty. Theoretically, the parking lsot will be selected as the closest potential parkin space, only if the condition just above indicates that the parking slot is empty.

Otherwise, following my various tests, the vehicle continued to appear despite the fact that there is one. This generated among other things, in case of spam spawn, to spawn the vehicles into each other.

Here is an excerpt from the list of parking slot positions :

    parking_slot_general = {
        {598.24,34.67,90.41,209.39}, -- Vinewood
        {581.34,38.86,92.12,192.03}, -- Vinewood
        {586.73,37.82,91.57,198.82}, -- Vinewood
        {592.34,36.37,91.0,198.15}, -- Vinewood
        {604.35,32.36,89.77,200.61}, -- Vinewood
        {610.31,30.22,89.15,201.51}, -- Vinewood
        {615.93,28.34,88.57,202.81}, -- Vinewood
        {621.63,26.59,87.98,214.79}, -- Vinewood
        {627.51,24.14,87.36,202.58}, -- Vinewood
        {407.91,-998.57,28.87,238.99}, -- Mission Row
        {408.05,-993.21,28.87,230.72}, -- Mission Row
        {407.94,-988.84,29.02,231.48}, -- Mission Row
        {408.39,-984.62,29.02,229.9}, -- Mission Row
        {408.41,-980.08,29.02,231.26}, -- Mission Row
        {431.32,-1027.79,28.67,359.35}, -- Mission Row
        {428.0,-1028.07,28.59,175.79}, -- Mission Row
        {435.04,-1027.32,28.46,0.75}, -- Mission Row
        {438.76,-1027.18,28.39,0.94}, -- Mission Row
        {442.58,-1026.58,28.32,180.12}, -- Mission Row
        {446.76,-1025.97,28.24,2.33}, -- Mission Row

Thank you :slight_smile:

I use entity enumerator and get the count of the entities in a given coordinates. If the count is 0 the spot is empty otherwise not.
Edit: You can check ESX.IsSpawnPointClear for reference.

It works with an entity enumerator. Thank you :slight_smile: