DomePlaysHD Posted March 14, 2019 Share Posted March 14, 2019 (edited) Hey guys, how i can delete a vehicle on PlayerDisconnected Event without errors? I always get a StackOverflowException. [ServerEvent(Event.PlayerDisconnected)] public void Event_OnPlayerDisconnected(Client client, DisconnectionType type, string reason) { if (client.HasData("JobStarted")) { NAPI.ClientEvent.TriggerClientEvent(client, "clearCheckpoint"); if (client.HasData("JobVehicle")) { client.Vehicle.Delete(); } } } Edited March 14, 2019 by DomePlaysHD Link to comment Share on other sites More sharing options...
robearded Posted March 14, 2019 Share Posted March 14, 2019 (edited) 2 hours ago, DomePlaysHD said: Hey guys, how i can delete a vehicle on PlayerDisconnected Event without errors? I always get a StackOverflowException. [ServerEvent(Event.PlayerDisconnected)] public void Event_OnPlayerDisconnected(Client client, DisconnectionType type, string reason) { if (client.HasData("JobStarted")) { NAPI.ClientEvent.TriggerClientEvent(client, "clearCheckpoint"); if (client.HasData("JobVehicle")) { client.Vehicle.Delete(); } } } You may get a StackOverflowException because you are trying to trigger a client event even tho the client disconnected? Are you sure the StackOverflowException comes from the vehicle delete? Edit: The above still shouldn't give a stack overflow but a null reference error if it would be the case. I remember running into something similar too, it happened when i was processing the event player out of vehicle. If you delete a vehicle that the player is inside the event out of vehicle will be called lot of times (and usually until it gives an error). Save the vehicle into a variable then warp the player out of the vehicle and then delete the vehicle. Or just add a 0.5s-1s timer before deleting the vehicle as the player will be disconnected anyway. Edited March 14, 2019 by robearded Link to comment Share on other sites More sharing options...
DomePlaysHD Posted March 15, 2019 Author Share Posted March 15, 2019 thx it works with a timer. here my code if someone needs it. [ServerEvent(Event.PlayerDisconnected)] public void Event_OnPlayerDisconnected(Client client, DisconnectionType type, string reason) { if (client.HasData("JobStarted")) { client.ResetData("JobStarted"); Vehicle job_vehicle = client.GetData("JobVehicle"); if (client.HasData("JobVehicle")) { NAPI.Task.Run(() => { job_vehicle.Delete(); }, delayTime: 1000); } } } #Close Link to comment Share on other sites More sharing options...
Xabi Posted March 15, 2019 Share Posted March 15, 2019 5 hours ago, DomePlaysHD said: thx it works with a timer. here my code if someone needs it. [ServerEvent(Event.PlayerDisconnected)] public void Event_OnPlayerDisconnected(Client client, DisconnectionType type, string reason) { if (client.HasData("JobStarted")) { client.ResetData("JobStarted"); Vehicle job_vehicle = client.GetData("JobVehicle"); if (client.HasData("JobVehicle")) { NAPI.Task.Run(() => { job_vehicle.Delete(); }, delayTime: 1000); } } } #Close Why would you GetData and, after getting it, check if the client has it with HasData? Link to comment Share on other sites More sharing options...
robearded Posted March 15, 2019 Share Posted March 15, 2019 1 hour ago, Xabi said: Why would you GetData and, after getting it, check if the client has it with HasData? Hmm, I think he wanted to put that line of code inside the if but didn't paid attention and put it before? It happens to me sometimes too, you only realize when you looking again at the code and you start thinking "what the heck did I wrote here🤔" Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now