Sign in to follow this  
DomePlaysHD

vehicle delete error on PlayerDisconnected

Recommended Posts

Posted (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 by DomePlaysHD

Share this post


Link to post
Share on other sites
Posted (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 by robearded

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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🤔

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.