Using canary: No Windows version: 1909 System specifications:
Server
Operating system: Windows Artifact version: 3580 IP address: localhost Resources: default System specifications:
Incident
Summary: When I start resource, automobile is created. If I stop resource, then automobile is deleted. If I start and stop (restart) resource to often, then created Automobile will not be deleted and we will get more and more automobiles in Network Object Viewer. Expected behavior: Created automobile exptected to be deleted. Actual behavior: Created automobile is not deleted Steps to reproduce:
Run next code on server side and then restart it many times with interval less then 1 second.
-- server.lua
-- Our plane itself will be stored in 'vehicle' variable
local vehicle = nil
-- Our plane coordinates
local startCoords = vector3(-1594.0, -2993.0, 20.94)
-- On following lines we identifying if resource was started and in this case we are creating our plane and freeze it
-- https://docs.fivem.net/docs/scripting-reference/events/list/onResourceStart/
AddEventHandler('onResourceStart', function(resourceName)
if GetCurrentResourceName() == resourceName then
local vehicleId = Citizen.InvokeNative(GetHashKey("CREATE_AUTOMOBILE"), GetHashKey("velum2"), startCoords)
while not DoesEntityExist(vehicleId) do
Citizen.Wait(1)
print("Vehicle is not created yet...")
end
vehicle = vehicleId
print("Vehicle created. Freeze position of the vehicle.")
Citizen.InvokeNative(GetHashKey("FREEZE_ENTITY_POSITION"), vehicle, true)
end
end)
-- We delete our plane in case resource was stoped (but it may not work if you restart resource to often - OneSync :/)
-- https://docs.fivem.net/docs/scripting-reference/events/list/onResourceStop/
AddEventHandler('onResourceStop', function(resourceName)
if GetCurrentResourceName() == resourceName then
if vehicle ~= nil then
while not DoesEntityExist(vehicle) do
print("Deleting vehicle...")
Citizen.Wait(0)
end
print("Delete vehicle")
DeleteEntity(vehicle)
end
end
end)
.dmp files/report IDs: Not related Any additional info:
This method is not going to block on a yield, seriously, note the part where it’s not printing ‘Delete vehicle’ sometimes.
The underlying issue, as said in chat, is that your entity handle somehow invalidates after some step you’re not providing, as seen by DoesEntityExist failing and you ending up in the yielding code path:
Next time don’t be suprised why people looking for a workaround, instead wasting their time and be offended. Anyway, I am providing repro for next error:
-- main.lua
-- Our plane itself will be stored in 'vehicle' variable
local vehicleIdentifier = "pubg_plane"
local vehicle = nil
-- Our plane coordinates
local startCoords = vector3(-1594.0, -2993.0, 20.94)
-- On following lines we identifying if resource was started and in this case we are creating our plane and freeze it
-- https://docs.fivem.net/docs/scripting-reference/events/list/onResourceStart/
AddEventHandler('onResourceStart', function(resourceName)
if GetCurrentResourceName() == resourceName then
local vehicleId = Citizen.InvokeNative(GetHashKey("CREATE_AUTOMOBILE"), GetHashKey("blista"), startCoords)
while not DoesEntityExist(vehicleId) do
Citizen.Wait(1)
print("Vehicle is not created yet...")
end
vehicle = vehicleId
Entity(vehicle).state.id = vehicleIdentifier
print("Vehicle created. Freeze position of the vehicle.")
Citizen.InvokeNative(GetHashKey("FREEZE_ENTITY_POSITION"), vehicle, true)
end
end)
-- We delete our plane in case resource was stoped (but it may not work if you restart resource to often - OneSync :/)
-- https://docs.fivem.net/docs/scripting-reference/events/list/onResourceStop/
AddEventHandler('onResourceStop', function(resourceName)
if GetCurrentResourceName() == resourceName then
if vehicle ~= nil then
local vehicles = GetAllVehicles()
for k,v in ipairs(vehicles) do
local entity = Entity(v)
local identifier = entity.state.id
print(k, v, identifier)
if identifier == vehicleIdentifier then
print("Delete vehicle")
DeleteEntity(v)
end
end
-- DeleteEntity(vehicle)
end
end
end)
Restart it as fast as possible until you get an error
You are welcome. Thanks for a rapid reaction on this case. I have a bit more information about this case:
After succesfull reproduction of the error, I’ve commented out everything in file, pasted following code and restarted resource:
-- main.lua
local vehicles = GetAllVehicles()
for k,v in ipairs(vehicles) do
local entity = Entity(v)
local identifier = entity.state.id -- this is a line 50
print(k, v, identifier)
if identifier == vehicleIdentifier then
print("Delete vehicle")
DeleteEntity(v)
end
end
My apologies for it taking a bit longer to debug this - latest Visual Studio update entirely broke debugging so it’s hard to investigate without having to restart VS every 2 breakpoints hit.