Destroy its own instantiated objects when leaving server

Hi !

I have a quick question here :slight_smile:

I made a little demo where i have a server and one client, on my own computer (127.0.0.1), i just connect to the server, pop some little cubes and then disconnect. Right now, eveything is working. So why am I here ? When my client leaves, i ask my server to clean up the mess and destroy everything the client did:

void OnPlayerDisconnected (NetworkPlayer player){
		Network.DestroyPlayerObjects (player);
		Network.RemoveRPCs (player);
}

This is working but my client still has the cubes in its scene when i disconnect so if i go back on the server with the same client, without quitting and launching the exe again, i find myself with an empty server -as i can expect with the few lines of code just above- BUT with a client with all the previous cubes i popped. So when i pop more cubes, the server thinks there is 1 cube while my client has in fact >1 cubes.

I’m trying to find a proper way to destroy what one specific client did in my scene, without having to mark it on a list everytime i pop something and then use Object.Destroy() on all the items of the list, something that would clean up without wondering what or how much, like the server is able to do, something equivalent to that:

void OnDisconnectFromServer(){
    DestroyOwnObjects ();
	RemoveOwnRPCs ();
 }

Cheers :slight_smile:

I found part of my solution: for the objects i instantiate with Network.Instantiate, adding this prior to disconnect will work:

Network.DestroyPlayerObjects (Network.player);
Network.RemoveRPCs (Network.player);

But it doesn’t work with objects that i loaded on the server only ! When i connect my client to the server, pop one cube on RPCMode.AllBuffered, one cube on RPCMode.Server and then disconnect, my server will have one cube (instead of two like in my previous post), even if i do have these lines in my server script:

void OnPlayerDisconnected (NetworkPlayer player){
	Network.DestroyPlayerObjects (player);
	Network.RemoveRPCs (player);
}

In addition, with the new lines of code, my application seems less stable and crashes from time to time - not sure it is related, but who knows ?