- Home /
Question by
Stormy102 · Nov 10, 2014 at 02:07 PM ·
javascriptnetworkingscoreboardkick
What is wrong with this scoreboard?
I am trying to create a scoreboard for multiplayer. I want the scoreboard to track player's name, kills, death and ping. I also want the server to be able to kick players using this. This will provide the framework for kicking and a scoreboard. Here is the code:
#pragma strict
import System.Text.RegularExpressions;
//Strings
private var Username : String;
private var players : String[];
private var pings : String[];
//Ints
private var myConnection : int = -1;
private var i : int = 0;
private var a : int = 0;
private var kills : int[];
private var deaths : int[];
//Boolean
private var showScoreboard : boolean = false;
//KeyCodes
var toggleKey : KeyCode = KeyCode.Tab;
//NetworkPlayers
var connections : NetworkPlayer[];
function Start()
{
Username = PlayerPrefs.GetString("Username", "Guest");
if (Network.isServer)
{
connections = Network.connections;
ServerConfig();
}
if (Network.isClient)
{
if (myConnection == -1 && Network.isClient)
{
for (a = 0; a < connections.Length; a++)
{
if (connections[a] == networkView.owner)
{
myConnection = a + 1;
networkView.RPC("TellScoreboardName", RPCMode.All, Username, myConnection);
}
}
}
}
}
function Update()
{
if (myConnection == -1 && Network.isClient)
{
for (a = 0; a < connections.Length; a++)
{
if (connections[a] == networkView.owner)
{
myConnection = a + 1;
networkView.RPC("TellScoreboardName", RPCMode.All, Username, myConnection);
}
}
}
if (Input.GetKeyDown(toggleKey))
{
showScoreboard = true;
}
if (Input.GetKeyUp(toggleKey))
{
showScoreboard = false;
}
}
function OnGUI()
{
if (showScoreboard && Network.isServer)
{
GUI.Window(0, Rect(Screen.width /4, Screen.height/4, Screen.width /2, Screen.height/2), Scoreboard, "Scoreboard");
}
}
function Scoreboard(windowID : int)
{
GUI.Label(Rect(5,30,250,25), "Name");
GUI.Label(Rect(260,30,50,25), "Kills");
GUI.Label(Rect(315,30,50,25), "Deaths");
GUI.Label(Rect(370,30,100,25), "Ping (Avg)");
for (var PL : int = 0; PL < players.Length; PL++)
{
GUI.Label(Rect(5,60 + (25 * PL),250,25), players[PL]);
}
/*for (i = 0; i < kills.Length; i++)
{
GUI.Label(Rect(260,60 + (25 * i),50,25), kills[i].ToString());
}
for (i = 0; i < deaths.Length; i++)
{
GUI.Label(Rect(315,60 + (25 * i),50,25), deaths[i].ToString());
}
for (i = 0; i < connections.Length; i++)
{
GUI.Label(Rect(370,60 + (25 * (i + 1)),100,25), Network.GetAveragePing(Network.connections[i + 1]) + "ms");
}*/
if (Network.isServer && connections.Length > 0)
{
for (var PK = 0; PK < (connections.Length + 1); PK++)
{
if(GUI.Button(Rect(425, 50 + (25 * (PK + 1)), 100,25), "Kick"))
{
networkView.RPC("KickPlayer", RPCMode.AllBuffered, PK, connections[i]);
Network.CloseConnection(connections[i], true);
}
}
}
}
function ServerConfig()
{
var tempIntNoPlayers : int = GameObject.FindGameObjectWithTag("MainCamera").GetComponent(NetworkManager).NoOfPlayers;
players = new String[tempIntNoPlayers + 1];
kills = new int[tempIntNoPlayers + 1];
deaths = new int[tempIntNoPlayers + 1];
pings = new String[tempIntNoPlayers + 1];
players[0] = Username;
kills[0] = 0;
deaths[0] = 0;
pings[0] = "0";
}
@RPC
function ScoreboardUpdate(pls : String, info : NetworkMessageInfo)//, kls : int[], dts : int[], png : String[])
{
var stringArray : String[] = Regex.Split(pls, "-");
players = stringArray;
/*kills = kls;
deaths = dts;
pings = png;*/
}
@RPC
function TellScoreboardName(pl : String, connectionInt : int, info : NetworkMessageInfo)
{
if (Network.isServer)
{
players[connectionInt] = pl;
}
}
@RPC
function KickPlayer(playerNo : int, player : NetworkPlayer)
{
if (Network.isServer)
{
GameObject.FindGameObjectWithTag("MainCamera").GetComponent(FPSChat).addGameChatMessage(players[playerNo]+" was kicked from the game");
}
}
Any suggestions will help. Cheers,
Stormy102
Comment
Answer by ExtremePowers · Nov 10, 2014 at 02:12 PM
The client never gets the connections array if im not mistaking, so the length that it tries to get will always be 0.
Your answer
![](https://koobas.hobune.stream/wayback/20220613171148im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Two questions with mutliplayer 0 Answers
Where is a free, helpful tutorial on how to use Unity's networking? 2 Answers
MultiPlayer Game Network ? 1 Answer
getting a list of all lan servers 0 Answers
Unity 3d Networking Local Search 0 Answers