26.06.2015, 08:43
(
Last edited by Youssef221; 26/06/2015 at 09:19 AM.
)
Hey guys, I am gonna tell you how to create a simple spawn protection.
1- We need to create a variable called:
2- Go to OnPlayerSpawn:
We will see this:
Now if the players kept shooting at the spawn protected player, his health won't decrease, and they will call him a hacker, and other known things will happen, to prevent this, we will set the spawn protected player's virtual world randomly:
this means that the players world can be (example), if your id is 1, then your virtual world is 1.
Now let's make a timer called: Spawnprotect, we will use this to start and end the player's spawn protection, this is where we will use the Spawntimer variable:
Now we will let the player know that he is spawn protected by sending him a message:
OPTIONAL: we will also send a gametext to the player if he didn't see the message:
3- "Spawnprotect" functions:
Since we made a timer called: Spawnprotect, when it ends, we will need to add some functions (globally we need to turn off the spawn protection).
First we will need to create a forward for the timer:
NOTE: the forward needs to have ";" at the end, otherwise it will return some errors.
Secondly, we will make a callback called: public Spawnprotect, this will be used when the timer ends:
then, we will set the player's variable and we will send him a message with gametext:
4- Killing The Timer:
Finally, when the player disconnects, we will need to kill the timer, so the timer doesn't bug to other players,
Go to OnPlayerDisconnect and write this code (NOTE: we will again use the Spawntimer[playerid] to kill the timer because we have made it equal to the "Spawnprotect" timer in OnPlayerSpawn):
If you wanna ask a question, please PM me.
Special Thanks To: Infinity and Sellize.
NOTE: this is my first tutorial.
1- We need to create a variable called:
pawn Code:
new Spawntimer[MAX_PLAYERS]; // We will use this for killing the spawn timer, MAX_PLAYERS is used because it will be like a variable for a player not a global variable
We will see this:
pawn Code:
public OnPlayerSpawn(playerid)
{
return 1;
}
pawn Code:
public OnPlayerSpawn(playerid)
{
new world[MAX_PLAYERS]; // If we wanna set the player's world, we will need to create a variable
called "world" to use it for setting the player's world.
world[playerid] = playerid; // this means that the value of player's variable "world" is equal to his id
SetPlayerVirtualWorld(playerid, world[playerid]); // we will set the player's world with the player's "world" variable
if(world[playerid] == 0) // This means if the player's "world" variable is equal to 0
{
SetPlayerVirtualWorld(playerid, 151); // We will the player's world to that number (since my max slots in my
server is 150, I add +1 and then I put the result in the function, example, if your max slots is 50, Set the player's world to 51.
}
SetPlayerHealth(playerid, 100000); // this will make the player's health unlimited (100000) on spawn protection
return 1;
}
Now let's make a timer called: Spawnprotect, we will use this to start and end the player's spawn protection, this is where we will use the Spawntimer variable:
pawn Code:
public OnPlayerSpawn(playerid)
{
new world[MAX_PLAYERS];
world[playerid] = playerid;
SetPlayerVirtualWorld(playerid, world[playerid]);
if(world[playerid] == 0)
{
SetPlayerVirtualWorld(playerid, 151);
}
SetPlayerHealth(playerid, 100000);
Spawntimer[playerid] = SetTimerEx("Spawnprotect", 5000, false, "d", playerid); // 5000 = 5 Seconds, False = Timer doesn't get repeated, "d" is the format syntax, we use "d" if its for playerid, playerid is... you.
return 1;
}
pawn Code:
public OnPlayerSpawn(playerid)
{
new world[MAX_PLAYERS];
world[playerid] = playerid;
SetPlayerVirtualWorld(playerid, world[playerid]);
if(world[playerid] == 0)
{
SetPlayerVirtualWorld(playerid, 151);
}
SetPlayerHealth(playerid, 100000);
Spawntimer[playerid] = SetTimerEx("Spawnprotect", 5000, false, "d", playerid);
SendClientMessage(playerid, -1, "You are under spawn protection for 5 seconds."); // This will send a white message to the player, saying "You are under spawn protection for 5 seconds."
return 1;
}
pawn Code:
public OnPlayerSpawn(playerid)
{
new world[MAX_PLAYERS];
world[playerid] = playerid;
SetPlayerVirtualWorld(playerid, world[playerid]);
if(world[playerid] == 0)
{
SetPlayerVirtualWorld(playerid, 151);
}
SetPlayerHealth(playerid, 100000);
Spawntimer[playerid] = SetTimerEx("Spawnprotect", 5000, false, "d", playerid);
SendClientMessage(playerid, -1, "You are under spawn protection for 5 seconds.");
GameTextForPlayer(playerid, "~g~Spawn Protection On", 3000, 4); // this will send a green game text (because of the ~g~) to the player and it will only last 3000 miliseconds (3 seconds), with the game text type 4
return 1;
}
Since we made a timer called: Spawnprotect, when it ends, we will need to add some functions (globally we need to turn off the spawn protection).
First we will need to create a forward for the timer:
pawn Code:
forward Spawnprotect(playerid);
Secondly, we will make a callback called: public Spawnprotect, this will be used when the timer ends:
pawn Code:
forward Spawnprotect(playerid);
public Spawnprotect(playerid)
{
return 1;
}
pawn Code:
foward Spawnprotect(playerid);
public Spawnprotect(playerid)
{
SetPlayerVirtualWorld(playerid, 0); // This will set the player's world to the default (0)
SendClientMessage(playerid, -1, "You are now no longer under spawn protection."); // This will send the player a white message.
GameTextForPlayer(playerid, "~r~Spawn Protection Off", 3000, 4); // This will send the player a red game text (because of ~r~) and it will last 3000 miliseconds (3 seconds) with game text type 4.
SetPlayerHealth(playerid, 100); // We will give back the player's normal health (100)
return 1;
}
Finally, when the player disconnects, we will need to kill the timer, so the timer doesn't bug to other players,
Go to OnPlayerDisconnect and write this code (NOTE: we will again use the Spawntimer[playerid] to kill the timer because we have made it equal to the "Spawnprotect" timer in OnPlayerSpawn):
pawn Code:
KillTimer(Spawntimer[playerid]);
Special Thanks To: Infinity and Sellize.
NOTE: this is my first tutorial.