PhotonNetwork.CloseConnection(playerToKick) does not remove player from room

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

PhotonNetwork.CloseConnection(playerToKick) does not remove player from room

Bokky
2021-10-28 15:27:53

I am trying to implement a 'kick player' function to a room for the host. I have read through the forums and the only answer I can find is using  PhotonNetwork.CloseConnection. However, this does not remove the player from the room. Please can you advise how to remove the player? My code is below:

public void OnClick_CallKick()

{

Kick(nicknameInputField);

print("Kick Button Clicked");

}

private void Kick(TMP_InputField inputField)

{

if (inputField == null)

{

return; // log error?

}

string nickname = inputField.text;

Kick(nickname);

}

private void Kick(string nickname)

{

if (string.IsNullOrEmpty(nickname))

{

return; // log error?

}

foreach (KeyValuePair<int, Player> player in PhotonNetwork.CurrentRoom.Players)

{

if (player.Value != PhotonNetwork.LocalPlayer) //checking if this is the local player in question

{

if (nickname.Equals(player.Value.NickName))

{

print("Kicking " + player.Value);

KickingPlayer(player.Value);

return;

}

}

}

}

private void KickingPlayer(Player playerToKick)

{

if (!PhotonNetwork.IsMasterClient)

{

return; // log error?

}

print("Closing connection for " + playerToKick.NickName);

//not working

PhotonNetwork.EnableCloseConnection = true;

PhotonNetwork.CloseConnection(playerToKick);

}

Comments

[Deleted User]
2021-10-29 00:29:36

Hi, depending on what version of PUN you are using, Photon recently pushed an update to have CloseConnection disabled by default (for good reason). CloseConnection is a clientside based kick, meaning it does not involve the server authority at all other than just transmitting the event.

This means every client needs to have EnableCloseConnection as true. I advise putting this in a constructor somewhere when the game starts, you could even put it in the PhotonNetwork constructor. Just remember that changing the enable bool locally does not change it for everyone else.

Back to top