Vice City: Multiplayer

Server Development => Scripting and Server Management => Topic started by: Spice on Jul 26, 2015, 07:45 AM

Title: Again problem with timer
Post by: Spice on Jul 26, 2015, 07:45 AM
i was trying to do that when 1 hour pas all players in my server get healed
but problem is it heals all players  after 5 to 10 seconds here is code

function healallp( )
{
 Message("** All Players in server have been healed after 1 hour.");
if( GetPlayers() > 0 )
{
 for(local i=0; i<GetPlayers(); i++ )
 {
   local player = FindPlayer( i );
    if ( ( status[ player.ID ].IsReg == true ) && ( status[ player.ID ].IsLogged == true) )
   {
PrivMessage( player, "You have been healed.");
player.Health = 100;
    }
  }
}
}

Since this function works fine but main problem is in timer
i add it
 on Script load
NewTimer( "healallp", 3600000, 0 );
please tell me where i am doing it wrong.?...THANKX
Title: Re: Again problem with timer
Post by: Spice on Jul 26, 2015, 08:24 AM
i solved it my self but it gives an error if a player come then he quit..
for example there were two players in server
1st id = 0
2nd id = 1
if 1st player quit then after some time when the function is called again it says id does not exsits...
Title: Re: Again problem with timer
Post by: MacTavish on Jul 26, 2015, 08:35 AM
I am not sure about the problem but you can try this

function healallp( )
{
 Message("** All Players in server have been healed after 1 hour.");
if( GetPlayers() ==0 ) return;
else
{
 for(local i=0; i<GetMaxPlayers(); i++ )
 {
   local player = FindPlayer( i );
    if ( player && player.IsSpawned)
   {
 PrivMessage( player, "You have been healed.");
 player.Health = 100;
    }
  }
}
}

Just change your function with this

You wasnt checking that the player is still in server and Spawned
Title: Re: Again problem with timer
Post by: DizzasTeR on Jul 26, 2015, 08:42 AM
@Beztone, Its not about spawn, its about GetPlayers(), it returns the number of people playing, which is not in sequence. For example:

player1 joined ID: 0

player2 joined ID: 1

player3 joined ID: 2


You used:
for( local i = 0; i < GetPlayers(); i++ ){ local plr = FindPlayer( i ); if( plr ) plr.Health = 100; }
it will work, now player2 leaves so its like,

player1 ID: 0

player3 ID:2


and you do the same code, the getplayers will return 2, a loop of 2 will be called, and it does FindPlayer(0) it will work because that exists, when it does FindPlayer(1) since that ID doesn't exist it won't work, and the loop will end, leading the last player to not get healed. All he had to do was change GetPlayers to GetMaxPlayers.
Title: Re: Again problem with timer
Post by: MacTavish on Jul 26, 2015, 08:45 AM
I changed the loop also
Title: Re: Again problem with timer
Post by: Spice on Jul 26, 2015, 08:54 AM
Thankxs to all you guys it actually worked and i got the point....