[HELP] GetNetworkObject : no object by ID

Hey there,

got one question regarding your code:
Is it correct that your line targetPos = getRandomNumber() is a placeholder for simplicity sake? Like you are saving a lot of data inside targetPos(like position, heading, rotation etc) which can’t currently be seen inside the code. I’ll assume that the above is the case.

That being said I had my fair share of problems syncing animations across multiple clients. I was able to get an unreliable solution meaning it works most of the time.

What I’d try is create a server-event which would trigger a client-event for every client.
So it would look like this:

client-side, your code

-- omitting your definitions

local ObjNetID = ObjToNet(myObj) -- get the network ID of your Object

while targetPos.rX <= 0.0 do -- got a question on this, see note at bottom of post
  Wait(0)
  targetPos.rX = targetPos.rX + 1
  TriggerServerEvent('syncsv:RotateEntity', ObjNetID, targetPos.rX, targetPos.rY, targetPos.rZ)
end

server-side

RegisterNetEvent('syncsv:RotateEntity')
AddEventHandler('syncsv:RotateEntity', function(ObjNetID, rX, rY, rZ)
  TriggerClientEvent('synccl:RotateEntity', -1, ObjNetID, rX, rY, rZ) -- Triggers the event for every client (-1)
end)

again client-side

RegisterNetEvent('synccl:RotateEntity')
AddEventHandler('synccl:RotateEntity', function(ObjNetID, rX, rY, rZ)
  local Obj = NetToObj(ObjNetID)
  SetEntityRotation(Obj, rX, rY, rZ, 5, true) --note that the documentation mentions two more parameters than you use
end)

This code will be problematic because of the Wait(0)-while loop in your script. I don’t fully understand what the intention of that loop is but it might be a good idea to set the Wait-timer to something like Wait(50) or even higher numbers (500?). This would need testing to see how it behaves with multiple clients present.


Note: Regarding your target animation I’d like to ask what the intention is behind looping it like that. I’m assuming every value rx, ry, rz to be an angle and as such to be in the range [0, 360].
First I’m wondering whether you just want to make sure that no negative values get set. If that is the case move SetEntityRotation() outside of the while loop (the above mentioned events would need to be added to the following code):

while targetPos.rX <= 0.0 do
  Wait(0)
  targetPos.rX = targetPos.rX + 1
end
SetEntityRotation(myObj, targetPos.rX, targetPos.rY, targetPos.rZ)

This would also eliminate the need for the Wait-timer to be set larger as I described above (as the event only triggers once and not x times).

If my assumption regarding rx, ry, rz to be angles is correct I’d suggest the following approach to make sure that every angle is between 0 and 360 degrees:

while targetPos.rX < 0.0 or targetPos.rX > 360.0 do
  if targetPos.rX < 0.0 then
    targetPos.rX = targetPos.rX + 360.0
  elseif targetPos.rX > 360.0 then -- the definition of this if-clause is redundant, I do it just for clarity
    targetPos.rX = targetPos.rX - 360.0
  end
  Wait(0)
end

while targetPos.rY < 0.0 or ... --copy paste the above code and modify for rY and rZ

This should preserve the actual angle of the object you want to place.

1 Like

Hey, thanks for your reply and your time.

I do use a while true do for changing the angle of the target TOP/DOWN to add a good effect and not just create the object on top

You the best !

To be honest I had also thought of passing the target creation + the rotation on the server-side and then creating the object locally but with your idea to only do the server-side rotation it works perfectly well.

Hey, so all the target stuff are well replicated, i just found one thing that i don’t like at all, is there a way to change the replication system for the rotation to have some thing more fast as you can see on the video, the target rotation are so “slow” like they are not reactive.

I really like this video :smiley:

the quick workaround for a faster movement would probably be editing the +1 in the while loop, e.g.:

while targetPos.rX < 0.0 do -- note "smaller" instead of "smaller or equal"
  Wait(0)
  targetPos.rX = targetPos.rX + 5
  if targetPos rX >= 0.0 then -- this prevents the target from rotating too much
    targetPos.rX = 0.0
  end

  TriggerServerEvent('syncsv:RotateEntity', ObjNetID, targetPos.rX, targetPos.rY, targetPos.rZ)
end

Can you confirm you are using Wait(0) still? Also check it with multiple people on the server. Things tend to bug once multiple clients are near :smiley:

Strangely enough there doesn’t seem to be a native SetEntityRotationVelocity() if though Getexists…

If you want a more realistic approach, using a logistic function can do everything for you as you can pretty much set the velocity of the movement to whatever you want at each moment. Coding the function might be a little hard depending on how much you like maths though :smiley:

I suggest using GeoGebra (or to be exact the graphing tool) to plot your function and change the parameters until you get what you want. Definitely try the first option to see if changing the turning value actually does things. If it does this will work too. If necessary I can also recreate the function for you in lua.

1 Like

To be honest, i’m really a big noob with maths ahaha, and Yes i still use the wait(0)

If necessary I can also recreate the function for you in lua.
Yeah why not i would like to see how we can do that if you have time.

So i edit this, When we shoot the target the target goes down and not deleted until the timer goes down.
the issue that i got is for the other client they see on console “No object by id …”

I might be terribly wrong here but I don’t think that the console remark is necessarily bad. You can somewhat suppress the message by using NetworkDoesEntityExistWithNetworkId(). Anyone else got an opinion on this?

As for the function please try adding 5 to targetPos.rX first so we can see if that actually changes anything.
If it doesn’t change I’m out of ideas on how to influence the speed of the rotation.
If the rotation does change, check whether you can find a value where you are happy with the rotation speed.
Reason for this is that using a complicated mathematical function here will be quite heavy on computing time. Imo it’s not that worrying when not used much. However if a very simple add x approach is enough there is no need to do complicated coding.

