[Tutorial] How to make a simple server time system

Started by SA:MP, May 05, 2023, 05:48 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

SA:MP

[Tutorial] How to make a simple server time system

Hello this is my first tutorial Iam just gonna explain this fast (if there's anybody who needs this). This tutorial is about time on your server (not weather), how to make the server to automatically set the server time as the clock on your server...?

First you need to make a timer that will set server time every 1 hour (if you have a payday sistem which repeats every hour you can just put everything there):




Code:

forward Time(); // you should put this timer with the other timers in you server but it is not really important
public Time()
{
    // This is what will happen when timer's time run out
}

Second you need to get your server (clock) time inside that timer like this:




Code:

forward Time();
public Time()
{
    new h; // here we make a new variable in which we will store that time we get from server
    gettime(h); // here we store it to variable 'h'
}

Then we need to put server time as the variable 'h', but if in your country currently is winter probbably it is night at like 6 o'clock so you can make that too like this:




Code:

forward Time();
public Time()
{
    new h;
    gettime(h);
    if(h == 18 || h == 19) // here we check if it is 19h or 18h if it is we will set the world time to current time plus 5 hours so it will be night and so we are goin on with this below
    {
         SetWorldTime(h+5);
    }
    else if(h == 20 || h == 21)
    {
        SetWorldTime(h+4);
    }
    else if(h == 22 || h == 23)
    {
        SetWorldTime(h+2);
    }
    else   // here we check if current time is not 18,19,20,21,22,23 (everything that we haven't specified before) and if time is like 9h AM it will set your world time to 9
    {
        SetWorldTime(h);
    }
}

And one last thing is to actually set the timer and make it repeating every 1h (put this in your on gamemode init so it will be set as server is started):




Code:

SetTimer("Time", 3600000, true); // this is setting the public timer which you made before, 3600000 is the one hour in milliseconds and true means that timer will be repeated every time when interval get to 0 and your interval is 1hour (3600000)

This is my first tutorial and I haven't spend much time on it so don't hate a lot. And if there's mistakes type below

Source: [Tutorial] How to make a simple server time system