[Tutorial] How to make a simple admin jail system
#1

Hey, this is my tutorial on how to make a very simple jail system with two commands - /jail and /unjail.

I'll make it quick, so the players prison time won't save on logout, but if you would like it to I would suggest checking a tutorial on how to make a register system first. From there you could save the players current prison time so it remains when the player relogs. But for now let's begin with the tutorial:

1. We'll be using ZCMD and sscanf to create both commands, so at the top of our blank scripts we should add #include <zcmd> right underneath #include <a_samp>. It should look like this at the top of your script:

pawn Code:
#include <a_samp>
#include <zcmd>
#include <sscanf2>
You will need to download zcmd.ini from here: https://sampforum.blast.hk/showthread.php?tid=91354
.. and you can download sscanf from here: https://sampforum.blast.hk/showthread.php?tid=120356


2. Let's create the prison time variable, each player needs their own unique one so our variable will look like this: PrisonTime[MAX_PLAYERS] but thats the incorrect format for creating variables. At the top of your script underneath the includes we'll put this:

pawn Code:
new PrisonTime[MAX_PLAYERS];
This means if we want to make a command called /prisonjeff we can use the variable like so:

pawn Code:
PrisonTime[Jeff] = 100; // Sets his prison time to 100 seconds/minutes depending on what you want it to be.

3. Now we move on to creating the actual command which prisons the players of our choice. Because we're using ZCMD we should start the command off like so:

pawn Code:
CMD:jail(playerid, params[])
{
    return 1;
}
Every command needs 'return 1;' at the end of it otherwise the command will just return an error message, as if the command does not exist. The brackets { } are used to keep the script wrapped neatly or the script will get confused as what is what.

We want it so only RCON admins can use the command, so we can use the function IsPlayerAdmin for that:

pawn Code:
CMD:jail(playerid, params[])
{
    if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid,-1,"ERROR: Only administrators may use this!");
    return 1;
}
Think of '!' as opposite, so if the player is not an admin it returns a message to the player.

Now we'll make the parameters which is basically '/jail 1 10' which includes the spaces, allows us to select an individual player. So we'll start by creating the variables which store the players ID and the players prison time. From there we can use sscanf to check if the player typed in all the parameters and if not it returns a usage message:

pawn Code:
CMD:jail(playerid, params[])
{
    if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid,-1,"ERROR: Only administrators may use this!");
    new targetid, time;
    if(sscanf(params,"ui",targetid,time)) return SendClientMessage(playerid,-1,"USAGE: /jail (playerid) (minutes)");
    if(time == 0) return SendClientMessage(playerid,-1,"ERROR: You must prison the player for at least 1 minute.");
    {

    }  
    return 1;
}
NOTE: The "ui" tells us what the variables will store, so targetid will store a "u" which is a players ID. The "i" will
store an integer which is the time.

Next we'll make the global message which tells everybody the player was prisoned and actually prison the player. We'll do this by teleporting them to a special area and set their virtual world so nobody can interfer with prisoned players. Add on the string variable 'string' to the end of the previous 'new' line. We can format the string and send it to everybody online.

pawn Code:
CMD:jail(playerid, params[])
{
    if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid,-1,"ERROR: Only administrators may use this!");
    new targetid, time, string[128];
    if(sscanf(params,"ui",targetid,time)) return SendClientMessage(playerid,-1,"USAGE: /jail (playerid) (minutes)");
    if(time == 0) return SendClientMessage(playerid,-1,"ERROR: You must prison the player for at least 1 minute.");
    {
        SetPlayerPos(targetid,X,Y,Z); // Choose your own prison, find the coordinates and replace X, Y and Z.
        SetPlayerInterior(targetid,INT); // Replace INT with the interior of where your prison is.
        SetPlayerVirtualWorld(targetid,30); // Any random VW will do, so regular players cant see prisoners.
        PrisonTime[targetid] = time;
    }  
    return 1;
}
From here we will broadcast a message to every player saying who prisoned who for how long, so heres an example of the format: "Jeffrey Dodges has jailed David Berkshaw for 10 minutes."
To do this we need to store the player and the admins names in a string variable, so we add an additional two strings to the 'new' line with a length of MAX_PLAYER_NAME which is 24.

