SA-MP Forums Archive
What's wrong with my script? - 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: What's wrong with my script? (/showthread.php?tid=611537)



What's wrong with my script? - Zeus666 - 07.07.2016

Код:
forward AjailCount(playerid);

public AjailCount(playerid)
{
            if(pInfo[playerid][pJailed] != 0)
    {
            pInfo[playerid][pJailed]--;
            if(pInfo[playerid][pJailed] == 0)
        {
            SendClientMessage(playerid, COLOR_RED, "Your time in Ajail is over..");
            SetPlayerInterior(playerid, 0);//You can change interior
            SetPlayerVirtualWorld(playerid, 0); //You can change virtual world
            SetPlayerPos(playerid, 1529.6,-1691.2,13.3);
        }
    }
            return 1;
}


CMD:ajail(playerid, params[])
{
  new targetid, time, reason[64];
  new string[128], adminname[64], targetname[64];
  if(pInfo[playerid][pAdminLevel] < 1) return SendClientMessage(playerid, COLOR_RED, "ERROR: You are not Admin");
  if(sscanf(params, "uds[64]", targetid, time, reason)) return SendClientMessage(playerid, -1, "USAGE: /ajail <playerid> <time> <reason>");
  if(time == 0) return SendClientMessage(playerid, COLOR_RED, "ERROR: Minimum 1 minute !");

  GetPlayerName(targetid, targetname, MAX_PLAYER_NAME);
  GetPlayerName(playerid, adminname, MAX_PLAYER_NAME);

  pInfo[playerid][pJailed] = 1;
  SetPlayerPos(targetid, 346.870025, 309.259033, 999.155700);
  SetPlayerVirtualWorld(targetid, 2);
  SetPlayerInterior(targetid,6);
  pInfo[playerid][pJailed] = time;
  format(string, sizeof(string), "Administrator %s ajailed %s for %d minutes. Reason: %s", adminname, targetname, time, reason);
  SendClientMessageToAll(COLOR_RED, string);
  playertimer[targetid] = SetTimerEx("AjailCount", 60000, 1, "i", playerid);
  return 1;
}

CMD:unjail(playerid, params[])
{
    new targetid, string[128], adminname[64], targetname[64];
    if(pInfo[playerid][pAdminLevel] < 1) return SendClientMessage(playerid, COLOR_RED, "ERROR: You are not admin!");
    if(sscanf(params, "u", targetid)) return SendClientMessage(playerid, -1, "USAGE: /unajail <playerid>");
    if(pInfo[targetid][pJailed] == 0) return SendClientMessage(playerid, COLOR_RED, "ERROR: Player not in ajail right now!");
    
    
    GetPlayerName(playerid, adminname, MAX_PLAYER_NAME);
    GetPlayerName(targetid, targetname, MAX_PLAYER_NAME);
    
    format(string, sizeof(string), "Administrator %s took %s out of Admin Jail.", adminname, targetname);
    SendClientMessage(targetid,-1,string);
    SendMessageToAllAdmins(string, -1);
    SetPlayerPos(playerid, 1529.6,-1691.2,13.3);
	SetPlayerInterior(playerid, 0);
	SetPlayerVirtualWorld(playerid, 0);
    pInfo[playerid][pJailed] = 0;
    return 1;

}
There's something wrong with the timer. If I ajail for 1 minute, it gives only about 40 seconds.


Re: What's wrong with my script? - Vince - 07.07.2016

I don't see any KillTimer statements.


Re: What's wrong with my script? - Alpay0098 - 07.07.2016

Try using this system in seconds instead of using minutes.
And also don't forget to kill the timer in AjailCount function.


Re: What's wrong with my script? - Stinged - 07.07.2016

Why do you create your arrays/strings before checking for the admin stuff and the syntax (sscanf)?
You're wasting a lot of memory if the command returns something before even using them...


Re: What's wrong with my script? - Shveps - 07.07.2016

Hi, try to use

pInfo[playerid][pJailed] = time*60;

playertimer[targetid] = SetTimerEx("AjailCount", pInfo[playerid][pJailed]*1000, false, "i", playerid)


Re: What's wrong with my script? - Sjn - 08.07.2016

Quote:
Originally Posted by Stinged
Посмотреть сообщение
Why do you create your arrays/strings before checking for the admin stuff and the syntax (sscanf)?
You're wasting a lot of memory if the command returns something before even using them...
Correct me if I am wrong, but wouldn't the local variables get destroyed once the function exits returning a value? Even tho I do the same as you just stated. But just wondering, because there are some people who use if-else instead of directly returning error/syntax in the command and I doubt they have any memory problem with it.


Re: What's wrong with my script? - Zeus666 - 08.07.2016

