NetworkGetNetworkIdFromEntity crashing serverside script "Tried to access invalid entity"

  • Client version: Beta
  • FxServer Version: b7604
  • What you expected to happen: It should not crash the whole serverside script when getting a invalid entity
  • What actually happens: The serverside script crashes
  • Category of bug: serverside native

Hey, I’d like to report an issue with the “NetworkGetNetworkIdFromEntity” native. It’s causing crashes when attempting to access an invalid entity, even when checking the entity with “DoesEntityExist” first. It seems that in rare cases, the entity exists when checked with DoesEntityExist but is considered invalid when checked with NetworkGetNetworkIdFromEntity.

Proposed fix: Change the native function to prevent crashing the entire server-side script when calling an invalid entity. Currently, the entire server-side script crashes with the error message: “Execution of native 000000009e35dab6 in script host failed: Tried to access invalid entity.”

Repro script (server-side), although encountering the crash is highly unlikely, because more players = more vehicles / lower wait timer = higher possibility:

CreateThread(function()
    while true do
        for _, vehicle in ipairs(GetAllVehicles()) do
			if DoesEntityExist(vehicle) then
				local netId = NetworkGetNetworkIdFromEntity(vehicle) --CAUSING SCRIPT TO CRASH
			end
        end
        Wait(1000)
    end
end)

There are multiple server-side entity-related natives that can throw this type of exception (even when the entity does seem to exist).

To work around this in resource code, you can wrap any of those affected natives in a pcall and treat any thrown exception as if the entity doesn’t exist.

For example:

local status, res = pcall(function()
  return NetworkGetNetworkIdFromEntity(vehicle)
end)

(this might not be the most elegant solution, but yeah :smiley: )

Thx a lot for the workaround.
But still something that really should be handled by the native itself, as you said on discord :slight_smile:

1 Like