2 Likes

I see what you mean, yeah, but the speed is not the issue the issue come from the replication i guess, it’s make some weird lags when the target does rotate

So what i mean when i say it’s not reactive you can see like on the video is not the same
i do rec one without sync and the other one with sync :

So i want to know how can i replicate my object rotation without any “lags”.

without sync all work well and reactive, but with sync added passed by server-side for the rotation it work but with some lags.

Target Sync :

Target Not Sync :

the lag is the result of the inquiry getting sent over the server and back to you.
Note that I do not have any larger background to coding so I don’t really know pros and cons of different solutions.

I’ll need some help here (or you to test things).
Copying the code from my first answer you could change it like this:

client-side 1

-- omitting your definitions

local ObjNetID = ObjToNet(myObj)
local PlayerPedNetID = PedToNet(GetPlayerPed(-1)) -- saves the NetID of the shooting players' ped

while targetPos.rX <= 0.0 do
  Wait(0)
  targetPos.rX = targetPos.rX + 1
  TriggerEvent('synccl:RotateEntity', ObjNetID, targetPos.rX, targetPos.rY, targetPos.rZ, 0) -- triggers the client event directly for the shooting ped, note the `0` for the last entry
  TriggerServerEvent('syncsv:RotateEntity', ObjNetID, targetPos.rX, targetPos.rY, targetPos.rZ, PlayerPedNetID) -- send the NetID to the server
end

server-side 1

RegisterNetEvent('syncsv:RotateEntity')
AddEventHandler('syncsv:RotateEntity', function(ObjNetID, rX, rY, rZ, PlayerPedNetID)
  TriggerClientEvent('synccl:RotateEntity', -1, ObjNetID, rX, rY, rZ, PlayerPedNetID) -- Triggers the event for every client and sends the NetID of the shooting clients' ped with it
end)

client-side 2

RegisterNetEvent('synccl:RotateEntity')
AddEventHandler('synccl:RotateEntity', function(ObjNetID, rX, rY, rZ, PlayerPedNetID)
  local Obj = NetToObj(ObjNetID)
  local PlayerNetID = PedToNet(GetPlayerPed(-1)) -- checks the current clients' PlayerPedNetID
  if PlayerNetID ~= PlayerPedNetID then -- checks whether the current players' ped is equal to the triggering clients' ped; executes only if that isn't the case
    SetEntityRotation(Obj, rX, rY, rZ, 5, true)
  end
end)

The idea here is that the NetID of the players’ ped is unique (I hope…). First the event to rotate the entity gets triggered instantly for the shooting player. Next it gets triggered over the server again and for all players in the vicinity as well. However the execution over the server has the NetID of the triggering player sent as well. Only if the sent NetID is NOT equal to the current clients’ Net ID (indicating that it is a different client) the rotation gets set properly.

1 Like

You the best, thanks you so much for your time !
that is the solution so to all people who get some latence with sync using isNetWork just look at the solution process Silverman man you the best !

eh…
have you tried having two people shoot at the same target at the same time?
I suppose that getting this script to trigger on the same object by two people at the same time might see some funny results. A lot of if’s though so might be quite the edge-case.
Still there might be other edge-cases hidden that will thoroughly break your script if not attended to. I suggest testing (and by that I mean excessively playtest it with multiple people) a lot more before actually marking this as the solution. It could work but there is still a lot that can go wrong :smiley:

1 Like

I see what you mean, but for my case this is not what people will can do.
for the trainning system it will be one by time the other player will wait them time to can train

As you can see on the video all seem work like i needed.

I only do have one issue that i don’t understand why and how that happend :
GetNetworkObject : no object by ID …

function CreateTarget() --> Here we create the target.
    targetPos = getRandomNumber()

    myObj = CreateObject(Config.HashTarget, targetPos.x, targetPos.y, targetPos.z-1, true)
    SetEntityHeading(myObj, targetPos.h)
    SetEntityRotation(myObj, targetPos.rX, targetPos.rY, targetPos.rZ)
    TargetSpawned[#TargetSpawned+1] = ObjToNet(myObj)

    local ObjNetID = ObjToNet(myObj)
    targetNetId = ObjNetID
    local PlayerPedNetID = PedToNet(GetPlayerPed(-1))

    if not NetworkGetEntityIsNetworked(myObj) then
        NetworkRegisterEntityAsNetworked(myObj)
    end

    if NetworkDoesNetworkIdExist(targetNetId) then
        if (NetworkGetEntityIsNetworked(myObj)) then
            while targetPos.rX <= 0.0 do
                Wait(0)
                targetPos.rX = targetPos.rX + 1.7
                TriggerEvent('GTA:RotateEntity', targetNetId, targetPos.rX, targetPos.rY, targetPos.rZ, 0)
                TriggerServerEvent('GTA:RequestRotateEntity', targetNetId, targetPos.rX, targetPos.rY, targetPos.rZ, PlayerPedNetID)
            end
        end
    end
end

This is the main function that i use to create the target, as we can see the target seem to be networked.

Hello first of all I’m sorry for the inconvenience because it has nothing to do with it but I wanted to know how you manage to have two five meters open at the same time please because being alone it’s complicated to develop things very often thank you.

Hoo you mean how to have 2 instance of fivem.
Check it here : [How To] Correctly Use the -cl2 Launch Parameter to Launch FiveM Twice

1 Like

Thank you very much

1 Like

same error on vehicle create did u find any solution ?