Server lag caused by timers..
#1

Ok, I decided to add some freeroam features to my server, /rainbow and /autofix
idk if thats caused by my internet (its a lil bit slow atm) or the server lags (its hosted in ultra h though)
however, i made 2 timers with 700 intervals, the first looping all online players and if the players activated the /rainbow, it simple change his vehicle colour, the other does the same but repairs the car instead.
my ping is 311 in the server (canada hosted -.-)
idk if the server lagging or its my internet, can someone tell me?
heres my timers code :S
PHP код:
forward AutoFixCars();
public 
AutoFixCars()
{
    for(new 
0GetPlayerPoolSize(); <= ji++) 
    {
        if(
AutoFix[i] == && IsPlayerInAnyVehicle(i)) RepairVehicle(GetPlayerVehicleID(i));
    }
}
forward RainbowCars();
public 
RainbowCars()
{
    for(new 
0GetPlayerPoolSize(); <= ji++) 
    {
        if(
IsPlayerInAnyVehicle(i) && Rainbow[i] == 1)
        {
            new 
veh GetPlayerVehicleID(i);
            
ChangeVehicleColor(vehrandom(256), random(256));
        }
    }

P.S.: I was online alone in the server so, Not sure what happens if theres 40 players online lol..
Reply
#2

First of all, foreach.
Secondly, impossible to tell if it's your internet, the host, or the script that causes the lag.
You have to check. Try playing without the timers and see if the lag still exists. If it is, it's not the script.
If it doesn't, it's the script. And by 700 interval you mean 700ms? That's unnecessarily fast.
Even in the extreme case that a car is about to blow up, it will take it 3 seconds to explode. So you can set the repair timer interval to 2-2.5 seconds.
And what's the point of a color changing every 700ms, make each color stay for a while, and change every 5-6 seconds.

With that said I find it hard to believe that 700ms timer causes such aggressive lag. It should affect but not that much.
Reply
#3

Use one timer or you can add this into OnPlayerUpdate
pawn Код:
forward AutoFixCars();
public AutoFixCars()
{
    static veh;
    for(new i = GetPlayerPoolSize(); i > -1; i--)
    {
        if(AutoFix[i] == 1 || Rainbow[i] == 1)
        {
            veh = GetPlayerVehicleID(i);
            if(veh)
            {
                if(AutoFix[i] == 1)
                    RepairVehicle(veh);

                if(Rainbow[i] == 1)
                    ChangeVehicleColor(veh, random(256), random(256));
            }
        }
    }
}
+

use https://sampforum.blast.hk/showthread.php?tid=271129 and you will see whats wrong

or

https://sampforum.blast.hk/showthread.php?tid=292130
Reply
#4

Ok I used the code you posted above instead of 2 timers, also it was caused by my internet (server wasn't lagging at all ), but may I know why did you replaced:
PHP код:
 for(new 0GetPlayerPoolSize(); <= ji++) 
with

PHP код:
 for(new GetPlayerPoolSize(); > -1i--) 
Is it a kind of optimization or what?
Reply
#5

Yes one variable 'new' instead of two, in this case you don't need loop 0,1,2,3... so its going down ...3,2,1,0
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)