- Home /
Why dont this script work?
Basicly I'm trying to make it so if the server host disconnects, ALL player objects gets wiped.
function OnDisconnectedFromServer (player: NetworkPlayer) {
Debug.Log("Disconnected from server: " + player);
Network.RemoveRPCs(player);
Network.DestroyPlayerObjects(player);
}
This should work shouldn't it!?
Gives me error : Script error: OnDisconnectedFromServer This message parameter has to be of type: The message will be ignored.
I just dont understand it
Answer by MrCranky · Jan 13, 2014 at 03:16 PM
OnDisconnectedFromServer doesn't take a network player as an argument. The parameter should be a NetworkDisconnection object, which presumably will give the reason why you were disconnected. You will have to figure out which network player was disconnected some other way.
I have made it so if a player disconnects, so does his stuff (player model etc), but I'm trying to make it so when the host leaves or shuts down server, all player objects from all players is removed. Any idea how to?
No idea, there's only a tiny fraction of your code provided.
It seems like a non-issue though. The host leaving isn't a special case of OnPlayerDisconnected, it's going to kill the server, surely? If the host leaves or disconnects, then everyone else will get the disconnection notice, because the server has gone away.
There are only two real events: either you have disconnected, or someone else has and you're still connected. If someone else has disconnected, you clean up their objects. I'm presu$$anonymous$$g you're already doing that on OnPlayerDisconnected. If you have been disconnected, then it doesn't matter if it was because the host died or because you got kicked, either way you should be cleaning up everyone else's objects as well as your own.
Right now, only if a player disconnects his stuff gets cleaned, but if a host leaves and the connections stops, it's as if they were still in game, other than they cant see other players moving ofcourse. I'm trying to make it do the same thing as it does when player disconnects, to when a host closes server, basicly delete their player model.
Right, so what's stopping you from doing that? If you get OnDisconnectedFromServer, then the game is done and you need to clean up. Put that method on whatever component manages your player model and every other player model, and do the cleanup there. You've given no indication as to how your code is structured, so we can't tell you where you should put the method or what it should do.
Your answer
![](https://koobas.hobune.stream/wayback/20220613130249im_/https://answers.unity.com/themes/thub/images/avi.jpg)