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
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!
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.
Thatās pretty much what iāve fell back on doing as I gave up with that function
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).