SA-MP Forums Archive
GetPlayerScore - SetPlayerColor - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: GetPlayerScore - SetPlayerColor (/showthread.php?tid=632435)



GetPlayerScore - SetPlayerColor - Melktert - 14.04.2017

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;




Re: GetPlayerScore - SetPlayerColor - coool - 14.04.2017

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.


Re: GetPlayerScore - SetPlayerColor - Bolex_ - 14.04.2017

use if instead


Re: GetPlayerScore - SetPlayerColor - LEOTorres - 14.04.2017

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.


Re: GetPlayerScore - SetPlayerColor - lackmail - 14.04.2017

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


Re: GetPlayerScore - SetPlayerColor - Vince - 14.04.2017

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.