ESX.Game.SpawnVehicle -> I can only spawn at some certain coords?

Hello! I’ve been trying to make a custom quest script, where the “npc” tells the user there is a fancy car to hijack. I store some random coords in a list, and then make a variabel that does a math.random to take out one of those coords, and spawn a vehicle on it, and put a blip on it aswell. However, it only works if my coords in that list is close to where my npc is. heres how i have done it!

Client side:

ESX = nil

local Keys = {

** [“ESC”] = 322, [“F1”] = 288, [“F2”] = 289, [“F3”] = 170, [“F5”] = 166, [“F6”] = 167, [“F7”] = 168, [“F8”] = 169, [“F9”] = 56, [“F10”] = 57,**

** ["~"] = 243, [“1”] = 157, [“2”] = 158, [“3”] = 160, [“4”] = 164, [“5”] = 165, [“6”] = 159, [“7”] = 161, [“8”] = 162, [“9”] = 163, ["-"] = 84, ["="] = 83, [“BACKSPACE”] = 177,**

** [“TAB”] = 37, [“Q”] = 44, [“W”] = 32, [“E”] = 38, [“R”] = 45, [“T”] = 245, [“Y”] = 246, [“U”] = 303, [“P”] = 199, ["["] = 39, ["]"] = 40, [“ENTER”] = 18,**

** [“CAPS”] = 137, [“A”] = 34, [“S”] = 8, [“D”] = 9, [“F”] = 23, [“G”] = 47, [“H”] = 74, [“K”] = 311, [“L”] = 182,**

** [“LEFTSHIFT”] = 21, [“Z”] = 20, [“X”] = 73, [“C”] = 26, [“V”] = 0, [“B”] = 29, [“N”] = 249, [“M”] = 244, [","] = 82, ["."] = 81,**

** [“LEFTCTRL”] = 36, [“LEFTALT”] = 19, [“SPACE”] = 22, [“RIGHTCTRL”] = 70,**

** [“HOME”] = 213, [“PAGEUP”] = 10, [“PAGEDOWN”] = 11, [“DELETE”] = 178,**

** [“LEFT”] = 174, [“RIGHT”] = 175, [“TOP”] = 27, [“DOWN”] = 173,**

** }**

** local NPC = {**

** {seller = true, model = “g_m_y_ballaeast_01”, x = 340.04, y = -2098.44, z = 17.2, h = 241.85},**


}

local v1 = vector3(340.04, -2098.44, 17.2) --Coords för npcn

local PlayerData = {}

local cars = {“adder”, “comet”, “cheetah”}

**local hemtabilenblip **

local deliveryblip

local VehicleSpawnPoint = {

** {x = 347.22, y = -2096.55, z = 18.78, h = 112.08},**

** {x = 347.22, y = -2096.55, z = 18.78, h = 112.08},**

** {x = 347.22, y = -2096.55, z = 18.78, h = 112.08}**

}

local Deliverypoints = {

** {x = 104.88, y = -1995.25, z = 20.27},**

** {x = 104.88, y = -1995.25, z = 20.27},**

** {x = 104.88, y = -1995.25, z = 20.27}**

}

Citizen.CreateThread(function()

**while ESX == nil do **

** TriggerEvent(‘esx:getSharedObject’, function(obj) ESX = obj end)**

** Citizen.Wait(0)**

end

if Config.EnableNpc then

** for _, v in pairs(NPC) do**

** RequestModel(GetHashKey(v.model))**

** while not HasModelLoaded(GetHashKey(v.model)) do**

** Wait(1)**

** end**

** local npc = CreatePed(4, v.model, v.x, v.y, v.z, v.h, false, true)**

** SetPedFleeAttributes(npc, 0, 0)**

** SetPedDropsWeaponsWhenDead(npc, false)**

** SetPedDiesWhenInjured(npc, false)**

** SetEntityInvincible(npc , true)**

** FreezeEntityPosition(npc, true)**

** SetBlockingOfNonTemporaryEvents(npc, true)**

** if v.seller then **

** RequestAnimDict(“missfbi_s4mop”)**

** while not HasAnimDictLoaded(“missfbi_s4mop”) do**

** Wait(1)**

** end**

** TaskPlayAnim(npc, “missfbi_s4mop” ,“amb@world_human_aa_smoke@male@idle_a” ,8.0, 1, -1, 49, 0, false, false, false)**

** else**

** GiveWeaponToPed(npc, GetHashKey(“WEAPON_ADVANCEDRIFLE”), 2800, true, true)**

** end**

** end**

end

end)

