SA-MP Forums Archive
Problems with visit counter - 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)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Problems with visit counter (/showthread.php?tid=107933)



Problems with visit counter - JonyAvati - 11.11.2009

Description of the problem
I've made a visit counter, everytime somebody connects, 1 visit is added, but instead of adding 1 per connection, it adds 6.

Codes

SaveVisits stock
pawn Код:
stock SaveVisits()
{
    format(file,sizeof(file),"visits.ini");
    dini_IntSet(file,"visits", GetVisits());
    return 1;
}
Public loaded with a timer in OnGameModeInit :
pawn Код:
SetTimerEx("CheckVisits",9000,true, "d");
pawn Код:
forward CheckVisits(playerid);
public CheckVisits(playerid)
{
    format(file,sizeof(file),"visits.ini");
    AddVisits(dini_Int(file, "visits"));
    return 1;
}
This at OnPlayerConnect
pawn Код:
AddVisits(1);
And this is the AddVisits stock:
pawn Код:
stock AddVisits(money)
{
    Visits = Visits + money;
    return Visits;
}
I hope somebody will be able to help me, thank you!
PS : May I add the AddVisits(1); in the command /login instead of OnPlayerConnect? Maybe SA:MP is buggy, so it's like 6 connections? Tested in /login but the same result, but it just adds the double.


Re: Problems with visit counter - JonyAvati - 11.11.2009

Administrators / moderators please don't erase this post, just lock it.
_________________________________________________

Problem solved
I'll give you the bug fix, this could be used as an example for anything else...
_________________________________________________

The bug was, that in "CheckVisits", I did
pawn Код:
AddVisits(dini_Int(file, "visits"));
So it added more visits than the server has, and keeped the visits that where in visits file, so for example, if there are 3 visits, the script will count 3 more, then 6. The solution was set the visits to 0 after adding more:

pawn Код:
format(file,sizeof(file),"visits.ini");
ResetVisits();
AddVisits(dini_Int(file, "visits"));
ResetVisits is

pawn Код:
stock SaveVisits()
{
    format(file,sizeof(file),"visits.ini");
    dini_IntSet(file,"visits", GetVisits());
    return 1;
}
_________________________________________________


Re: Problems with visit counter - Daren_Jacobson - 11.11.2009

well, you beat me to the answer, i saw that you were calling CheckVisits every nine seconds, and adding whatever was in the file to the existing number.