Error while trying to transfer entity from the server to the client

I’m generating a bunch of objects on the server and want to make them interactable using ox_target, for that I need to transfer them to the client. The problem is that when I try to transfer them using NetID I get the following error:

server.lua (lines 107-110):

        table.insert(objs, CreateObject(prop, xcord, ycord, 9.24316, false, false, false))
        local rotation = math.random(0, 360)+0.0
        SetEntityHeading(objs[ind], rotation)
        TriggerClientEvent("testtest", 1, NetworkGetNetworkIdFromEntity(objs[ind]))

client.lua:

RegisterNetEvent('testtest')
AddEventHandler('testtest', function(netId)
    local ent = NetworkGetEntityFromNetworkId(netId)

    exports.ox_target:addEntity(ent, {
        {
            icon = "fa-solid fa-box",
            label = "test",
            distance = 2.0,
            onSelect = function(data)
                print("test")
            end,
            canInteract = function(entity)
                return true 
            end
        }
    })
end)

Since then I figured out that somehow my function broke and this way the entities doesn’t exist. I figured it out because the rotate function does nothing so I put a DoesEntityExist(objs[ind)) into the script and got back bunch of falses. What is the right way to store objects in a table? I create them in a bulk so local obj = CreateObject(etc… doesn’t work.

this returns false

        table.insert(objs, CreateObject(prop, xcord, ycord, 9.24316, false, false, false))
        local rotation = math.random(0, 360)+0.0
        print(DoesEntityExist(objs[ind])

I found the solution firstly I needed to use CreateObjectNoOffset() instead of CreateObject() secondly I had to wait a little before passing it to the client so that the object is created there too.

        table.insert(objs, CreateObjectNoOffset(prop, xcord, ycord, zcord, false, false, false))
        local rotation = math.random(0, 360)+0.0
        SetEntityHeading(objs[ind], rotation)
        Wait(1000)
        TriggerClientEvent("testtest", -1, NetworkGetNetworkIdFromEntity(objs[ind]))

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