Spawning Objects Server Side - ESX.OneSync.SpawnObject

Hello guys,

I am trying to figure out the ESX/Onesync function:

ESX.OneSync.SpawnObject(model, coords, heading, cb)

I want the objects to be server side so that when players load into the server after the script starts the props will be spawned.

My code so far is the following:

local ESX = exports["es_extended"]:getSharedObject()

local keycardProps = {} 
local model = 'hei_prop_hei_bio_panel'
local coords = vector3(41.4555, -2578.5092, 6.5398)
local yaw = -90.50
local Heading = -90.50

function SpawnObjectAndTrack(model, coords, heading)
    ESX.OneSync.SpawnObject(model, coords, heading, function(Object)
        while not DoesEntityExist(Object) do
            Citizen.Wait(1000)
            print("Waiting for Keycard Props to spawn.")
        end
		print("Keycard Props have spawned.")
        FreezeEntityPosition(Object, true)  
        table.insert(keycardProps, Object)
    end)
end

function CleanupkeycardProps()
    print("Cleaning up keycard props...")
    for _, Object in ipairs(keycardProps) do
       DeleteEntity(object)
    end
    keycardProps = {}  
end

Citizen.CreateThread(function()
    SpawnObjectAndTrack(model, coords, Heading)
end)

RegisterCommand("test", function(source, args, rawCommand)
CleanupkeycardProps()
end, false)

AddEventHandler("onResourceStop", function(resourceName)
    if resourceName == 'D2D-CardRooms' then
        CleanupkeycardProps()
    end
end)

The object spawns but im stuck in the DoesEntityExist loop, printing ā€˜Objectā€™ also comes back at nil.

Could anyone point me into the right direction as I am at a lost :smile:

I havenā€™t used ESX for a long time, and I can see that it has changed a lot, but Iā€™ll try to help!

The ESX.OneSync.SpawnObject function (if you are on the newest version of esx at least) actually returns a netID and not the entity handle as the documentation for ESX might make you believe.

Now, that only explains why the DoesEntityExist loop continues forever (as well, the variable you are checking isnā€™t an entity handle, itā€™s a netID), but not why Object is nil. But as you havenā€™t included the print(Object) in the code above I canā€™t really give you a solid answer on that, so Iā€™ll leave it up for you to figure out.

But if I should believe the documentation, then you might want to add a Wait(100) before executing the rest of the code ā€œto ensure the Object is availableā€. So to conclude, you might want to try something like this:

function SpawnObjectAndTrack(model, coords, heading)
    ESX.OneSync.SpawnObject(model, coords, heading, function(Object)
    	Wait(100)
		local entity = NetworkGetEntityFromNetworkId(Object)
        print(entity, Object)
        while not DoesEntityExist(entity) do
            Citizen.Wait(1000)
            print("Waiting for Keycard Props to spawn.")
        end
		print("Keycard Props have spawned.")
        FreezeEntityPosition(entity, true)  
        table.insert(keycardProps, entity)
    end)
end

I hope that helps, if not solving it at least giving you some new ideas. You could also just spawn the object yourself instead of using esxā€™s functions, but thatā€™s just a thought. Best of luck!

1 Like

Worth noting that function uses an RPC, so it requires a player to spawn in the entity and can have various oddities. Recommend you use CreateObjectNoOffset or do local entities depending on your exact use case.

2 Likes

Hi Linden!

Thatā€™s pretty much what iā€™ve fell back on doing as I gave up with that function :smile:

I am actually using ox target & lib in this project and was wondering if you knew of an ā€œappreciatedā€ version of SetEntityDistanceCullingRadius to pair with CreateObjectNoOffset, itā€™s worked as a hot fix for entities that have no net id when passing the entities from server to client (presumably because like you noted above it requires a player nearby to spawn).

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.