pawn Code:
CMD:jail(playerid, params[])
{
    if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid,-1,"ERROR: Only administrators may use this!");
    new targetid, time, string[128], playername[MAX_PLAYER_NAME], adminname[MAX_PLAYER_NAME];
    if(sscanf(params,"ui",targetid,time)) return SendClientMessage(playerid,-1,"USAGE: /jail (playerid) (minutes)");
    if(time == 0) return SendClientMessage(playerid,-1,"ERROR: You must prison the player for at least 1 minute.");
    {
        SetPlayerPos(targetid,X,Y,Z); // Choose your own prison, find the coordinates and replace X, Y and Z.
        SetPlayerInterior(targetid,INT); // Replace INT with the interior of where your prison is.
        SetPlayerVirtualWorld(targetid,30); // Any random VW will do, so regular players cant see prisoners.
        PrisonTime[targetid] = time;
        GetPlayerName(playerid,adminname);
        GetPlayerName(targetid,playername);
        format(string,sizeof(string),"%s has jailed %s for %i minutes.",adminname,playername,time);
        SendClientMessageToAll(0xAA3333AA,string);
    }  
    return 1;
}
As you can see, we format the string as use %s for a string and %i for an integer.
NOTE: 'sizeof(string)' means find the strings original length and set it as that, so it finds the strings length which is 128 and sets it to that.

4. Now lets create the /unjail command, like before we start with the same format of CMD: etc and add in our brackets and 'return 1;'.

pawn Code:
CMD:unjail(playerid, params[])
{
    return 1;
}
Like before lets set it so only administrators can use the command and we'll do the sscanf params, we can skip that because I already taught you how!

pawn Code:
CMD:unjail(playerid, params[])
{
    If(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid,-1,"ERROR: Only administrators may use this!");
    new targetid;
    if(sscanf(params,"u",targetid)) return SendClientMessage(playerid,-1,"USAGE: /unjail (playerid)");
    {
   
    }
    return 1;
}
There, so now we must check if the player is even prisoned and if not, we'll send the admin an error message.

pawn Code:
CMD:unjail(playerid, params[])
{
    If(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid,-1,"ERROR: Only administrators may use this!");
    new targetid;
    if(sscanf(params,"u",targetid)) return SendClientMessage(playerid,-1,"USAGE: /unjail (playerid)");
    {
        if(PrisonTime[targetid] == 0) return SendClientMessage(playerid,-1,"ERROR: That player is not prisoned!");
    }
    return 1;
}
Simple huh? Now we'll continue by setting the players prison time to 0, formatting a message and broadcasting it to everyone. Easy, since we did it before. REMEMBER: Add the new variables to our 'new' line.


pawn Code:
CMD:unjail(playerid, params[])
{
    If(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid,-1,"ERROR: Only administrators may use this!");
    new targetid, string[128], playername[MAX_PLAYER_NAME], adminname[MAX_PLAYER_NAME];
    if(sscanf(params,"u",targetid)) return SendClientMessage(playerid,-1,"USAGE: /unjail (playerid)");
    {
        if(PrisonTime[targetid] == 0) return SendClientMessage(playerid,-1,"ERROR: That player is not prisoned!");
        {
            PrisonTime[targetid] = 0; // Set it to 0.
            GetPlayerName(playerid,adminname);
            GetPlayerName(targetid,playername);
            format(string,sizeof(string),"%s has unjailed %s.",adminname,playername);
            SendClientMessageToAll(0xAA3333AA,string);
            SetPlayerPos(targetid,X,Y,Z); // Change X, Y and Z to the coords where you want them to go when they're unjailed.
            SetPlayerInterior(targetid,0);
            SetPlayerVirtualWorld(targetid,0);
        }  
    }
    return 1;
}
5. Now finally we have to make a looped timer which counts down the players prison time and when it reaches 0 it must unjail the player. We'll begin by typing this under our PrisonTime variable at the top of our script:

pawn Code:
forward PrisonCounter(playerid);
When the player is prisoned, we want it to start the counter so lets go back to our /jail command and add that in:

