[Tutorial] Making a ping-kicker [no lagspike kicking]
#1

Hi,

I'll explaing to you how to make a ping-kicker, some people may have some lag spikes but this system wont kick you then. Well, let's start then!

How to:

1) Open pawno.exe
2) Make a new script file (CTRL+N)
3) Delete everything inside it

4) Now, let's make the script itself.

a) First you should add this on top of your script:
pawn Код:
#include <a_samp>
Now we can use the basic script functions like SendClientMessage and so on.

b) It would be annoying if you have to change the max ping inside the script, to prevent this, lets make a define. When we use this define "maxping" in anywhere in the script, it will replace it with the number you defined it with. Example:
pawn Код:
if(playerping > maxping) // But the compiler will see it as..
if(playerping > 600) // Because we defined maxping as 600.
Also lets define maxlagtimes, it will be explained later.

So, now our script should look like this:
pawn Код:
#include <a_samp>
#define maxping 600
#define maxlagtimes 5
c) Now we have to create a function, so we can check if the player ping is over the allowed.
pawn Код:
new pLagTimes[MAX_PLAYERS]; // We make this variable to store the amount of times the system has detected lag on a player.

forward CheckPing(); // If we make a public function, we have to forward it or it wouldn't work.
public CheckPing() // This is the function, we use dont need CheckPing(playerid) because we loop the players inside the function already.
{
    new string[128], pName[MAX_PLAYER_NAME]; // We make strings to hold the message, and the player name.
    new pPing; // We make a new variable and store that players ping in it.
    // I put these outside of the loop, so the loop would be faster.
    for(new i = 0; i < MAX_PLAYERS;i++) // This loops through all player slots in the server.
    {
        if(IsPlayerConnected(i)) // If it detects a player is connected in that player slot..
        {
            pPing = GetPlayerPing(i); // We put the players ping in a variable.
            if(pPing > maxping) // If player's ping is over the allowed amount, then..
            {
                pLagTimes[i]++; // We add 1 lag time. When he reaches 5 lag times, player will get kicked.
                // This is useful, because a lag spike can happen, but when he lags 5 times I'm sure the player is lagging.

                if(pLagTimes[i] == maxlagtimes) // Now we check if players lag times have reached the maxlagtimes. You can edit maxlagtimes, so people have more chance to not be kicked.
                {
                    GetPlayerName(i, pName, sizeof(pName)); // With this we get the players name and store it in the string.
                    format(string, sizeof string, "User %s has been kicked because of high ping! (%d/%d)", pName, pPing, maxping); // Now we format the message, because we use strings in the message we have to format it.
                    SendClientMessageToAll(-1, string); // We send it to all the players, -1 is the color white.

                    Kick(i); // We kick the player.
                }
            }
        }
    }
}
d) But now we need to use it, we have the function, but we are not actually using it. For this, we do this thing:
pawn Код:
public OnFilterScriptInit() // When the filterscript starts in the game.
{
    SetTimer("Checkping", 1000*5, 1); // It will set a timer, the function CheckPing will execute in every 5 secs. 1 in the end means, that it will repeat.
    return 1;
}
e) Now the script is almost done, but think of one thing.. when a player gets kicked, then pLagTimes[playerid] will still be 5, and when the script checks the player, he gets kicked. So, how do we reset it? It's simple:
pawn Код:
public OnPlayerConnect(playerid) // When a new player connects.
{
    pLagTimes[playerid] = 0; // We set the amount of pLagTimes[playerid] to 0.
    return 1;
}
5) The script is now done, and it should look like this:
pawn Код:
#include <a_samp>
#define maxping 600
#define maxlagtimes 5

new pLagTimes[MAX_PLAYERS]; // We make this variable to store the amount of times the system has detected lag on a player.

public OnFilterScriptInit() // When the filterscript starts in the game.
{
    SetTimer("Checkping", 1000*5, 1); // It will set a timer, the function CheckPing will execute in every 5 secs. 1 in the end means, that it will repeat.
    return 1;
}

public OnPlayerConnect(playerid) // When a new player connects.
{
    pLagTimes[playerid] = 0; // We set the amount of pLagTimes[playerid] to 0.
    return 1;
}

forward CheckPing(); // If we make a public function, we have to forward it or it wouldn't work.
public CheckPing() // This is the function, we use dont need CheckPing(playerid) because we loop the players inside the function already.
{
    new string[128], pName[MAX_PLAYER_NAME]; // We make strings to hold the message, and the player name.
    new pPing; // We make a new variable and store that players ping in it.
    // I put these outside of the loop, so the loop would be faster.
    for(new i = 0; i < MAX_PLAYERS;i++) // This loops through all player slots in the server.
    {
        if(IsPlayerConnected(i)) // If it detects a player is connected in that player slot..
        {
            pPing = GetPlayerPing(i); // We put the players ping in a variable.
            if(pPing > maxping) // If player's ping is over the allowed amount, then..
            {
                pLagTimes[i]++; // We add 1 lag time. When he reaches 5 lag times, player will get kicked.
                // This is useful, because a lag spike can happen, but when he lags 5 times I'm sure the player is lagging.

                if(pLagTimes[i] == maxlagtimes) // Now we check if players lag times have reached the maxlagtimes. You can edit maxlagtimes, so people have more chance to not be kicked.
                {
                    GetPlayerName(i, pName, sizeof(pName)); // With this we get the players name and store it in the string.
                    format(string, sizeof string, "User %s has been kicked because of high ping! (%d/%d)", pName, pPing, maxping); // Now we format the message, because we use strings in the message we have to format it.
                    SendClientMessageToAll(-1, string); // We send it to all the players, -1 is the color white.

                    Kick(i); // We kick the player.
                }
            }
        }
    }
}
I hope this tutorial helped you!

Click here to download full script: Solidfiles
Reply
#2

You never mentioned defining maxlagtimes, pName is blank, and you should create your variables outside of loops.
Reply
#3

Fixed, I didn't have time before to test it, but fixed now. I'll edit the strings later when I got time.

E:// All fixed!
Reply
#4

You SendClientMessageToAll(); in a loop, that will spam the chat 500 times..
Reply
#5

The SendClientMessageToAll only gets sent when a players ping is over the allowed..
Reply
#6

Quote:
Originally Posted by Dan.
Посмотреть сообщение
The SendClientMessageToAll only gets sent when a players ping is over the allowed..
The chat will still be spammed no matter what.
Reply
#7

No it won't.. the code inside the loop will be run a lot of times each time with diffrent (i), so you're saying me, if the player doesen't have a high ping the message will still be sent? Nope..
Reply
#8

The variables should be declared in the timer but not in the loop.
Reply
#9

Thanks.
Reply
#10

check this
pawn Код:
GetPlayerName(playerid,pName,sizeof(pname));
but you didnt define "playerid" it with
pawn Код:
PingCheck();
function.it will give an error
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)