Object-streamer issues

Hello!

I’m making an object streamer and it’s going quite well, but there’s a weird bug that when my character spawns for the first time, the object appears for a moment and then disappears. If I go out of the streaming radius and then back in then the object appears, so the basic idea works, but unfortunately it’s not good this way.

Anyone have any idea what could be causing this?

function SpawnObject(hash, x, y, z, rx, ry, rz)
     local model = GetHashKey(hash)
     
     RequestModel(model)
     while not HasModelLoaded(model) do
          Citizen.Wait(1)
     end

     local handle = CreateObject(model, x, y, z, true, false, false, false)
     SetEntityRotation(handle, rx, ry, rz, 1, false)

     SetModelAsNoLongerNeeded(model)
     return handle
end

-- Request object data from server side when loading client
Citizen.CreateThread(function()
     TriggerServerEvent("bvzs:streamer:getData")
end)

Citizen.CreateThread(function()
     while true do
          Citizen.Wait(1)

          local coords = GetEntityCoords(PlayerPedId())

          if #ServerObjects > 0 then
               for _, object in pairs(ServerObjects) do
                    if object ~= nil then
                         if GetDistanceBetweenCoords(coords, tonumber(object.x), tonumber(object.y), tonumber(object.z), true) <= streamedDistance and objects[_] == nil then
                              objects[_] = SpawnObject(tostring(object.hash), tonumber(object.x), tonumber(object.y), tonumber(object.z), tonumber(object.rx), tonumber(object.ry), tonumber(object.rz))
                              print("object: spawn")
                         end  

                         if GetDistanceBetweenCoords(coords, tonumber(object.x), tonumber(object.y), tonumber(object.z), true) > streamedDistance and objects[_] ~= nil then
                              DeleteObject(objects[_])
                              objects[_] = nil
                              print("object: delete")
                         end
                    end
               end
          end
     end
end)

Funny… it’s fixed. If I set the CreateObject isNetwork parameter to false, the above problem disappears.