22.02.2013, 21:13
The current one isn't directly 'bugged', but doesn't function the way you want to either. Your GetPlayerID function is still fucked, as it selects ONE random number out of 500 (MAX_PLAYERS), and if that ID is not connected it'll default to '-0' (which is still 0). I'm pretty sure that most of the time the script will then use player ID 0, considering most servers barely have 50 players. Also, just because a server has players, it doesn't mean someone has to be using player id 0. I'd write your GetPlayerID function as following:
Now this function also returns -1 when there is nobody online, something your script does not check for at all! That means that it'll try to spawn a cow (and probably does at (0.0, 0.0, 0.0)) without anyone noticing! Check if this function returns -1, and if so, don't execute the code as it's useless!
Last, your OnFilterScriptExit code lacks. timer[0] will always contain the right value as it's a repeating timer, but the timer assigned to timer[1] is run only once, before a new cow is spawned again. If someone was to create a new timer in the interval between destroying the cow and spawning a new one, the timer could easily get assign the same ID that's in your timer[1] variable. You should check if timer[1] is actually running when you exit the filterscript, and if so, it should kill the cow as well (Because if you unload the script right now the cow will still remain an active object!)
pawn Код:
GetPlayerID()
{
new players[MAX_PLAYERS];
new online;
for(new i; i != MAX_PLAYERS; i++)
{
if(IsPlayerConnected(i))
{
players[online] = i;
online++;
}
}
if(online == 0) return -1;
return players[random(online)];
}
Last, your OnFilterScriptExit code lacks. timer[0] will always contain the right value as it's a repeating timer, but the timer assigned to timer[1] is run only once, before a new cow is spawned again. If someone was to create a new timer in the interval between destroying the cow and spawning a new one, the timer could easily get assign the same ID that's in your timer[1] variable. You should check if timer[1] is actually running when you exit the filterscript, and if so, it should kill the cow as well (Because if you unload the script right now the cow will still remain an active object!)