GetPlayerScore - SetPlayerColor
#1

There is something wrong with this coding but I cant seem to see what, eg. when the player's score is 1001 it still shows the first color for 0-500, can someone help me please?

PHP код:
public OnPlayerUpdate(playerid)
{
    if((
GetPlayerScore(playerid) >= 0) && (GetPlayerScore(playerid) <= 500))
    {
    
SetPlayerColor(playeridCOLOR_YELLOW);
    }
    else if((
GetPlayerScore(playerid) > 500) && (GetPlayerScore(playerid) <= 1000))
    {
    
SetPlayerColor(playeridCOLOR_ORANGE);
    }
    else if((
GetPlayerScore(playerid) > 1000) && (GetPlayerScore(playerid) <= 5000))
    {
    
SetPlayerColor(playeridCOLOR_RED);
    }
    else if((
GetPlayerScore(playerid) > 10000) && (GetPlayerScore(playerid) <= 15000))
    {
    
SetPlayerColor(playeridCOLOR_PURPLE);
    }
     else if((
GetPlayerScore(playerid) > 15000) && (GetPlayerScore(playerid) <= 200000))
    {
    
SetPlayerColor(playeridCOLOR_COOL);
    }
    return 
1;

Reply
#2

It is because you are using else if. It will be skip all these if the first one or one of them is true, in this case the first one is true.
Reply
#3

use if instead
Reply
#4

I wouldn't use an if or else statement at all in this instance, it's better to use switch when dealing with this. Also, there's no point of constantly calling upon the GetPlayerScore throughout your checks for the score, you can just assign it to a variable, it's a lot more efficient.

Код:
public OnPlayerUpdate (playerid)
{
	new score = GetPlayerScore (playerid);
	
	switch (score)
	{
		case 0 .. 500:
		{
			SetPlayerColor(playerid, COLOR_YELLOW); 
		}
		
		case 501 .. 1000:
		{
			SetPlayerColor(playerid, COLOR_ORANGE); 
		}
		
		case 1001 .. 5000:
		{
			SetPlayerColor(playerid, COLOR_RED); 
		}
		
		case 10001 .. 15000:
		{
			SetPlayerColor(playerid, COLOR_PURPLE); 
		}
		
		case 15001 .. 20000:
		{
			SetPlayerColor(playerid, COLOR_COOL); 
		}
	}
	
	return 1;
}
Furthermore, I wouldn't recommend using OnPlayerUpdate for this feature, unless the score of the player's on your server is going to change 30+ times every second.
Reply
#5

maybe you setting player color to yellow somewhere else because there is nothing wrong with your code i am not saying its the best way but its doing the job
Reply
#6

Stop using OnPlayerUpdate for shit like this. Score does not change by itself. It only changes when you use SetPlayerScore. It is at these locations that you should perform this check (write a function, you know, or hook SetPlayerScore), not 30 times per second in OnPlayerUpdate.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)