pawn Code:
CMD:jail(playerid, params[])
{
    if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid,-1,"ERROR: Only administrators may use this!");
    new targetid, time, string[128], playername[MAX_PLAYER_NAME], adminname[MAX_PLAYER_NAME];
    if(sscanf(params,"ui",targetid,time)) return SendClientMessage(playerid,-1,"USAGE: /jail (playerid) (minutes)");
    if(time == 0) return SendClientMessage(playerid,-1,"ERROR: You must prison the player for at least 1 minute.");
    {
        SetPlayerPos(targetid,X,Y,Z); // Choose your own prison, find the coordinates and replace X, Y and Z.
        SetPlayerInterior(targetid,INT); // Replace INT with the interior of where your prison is.
        SetPlayerVirtualWorld(targetid,30); // Any random VW will do, so regular players cant see prisoners.
        PrisonTime[targetid] = time;
        GetPlayerName(playerid,adminname);
        GetPlayerName(targetid,playername);
        format(string,sizeof(string),"%s has jailed %s for %i minutes.",adminname,playername,time);
        SendClientMessageToAll(0xAA3333AA,string);
        PrisonCounter(targetid);
    }  
    return 1;
}
Now lets head to the bottom of our script and begin the counter, we'll start by adding this in:

pawn Code:
public PrisonCounter(playerid) // REMEMBER: No ; at the end.
{
    return 1;
}

Now we check the players prison time and if its 0, we'll release them.

pawn Code:
public PrisonCounter(playerid) // REMEMBER: No ; at the end.
{
    if(PrisonTime[playerid] == 0) // If their prison time is 0/null..
    {
        SetPlayerPos(playerid,X,Y,Z); // Change X, Y and Z to the coords where you want them to go when they're unjailed.
        SetPlayerInterior(playerid,0);
        SetPlayerVirtualWorld(playerid,0);
        SendClientMessage(playerid,-1,"You have served your time in admin jail.")
    }
    else if(PrisonTime[playerid] => 1) // If their prison time is the bigger or equal to 1..
    {

    }
    return 1;
}
Simple, like the unjail command. Now we'll say if their prison time is bigger/equal to 1 then we'll loop the timer but we'll take a minute from their time. So eventually it ticks down to 0, where they get released. We

pawn Code:
public PrisonCounter(playerid) // REMEMBER: No ; at the end.
{
    if(PrisonTime[playerid] == 1) // If their prison time is 1..
    {
        SetPlayerPos(playerid,X,Y,Z); // Change X, Y and Z to the coords where you want them to go when they're unjailed.
        SetPlayerInterior(playerid,0);
        SetPlayerVirtualWorld(playerid,0);
        PrisonTime[playerid] = 0;
        SendClientMessage(playerid,-1,"You have served your time in admin jail.")
    }
    else if(PrisonTime[playerid] => 2) // If their prison time is the bigger or equal to 1..
    {
       
        SetTimerEx(PrisonCounter(playerid),60000,false,"u",playerid); // 60,000 is 60 seconds.
        PrisonTime[playerid] -= 1; // Takes 1 from the current value, so if their prison time is 10 it sets it to 9.
    }
    return 1;
}
So theres my tutorial on a very simplistic jail system, I hope this helps and be sure to comment if you have any additional questions to ask. Enjoy!
Reply
#2

Nice tutorial
Reply
#3

Use [Pawn] to avoid this mess. Please.
Reply
#4

I thought it was [pawno] when I posted it, so I immediately edited, but thanks anyways!
Reply
#5

nice
Reply
#6

Tutorial
The Tutorial is nice and help fully for New scripters that makes RP Game mode. +Rep
EDIT: Havr you think on if the player relog? Becuse the most newbies doesnt have a Automatic Position Saver and loader
Reply
#7

Quote:
Originally Posted by Meller
View Post
Tutorial
The Tutorial is nice and help fully for New scripters that makes RP Game mode. +Rep
EDIT: Havr you think on if the player relog? Becuse the most newbies doesnt have a Automatic Position Saver and loader
Like I said, they'll need to check a tutorial on a register/login system first. From there they could save the players prison time. All it would be is scripting a little beneath OnPlayerConnect, making it so if the players prison time is 1+ then it would send em to prison and begin the timer.
Reply
#8

Quote:
Originally Posted by jackx3rx
View Post
Like I said, they'll need to check a tutorial on a register/login system first. From there they could save the players prison time. All it would be is scripting a little beneath OnPlayerConnect, making it so if the players prison time is 1+ then it would send em to prison and begin the timer.
Oh sorry didnt saw that.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)