function Draw3DText(x, y, z, text, scale)

** local onScreen, _x, _y = World3dToScreen2d(x, y, z)**

** local pX, pY, pZ = table.unpack(GetGameplayCamCoords())**


** SetTextScale(0.0, 0.35)**

** SetTextFont(4)**

** SetTextProportional(1)**

** SetTextEntry(“STRING”)**

** SetTextCentre(1)**

** SetTextColour(255, 255, 255, 215)**


** AddTextComponentString(text)**

** DrawText(_x, _y)**


** local factor = (string.len(text)) / 230**

** – DrawRect(_x, _y + 0.0250, 0.095 + factor, 0.06, 41, 11, 41, 100)**

end

Citizen.CreateThread(function()

** while true do**

** Citizen.Wait(5)**

** local coords = GetEntityCoords(GetPlayerPed(-1))**

** for k, v in pairs(NPC) do**

** if GetDistanceBetweenCoords(coords, v.x, v.y, v.z, true) < 5 then**

** Draw3DText(v1.x, v1.y, v1.z + 1.25, “~w~[~g~E~w~] Snacka med Mohammed”, 0.80)**


** end**

** end**

end

end)

function getcoords ()

** exports[‘progressBars’]:startUI(1000, “Mohammed tar fram koordinater…”)**

Wait(1000)

end

