Score not updating bug fixed trial1

dev-hazim
Ali Sharoz 1 week ago
parent 075e65f66d
commit a316c3d81b

@ -10,17 +10,27 @@ public class PlayerScoreComponent : NetworkBehaviour
public PlayerItem playerItem;
public override void OnNetworkSpawn()
{
if (IsOwner && IsClient)
//if (IsOwner && IsClient)
//{
// Debug.Log($"[PlayerScoreComponent] Requesting score initialization for Player {OwnerClientId}");
// ScoreManager.Instance?.InitializePlayerScoreServerRpc(OwnerClientId,serverCharacter.uIStateDisplayHandler.m_UIState.playerName);
//}
//// For the server player (host), ensure the PlayerScoreComponent is registered properly
//if (IsServer && OwnerClientId == NetworkManager.Singleton.LocalClientId)
//{
// ScoreManager.Instance?.InitializePlayerScoreServerRpc(OwnerClientId, serverCharacter.uIStateDisplayHandler.m_UIState.playerName);
//}
if(IsOwner)
{
Debug.Log($"[PlayerScoreComponent] Requesting score initialization for Player {OwnerClientId}");
ScoreManager.Instance?.InitializePlayerScoreServerRpc(OwnerClientId,serverCharacter.uIStateDisplayHandler.m_UIState.playerName);
}
// For the server player (host), ensure the PlayerScoreComponent is registered properly
if (IsServer && OwnerClientId == NetworkManager.Singleton.LocalClientId)
{
ScoreManager.Instance?.InitializePlayerScoreServerRpc(OwnerClientId, serverCharacter.uIStateDisplayHandler.m_UIState.playerName);
}
serverCharacter=GetComponent<ServerCharacter>();
serverCharacter =GetComponent<ServerCharacter>();
}
public void UpdateScore(int newScore)

@ -25,7 +25,7 @@ public class ScoreManager : NetworkBehaviour
{
if (IsServer)
{
StartCrowPenaltyCoroutineServerRpc();
StartCrowPenaltyCoroutine();
}
}
@ -38,6 +38,7 @@ public class ScoreManager : NetworkBehaviour
playerNames[ownerClientId] = name;
Debug.Log($"[ScoreManager] Player {ownerClientId} initialized with a starting score of 200 and name '{name}'.");
Scoreboard.instance.ScoreBoardItemInitializer(ownerClientId, playerScores[ownerClientId], name);
InitializePlayerScoreClientRpc(ownerClientId, name);
UpdatePlayerScoreClientRpc(ownerClientId, 200);
}
else
@ -45,7 +46,23 @@ public class ScoreManager : NetworkBehaviour
Debug.LogWarning($"[ScoreManager] Player {ownerClientId} already initialized.");
}
}
[ClientRpc]
public void InitializePlayerScoreClientRpc(ulong ownerClientId, string name)
{
if (!playerScores.ContainsKey(ownerClientId))
{
playerScores[ownerClientId] = 200;
playerNames[ownerClientId] = name;
Debug.Log($"[ScoreManager] Player {ownerClientId} initialized with a starting score of 200 and name '{name}'.");
Scoreboard.instance.ScoreBoardItemInitializer(ownerClientId, playerScores[ownerClientId], name);
Scoreboard.instance.ScoreBoardUpdaterLocal(name, playerScores[ownerClientId]);
}
else
{
Debug.LogWarning($"[ScoreManager] Player (from client) {ownerClientId} already initialized.");
}
}
public void UpdatePlayerScore(ulong ownerClientId, int newScore)
{
if (playerScores.ContainsKey(ownerClientId))
@ -67,10 +84,11 @@ public class ScoreManager : NetworkBehaviour
{
string playerName = playerNames[ownerClientId];
Debug.Log($"[ScoreManager] Received score update for Player {ownerClientId} (Name: {playerName}): {newScore}");
Scoreboard.instance.ScoreBoardUpdater(playerName, newScore);
Scoreboard.instance.ScoreBoardUpdaterLocal(playerName, newScore);
}
else
{
Debug.LogWarning($"[ScoreManager] Player name not found for Player {ownerClientId}. Cannot update scoreboard.");
}
}
@ -93,8 +111,7 @@ public class ScoreManager : NetworkBehaviour
}
}
[ServerRpc]
public void StartCrowPenaltyCoroutineServerRpc()
public void StartCrowPenaltyCoroutine()
{
StartCoroutine(ApplyCrowPenaltyCoroutine());
}

@ -27,6 +27,7 @@ public class Scoreboard : NetworkBehaviour
item.PlayerClientID = clientId;
playerItems.Add(item);
}
public void ScoreBoardUpdater(string playerName, int score)
{
if (IsServer) ScoreBoardUpdaterClientRpc(playerName, score);
@ -46,6 +47,20 @@ public class Scoreboard : NetworkBehaviour
}
SortAndUpdateScoreboard();
}
public void ScoreBoardUpdaterLocal(string playerName, int score, ClientRpcParams clientRpcParams = default)
{
foreach (var item in playerItems)
{
if (item.PlayerName.text == playerName)
{
item.PlayerScore.text = score.ToString();
if (NetworkManager.Singleton.LocalClientId == item.PlayerClientID)
item.PlayerScore.color = Color.green;
break;
}
}
SortAndUpdateScoreboard();
}
public void FinalLeaderBoard()
{
if (IsServer) FinalLeaderBoardClientRPC();

Loading…
Cancel
Save