Score not updating bug fixed trial1

dev-ali
Ali Sharoz 2 weeks ago
parent 075e65f66d
commit a316c3d81b

@ -10,17 +10,27 @@ public class PlayerScoreComponent : NetworkBehaviour
public PlayerItem playerItem; public PlayerItem playerItem;
public override void OnNetworkSpawn() 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}"); 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); ScoreManager.Instance?.InitializePlayerScoreServerRpc(OwnerClientId, serverCharacter.uIStateDisplayHandler.m_UIState.playerName);
} }
serverCharacter=GetComponent<ServerCharacter>();
serverCharacter =GetComponent<ServerCharacter>();
} }
public void UpdateScore(int newScore) public void UpdateScore(int newScore)

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

@ -27,6 +27,7 @@ public class Scoreboard : NetworkBehaviour
item.PlayerClientID = clientId; item.PlayerClientID = clientId;
playerItems.Add(item); playerItems.Add(item);
} }
public void ScoreBoardUpdater(string playerName, int score) public void ScoreBoardUpdater(string playerName, int score)
{ {
if (IsServer) ScoreBoardUpdaterClientRpc(playerName, score); if (IsServer) ScoreBoardUpdaterClientRpc(playerName, score);
@ -46,6 +47,20 @@ public class Scoreboard : NetworkBehaviour
} }
SortAndUpdateScoreboard(); 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() public void FinalLeaderBoard()
{ {
if (IsServer) FinalLeaderBoardClientRPC(); if (IsServer) FinalLeaderBoardClientRPC();

Loading…
Cancel
Save