How to kick a player

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

  • Any help please
  • JohnTubeJohnTube mod
    edited January 27
    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);
        }
    
  • edited February 10
    @JohnTube

    It worked! Thank you so much, And i want something else, I want when the player kicks he is directed to Lobby
Sign In or Register to comment.