new Spawned[SCRIPT_MAX_PLAYERS];
new Float:ScriptHealth[SCRIPT_MAX_PLAYERS];
Check_Health(playerid)
{
new Float:fHealth = GetPlayerHealth(playerid);
if (fHealth > ScriptHealth[playerid])
return 0;
return 1;
}
public OnPlayerSpawn(playerid)
{
ScriptHealth[playerid] = 50;
SetPlayerHealth(playerid, ScriptHealth[playerid]);
Spawned[playerid] = 1;
return 1;
}
public OnPlayerUpdate(playerid)
{
if (Spawned[playerid] == 1 && !Check_Health())
{
Kick(playerid);
}
return 1;
}
|
I highly doubt of this code kicking a player falsely.
'Spawned[playerid] = 1' and 'SetPlayerHealth' are getting called on the same callback at the same time, so you would not get kicked. |
new Spawned[SCRIPT_MAX_PLAYERS];
new Float:ScriptHealth[SCRIPT_MAX_PLAYERS];
new bool:HasBeenSynced[SCRIPT_MAX_PLAYERS];
Check_Health(playerid)
{
new Float:fHealth = GetPlayerHealth(playerid);
if(!HasBeenSynced[playerid])
{
if(fHealth <= ScriptHealth[playerid]) HasBeenSynced[playerid] = true;
return 1;
}
if(fHealth > ScriptHealth[playerid]) return 0;
return 1;
}
public OnPlayerSpawn(playerid)
{
HasBeenSynced[playerid] = false;
ScriptHealth[playerid] = 50;
SetPlayerHealth(playerid, ScriptHealth[playerid]);
Spawned[playerid] = 1;
return 1;
}
public OnPlayerUpdate(playerid)
{
if(Spawned[playerid] == 1 && !Check_Health())
{
Kick(playerid);
}
return 1;
}
|
The Spawned[playerid] = 1 is being called AFTER their health is being set
|
|
The ~xMS is a timeline of the server executing code. (milliseconds) ~10ms | Server -> Client: I'm setting your health to 50.0 ~10ms | Server -> Spawned[playerid] = 1; ~11ms | -> Server now calls a timer that checks if the GetPlayerHealth()> ScriptHealth[playerid], but the server still hasn't received a response from the client that their health is now 50.0. ~11ms | -> Server kicks player. (One second later) ~1011ms | Client -> Server: Okay, my health is 50.0 now |
/*
Dont use custom MAX_PLAYERS definitions because (if you use include from other people) the value of MAX_PLAYERS won't change accordingly
Undef it and redefine it with your new value as the old value isn't needed anymore
*/
#undef MAX_PLAYERS
#define MAX_PLAYERS 50
new
Float: gScriptHealth[MAX_PLAYERS],
bool: gSyncHealth[MAX_PLAYERS char]
;
Check_Health(playerid) {
new
Float: health
;
if(GetPlayerHealth(playerid, health)) {
if(!gSyncHealth{playerid}) {
if(fHealth <= gScriptHealth[playerid]) {
gSyncHealth{playerid} = true;
}
return true;
}
return (fHealth <= gScriptHealth[playerid]);
}
return false;
}
stock SetPlayerHealthSync(playerid, Float: health) {
if(SetPlayerHealth(playerid, health) {
gScriptHealth[playerid] = health;
gSyncHealth{playerid} = false;
return true;
}
return false;
}
//OnPlayerSpawn
SetPlayerHealthSync(playerid, 50.0);
//OnPlayerUpdate
if(!Check_Health()) {
Kick(playerid);
}