local bilmodell = (cars[math.random(1, #cars)]) – hämtar ut random bil

local spawn = (VehicleSpawnPoint[math.random(1, #VehicleSpawnPoint)]) – hämtar ut random spawn location

local deliveryp = (Deliverypoints[math.random(1, #Deliverypoints)]) – hämtar ut random delivery location

function spawncar ()


** RequestModel(bilmodell)**

** while not HasModelLoaded(bilmodell) do**

** RequestModel(bilmodell)**

** Citizen.Wait(1)**

** end**

** ClearAreaOfVehicles(spawn.x, spawn.y, spawn.z, 10.0, false, false, false, false, false) – se till att det inte finns några andra npc vehicles i närheten.**

** ESX.Game.SpawnVehicle(bilmodell, vector3(spawn.x, spawn.y, spawn.z), spawn.h, function(vehicle)**

** – local vehicle = CreateVehicle(bilmodell, spawn.x, spawn.y, spawn.z, 0.0, true, false) – spawna bilen**

** SetEntityAsMissionEntity(vehicle, true, true)**

** SetVehicleDoorsLocked(vehicle, 2)**

** end)**



** --Sätter blippen på mappen där fordonet har spawnat.**


** hemtabilenblip = AddBlipForCoord(spawn.x, spawn.y, spawn.z)**

** SetBlipSprite(hemtabilenblip, 1)**

** SetBlipDisplay(hemtabilenblip, 4)**

** SetBlipScale(hemtabilenblip, 1.0)**

** SetBlipColour(hemtabilenblip, 5)**

** SetBlipAsShortRange(hemtabilenblip, true)**

** BeginTextCommandSetBlipName(“STRING”)**

** AddTextComponentString(“Bilen”)**

** EndTextCommandSetBlipName(hemtabilenblip)**

** SetBlipRoute(hemtabilenblip, true)**


–om vi är tillräckligt nära bilen, ta bort blippen

Citizen.CreateThread(function()

** while true do**

** Citizen.Wait(0)**

** local ped = GetPlayerPed(-1)**

** if GetDistanceBetweenCoords(spawn.x, spawn.y, spawn.z, GetEntityCoords(ped)) < 4.0 then**

** RemoveBlip(hemtabilenblip) **

** local lock = GetVehicleDoorLockStatus(vehicle)**

** if lock == 1 then**

** setdeliveryblip()**


** end**

end

end

end)


function setdeliveryblip()

** deliveryblip = AddBlipForCoord(deliveryp.x, deliveryp.y, deliveryp.z)**

** SetBlipSprite(deliveryblip, 1)**

** SetBlipDisplay(deliveryblip, 4)**

** SetBlipScale(deliveryblip, 1.0)**

** SetBlipColour(deliveryblip, 5)**

** SetBlipAsShortRange(deliveryblip, true)**

** BeginTextCommandSetBlipName(“STRING”)**

** AddTextComponentString(“Bilen”)**

** EndTextCommandSetBlipName(deliveryblip)**

** SetBlipRoute(deliveryblip, true)**

** end**

** end**

** – ESX.Game.SpawnVehicle(bilmodell, vector3(spawn.x, spawn.y, spawn.z), spawn.h, function(vehicle)**

** – print(DoesEntityExist(bilmodell), ‘this code is async!’)**

** – end)**

Check the coordinates
Use spawncar() when the player is within 50m of the spawn point

Example:

local inMission = false -- change inMission to true, when you need to spawn a vehicle
local spawnDist = 50.0
Citizen.CreateThread(function()
    while inMission do
        Citizen.Wait(500)
        local pCoords = GetEntityCoords(PlayerPedId())
        local dist = GetDistanceBetweenCoords(pCoords, spawn.x, spawn.y, spawn.z, false)
        if dist < spawnDist then
            spawncar()
        end
    end
end)

I advise you to use arrays to store the data of the current mission, it is much more convenient to work with them

Hello! Thank you so much for the reply, this works, however it just keeps spamming vehicle, feels like im missing to close the while loop somewhere…

Heres where i do it.

function spawncar ()

inMission = true

local spawnDist = 50.0

Citizen.CreateThread(function()

local cars = {"adder", "comet", "cheetah"}

local bilmodell = (cars[math.random(#cars)]) -- hämtar ut random bil

Citizen.CreateThread(function()

RequestModel(bilmodell)

while not HasModelLoaded(bilmodell) do

    RequestModel(bilmodell)

    Citizen.Wait(0)

end

end)

local hemtabilenblip

hemtabilenblip = AddBlipForCoord(Config.spawn.x, Config.spawn.y, Config.spawn.z)

                    SetBlipSprite(hemtabilenblip, 1)

                    SetBlipDisplay(hemtabilenblip, 4)

                    SetBlipScale(hemtabilenblip, 1.0)

                    SetBlipColour(hemtabilenblip, 5)

                    SetBlipAsShortRange(hemtabilenblip, true)

                    BeginTextCommandSetBlipName("STRING")

                    AddTextComponentString("Bilen")

                    EndTextCommandSetBlipName(hemtabilenblip)

                    SetBlipRoute(hemtabilenblip, true)

                    Citizen.CreateThread(function()

                    

                    while inMission do

                    Citizen.Wait(500)

                    local pCoords = GetEntityCoords(PlayerPedId())

                    local ped = GetPlayerPed(-1)

                    local dist = GetDistanceBetweenCoords(pCoords, Config.spawn.x, Config.spawn.y, Config.spawn.z, false)

                    if dist < spawnDist then

                    TriggerServerEvent('diize_quest:spawncar')

                    

                            if GetDistanceBetweenCoords(spawn.x, spawn.y, spawn.z, GetEntityCoords(ped)) < 4.0 then --ta bort blip om vi kommer nära.

                                    RemoveBlip(hemtabilenblip) 

                    

                        end

                    end

                    end

                    Citizen.CreateThread(function()

                        

                            Citizen.Wait(0)

                            local lock = GetVehicleDoorLockStatus(vehicle)

                            if lock == 1 or lock == 0 --IsPedInAnyVehicle(GetPlayerPed(-1)) 

                         then setdeliveryblip()

                        TriggerServerEvent('esx_carthief:registerActivity', 1)

                                isTaken = 1

                                isDelivered = 0

                    end

                end)

This is my mistake, I forgot to add something

local carSpawned = false -- create new local

-- Edit this thread
Citizen.CreateThread(function()
    while inMission do
        Citizen.Wait(500)
        if not carSpawned then -- add this check
            local pCoords = GetEntityCoords(PlayerPedId())
            local dist = GetDistanceBetweenCoords(pCoords, spawn.x, spawn.y, spawn.z, false)
            if dist < spawnDist then
                spawncar()
                carSpawned = true -- or add this in function spawncar()
            end
        end --
    end
end)

yeah i figured it out on my own last night haha. Thanks, it works great.