Quote:
Originally Posted by Alpay0098
Посмотреть сообщение
Try using this system in seconds instead of using minutes.
And also don't forget to kill the timer in AjailCount function.
I'm using miliseconds (60000)
.



So I've add

Код:
public AjailCount(playerid)
{
            if(pInfo[playerid][pJailed] != 0)
    {
            pInfo[playerid][pJailed]--;
            if(pInfo[playerid][pJailed] == 0)
        {
            KillTimer(playertimer[playerid]);
            SendClientMessage(playerid, COLOR_RED, "Your time in Ajail is over..");
            SetPlayerInterior(playerid, 0);//You can change interior
            SetPlayerVirtualWorld(playerid, 0); //You can change virtual world
            SetPlayerPos(playerid, 1529.6,-1691.2,13.3);
        }
    }
            return 1;
}


CMD:unjail(playerid, params[])
{
    new targetid, string[128], adminname[64], targetname[64];
    if(pInfo[playerid][pAdminLevel] < 1) return SendClientMessage(playerid, COLOR_RED, "ERROR: You are not admin!");
    if(sscanf(params, "u", targetid)) return SendClientMessage(playerid, -1, "USAGE: /unajail <playerid>");
    if(pInfo[targetid][pJailed] == 0) return SendClientMessage(playerid, COLOR_RED, "ERROR: Player not in ajail right now!");
    
    
    GetPlayerName(playerid, adminname, MAX_PLAYER_NAME);
    GetPlayerName(targetid, targetname, MAX_PLAYER_NAME);
    
    format(string, sizeof(string), "Administrator %s took %s out of Admin Jail.", adminname, targetname);
    SendClientMessage(targetid,-1,string);
    SendMessageToAllAdmins(string, -1);
    KillTimer(playertimer[targetid]);
    SetPlayerPos(playerid, 1529.6,-1691.2,13.3);
	SetPlayerInterior(playerid, 0);
	SetPlayerVirtualWorld(playerid, 0);
    pInfo[playerid][pJailed] = 0;
    return 1;

}
it is correct?


I've add on my script [pJailed] function and it's showing on mysql database how much time he has anymore. Not if he is jailed (1) or not jailed (0)


Re: What's wrong with my script? - Sjn - 08.07.2016

Because, you used [pJailed] to hold the jail time. Look at your code again, first you are setting it's value to 1 then again setting the jail's time in it.

Plus, you do not really need to set it's value to either 0 or 1 to indicate that the player is jailed. Just simply check with a logic: If the time value that it holds is higher than 0, player is still in jail. If the value is 0 then the player is not jailed. Simple.

I would recommend you to use gettime() instead of timer if you are not showing the estimated jail time in a textdraw/gametext.


Re: What's wrong with my script? - Stinged - 08.07.2016

Quote:
Originally Posted by Sjn
Посмотреть сообщение
Correct me if I am wrong, but wouldn't the local variables get destroyed once the function exits returning a value? Even tho I do the same as you just stated. But just wondering, because there are some people who use if-else instead of directly returning error/syntax in the command and I doubt they have any memory problem with it.
It's the same thing with if-else.

This is better
Код:
if (!IsPlayerAdmin(playerid))
    return SendClientMessage(...);

new string[64];
format(string...);

or

if (!IsPlayerAdmin(playerid))
{
    SendClientMessage(...);
}
else
{
    new string[64];
    format(string...);
}
than this
Код:
new sting[64];
if (!IsPlayerAdmin(playerid))
    return SendClientMessage(...);

format(string...);

or 

new string[64];
if (!IsPlayerAdmin(playerid))
{
    SendClientMessage(...);
}
else
{
    format(string...);
}
Basically in the first one, you're only creating string[64] when the player passed the IsPlayerAdmin check.
While in the second one, you'd be creating either way, which means it would be wasted if the player isn't an admin, because there's no actual use for it.


Re: What's wrong with my script? - Zeus666 - 08.07.2016

Quote:
Originally Posted by Sjn
Посмотреть сообщение
Because, you used [pJailed] to hold the jail time. Look at your code again, first you are setting it's value to 1 then again setting the jail's time in it.

Plus, you do not really need to set it's value to either 0 or 1 to indicate that the player is jailed. Just simply check with a logic: If the time value that it holds is higher than 0, player is still in jail. If the value is 0 then the player is not jailed. Simple.

I would recommend you to use gettime() instead of timer if you are not showing the estimated jail time in a textdraw/gametext.

I will create a gametext to show timeleft.


But i don't know how to fix this incorrect jailtime. I want also to make a jail log where it should show for every player

[DATE / HOUR ] - [AJAILED BY] - [REASON]