Need help with code
#1

Sorry for my bad english
I'm a code code ( wiki ):
Код:
public OnPlayerGiveDamage(playerid, damagedid, Float: amount, weaponid, bodypart)
{
    new string[128], victim[MAX_PLAYER_NAME], attacker[MAX_PLAYER_NAME];
    new weaponname[24];
    GetPlayerName(playerid, attacker, sizeof (attacker));
    GetPlayerName(damagedid, victim, sizeof (victim));
 
    GetWeaponName(weaponid, weaponname, sizeof (weaponname));
    format(string, sizeof(string), "%s has made %.0f damage to %s, weapon: %s, bodypart: %d", attacker, amount, victim, weaponname, bodypart);
    SendClientMessageToAll(0xFFFFFFFF, string);
    return 1;
}
Who help me a command can be turned off or on (if turned off is not displayed)
- thanks
Reply
#2

So you want a command that will toggle the damage messages?
Is this supposed to be a command that toggles it globally or personally? As in, if I toggle it, will it only turn it off for me, or for everyone?
Reply
#3

On top of the script declare a new boolean variable like this to hold the state
PHP код:
new bool:InfoMessage true;  //This is the default value of it when first decalred 
Manage that in your command:
PHP код:
CMD:toggleinfo(playeridparams[])
{
    if(
InfoMessage == false)
    {
        
SendClientMessageToAll(-1"The information message is now on!");
        
InfoMessage true//We toggle it on (set to true)
    
}
    else
    {
        
SendClientMessageToAll(-1"The information message is now off!");
        
InfoMessage false//We toggle it off (set to false)
    
}
    return 
1;

Now last put all your code in that OnPlayerGiveDamage call back inside an if statement
PHP код:
if(InfoMessage == true)
{
    
//Put your codes here

Reply
#4

Just turn off the player to use the command
Reply
#5

Quote:
Originally Posted by FailerZ
Посмотреть сообщение
On top of the script declare a new boolean variable like this to hold the state
PHP код:
new bool:InfoMessage true;  //This is the default value of it when first decalred 
Manage that in your command:
PHP код:
CMD:toggleinfo(playeridparams[])
{
    if(
InfoMessage == false)
    {
        
SendClientMessageToAll(-1"The information message is now on!");
        
InfoMessage true//We toggle it on (set to true)
    
}
    else
    {
        
SendClientMessageToAll(-1"The information message is now off!");
        
InfoMessage false//We toggle it off (set to false)
    
}
    return 
1;

Now last put all your code in that OnPlayerGiveDamage call back inside an if statement
PHP код:
if(InfoMessage == true)
{
    
//Put your codes here

Thank you
Reply
#6

Quote:
Originally Posted by OsteeN
Посмотреть сообщение
So you want a command that will toggle the damage messages?
Is this supposed to be a command that toggles it globally or personally? As in, if I toggle it, will it only turn it off for me, or for everyone?
Just turn off the player to use the command
Reply
#7

If you want it per player you need to make an array to hold the ids of the players.
Do the following changes I colored them in red:
Код:
new bool:InfoMessage[MAX_PLAYERS] = true;
Then change every variable of InfoMessage to InfoMessage[playerid]

but then you don't need to send message to all the players under onplayergivedamage.
Instead you need to create a loop to check who has it on or off.
Like this:
PHP код:
for(new iGetPlayerPoolSize(); <= ji++) //This will loop through all the players in the server
{
    if(
InfoMessage[i] == true//This will check the state of the info message for each player (i) in if statement to check if it is on (true)
    
{
        
//DO the codes here but SendClientMessage to i (the player who has the info message on)
        
SendClientMessage(i, -1"Player bla bla gave damage......"); //Send formatted message instead of this
    
}

EDIT: Don't forget to set the variable to false/true when the player disconnect because if player joined with the same id of the previous player and the prev player has it on he will have it on too so just default value when the player connects/disconnects like

Under OnPlayerConnect or OnPlayerDisconnect doesn't matter really.
PHP код:
InfoMessage[playerid] = true//True will make it show by default. change to false if you want otherwise 
Reply
#8

Quote:
Originally Posted by FailerZ
Посмотреть сообщение
If you want it per player you need to make an array to hold the ids of the players.
Do the following changes I colored them in red:
Код:
new bool:InfoMessage[MAX_PLAYERS] = true;
Then change every variable of InfoMessage to InfoMessage[playerid]

but then you don't need to send message to all the players under onplayergivedamage.
Instead you need to create a loop to check who has it on or off.
Like this:
PHP код:
for(new iGetPlayerPoolSize(); <= ji++) //This will loop through all the players in the server
{
    if(
InfoMessage[i] == true//This will check the state of the info message for each player (i) in if statement to check if it is on (true)
    
{
        
//DO the codes here but SendClientMessage to i (the player who has the info message on)
        
SendClientMessage(i, -1"Player bla bla gave damage......"); //Send formatted message instead of this
    
}

EDIT: Don't forget to set the variable to false/true when the player disconnect because if player joined with the same id of the previous player and the prev player has it on he will have it on too so just default value when the player connects/disconnects like

Under OnPlayerConnect or OnPlayerDisconnect doesn't matter really.
PHP код:
InfoMessage[playerid] = true//True will make it show by default. change to false if you want otherwise 
I do not understand the "for (new i," Add it in part
Reply
#9

Quote:
Originally Posted by Qbao
Посмотреть сообщение
I do not understand the "for (new i," Add it in part
The for is the way to initiate a loop -> Syntax: for(initial; condition; increment)
the new i part is to declare an integer varible that holds the number that will get increased/decresed depends on the increment (It will get 0 by default so no need for i = 0 and stuff)
The j is also an integer variable but this one will hold the numbers of all the player slots that you have set in your server,
We then assigned j to GetPlayerPoolSize() which is a native function that return the number of player slots as i explained above so we can go through them all
The second part is the condition here we set which values to go through. in our case it will go to all the player slots that we found in our GetPlayerPoolSize()
And last we decide what is the increment. we want it to increase (it will increase the i variable)after each iteration is done

You might wonder why we used j = GetPlayerPoolSize(); i <= j instead of just putting i <= GetPlayerPoolSize.
Infact it works this way too but the first is more effective because it doesn't get executed for each iterate

for(new i, j = GetPlayerPoolSize(); i < j; i++)
{
if(IsPlayerConnected(i)) //Add this func. I didn't add this part before. My bad this is more effective as it does check if the player is connected before checking whether the infomessage is true
{

}
}
Reply
#10

Quote:
Originally Posted by FailerZ
Посмотреть сообщение
The for is the way to initiate a loop -> Syntax: for(initial; condition; increment)
the new i part is to declare an integer varible that holds the number that will get increased/decresed depends on the increment (It will get 0 by default so no need for i = 0 and stuff)
The j is also an integer variable but this one will hold the numbers of all the player slots that you have set in your server,
We then assigned j to GetPlayerPoolSize() which is a native function that return the number of player slots as i explained above so we can go through them all
The second part is the condition here we set which values to go through. in our case it will go to all the player slots that we found in our GetPlayerPoolSize()
And last we decide what is the increment. we want it to increase (it will increase the i variable)after each iteration is done

You might wonder why we used j = GetPlayerPoolSize() instead of just putting i <= GetPlayerPoolSize.
Infact it works this way too but the first is more effective because it doesn't get executed for each iterate

for(new i, k = GetPlayerPoolSize(); i < j; i++)
{
if(IsPlayerConnected(i)) //I didn't add this part before. My bad this is more effective as it does check if the player is connected before checking whether the infomessage is true
{

}
}
Thank you, I did succeed
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)