How to kick a player

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.

How to kick a player

Thestranger
2020-01-25 04:25:36

Hi, i just made a script to kick a player from room if player name equal the value and click button he will be kicked out , I'm using Photon

using System.Collections;  
using System.Collections.Generic;  
using UnityEngine;  
using UnityEngine.UI;  
   
public class LeaveCurrentMatch : MonoBehaviour  
{  
    public GameObject playerName;  
    public void OnClick_LeaveMatch()  
    {  
        PhotonNetwork.LeaveRoom();  
        PhotonNetwork.LoadLevel(0);  
    }  
   
    public void Kick(PhotonPlayer kick)  
    {  
        InputField inputField = playerName.GetComponent<InputField>();  
        string value = inputField.text;  
        PhotonNetwork.playerName = value;  
        PhotonNetwork.CloseConnection(kick);  
    }  
}

Comments

Thestranger
2020-01-26 00:15:29

Any help please

JohnTube
2020-01-27 10:43:13

Hi @Thestranger,

Thank you for choosing Photon!

Example code to get player using its nickname.
Suggestions: assign & cache input field component.

    [SerializeField]  
    private InputField nicknameInputField;  
   
    public void Kick()  
    {  
          Kick(nicknameInputField);  
    }

    private void Kick(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(Player player in PhotonNetwork.CurrentRoom.Players)  
          {  
               if (!player.IsLocal && nickname.Equals(player.Nickname))  
               {  
                    Kick(player);  
                    return;  
               }  
          }  
          // log error? player is local or not found?  
     }

    private void Kick(PhotonPlayer playerToKick)  
    {  
          if (!PhotonNetwork.IsMasterClient)  
          {  
                return; // log error?  
          }  
        PhotonNetwork.CloseConnection(playerToKick);  
    }  

Thestranger
2020-02-10 06:45:11

@JohnTube

It worked! Thank you so much, And i want something else, I want when the player kicks he is directed to Lobby

Back to top