[Tutorial] Simple Spawn Protection
#1

Hey guys, I am gonna tell you how to create a simple spawn protection.

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
2- Go to OnPlayerSpawn:

We will see this:
pawn Code:
public OnPlayerSpawn(playerid)
{
     return 1;
}
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:

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;
}
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:

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;
}
Now we will let the player know that he is spawn protected by sending him a message:

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;
}
OPTIONAL: we will also send a gametext to the player if he didn't see the message:

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;
}
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:

pawn Code:
forward Spawnprotect(playerid);
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:

pawn Code:
forward Spawnprotect(playerid);
public Spawnprotect(playerid)
{
return 1;
}
then, we will set the player's variable and we will send him a message with gametext:

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;
}
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):

pawn Code:
KillTimer(Spawntimer[playerid]);
If you wanna ask a question, please PM me.

Special Thanks To: Infinity and Sellize.

NOTE: this is my first tutorial.
Reply
#2

I don't particularly agree with the method of spawn protection (spawning them in a virtual world), but that's just up to preferences.

What I do deem wrong, however, is that you use a random number generator to decide which virtual world the player is sent off to. Random numbers are not guaranteed unique, meaning multiple players could end up in the same virtual world, which is obviously not what you want. A simpler and guaranteed unique number would be to simply use the player's ID.
Reply
#3

Misunderstood at that time..
Reply
#4

Quote:
Originally Posted by Infinity
View Post
I don't particularly agree with the method of spawn protection (spawning them in a virtual world), but that's just up to preferences.

What I do deem wrong, however, is that you use a random number generator to decide which virtual world the player is sent off to. Random numbers are not guaranteed unique, meaning multiple players could end up in the same virtual world, which is obviously not what you want. A simpler and guaranteed unique number would be to simply use the player's ID.
I agree, eventhough it's a 1/40 chance you will end up in the same VW, you shouldn't take the risk. You could like Infinity said use the player's ID. Also, you should use a more general type of spawn protection, i.e. setting their health to a high value at each update or use a timer to save resources.
Other than that, good tutorial and nice effort
Reply
#5

@Infinity, @Sellize, you mean Like this?:
pawn Code:
world = playerid
?
Reply
#6

Quote:
Originally Posted by Youssef221
View Post
@Infinity, @Sellize, you mean Like this?:
pawn Code:
world = playerid
?
More like

PHP Code:
public OnPlayerSpawn(playerid)
{
     
SetPlayerVirtualWorld(playeridplayerid); // Set his VW to his own playerid
     
Spawntimer[playerid] = SetTimerEx("Spawnprotect"5000false"d"playerid);
     
SendClientMessage(playerid, -1"You are under spawn protection for 5 seconds.");
     
GameTextForPlayer(playerid"~g~Spawn Protection On"30004); // 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;

Also.. I found something weird in your code.
Why would you check if world == 0? You could just do this to prevent the world from being 0:

PHP Code:
public OnPlayerSpawn(playerid)
{
     
SetPlayerVirtualWorld(playeridrandom(39)+1); // Add 1 to the random number to prevent it from being 0
     
Spawntimer[playerid] = SetTimerEx("Spawnprotect"5000false"d"playerid);
     
SendClientMessage(playerid, -1"You are under spawn protection for 5 seconds.");
     
GameTextForPlayer(playerid"~g~Spawn Protection On"30004); // 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;

Reply
#7

Because the default world is 0, and still:

pawn Code:
SetPlayerVirtualWorld(playerid, random(39)+1);
you said earlier:

Quote:
Originally Posted by Sellize
View Post
I agree, eventhough it's a 1/40 chance you will end up in the same VW, you shouldn't take the risk. You could like Infinity said use the player's ID. Also, you should use a more general type of spawn protection, i.e. setting their health to a high value at each update or use a timer to save resources.
Other than that, good tutorial and nice effort
so I am checking if the player's world has gone to 0, it will reset his world..

instead of using random generator like you and infinity said.
Reply
#8

Quote:
Originally Posted by Youssef221
View Post
Because the default world is 0, and still:

pawn Code:
SetPlayerVirtualWorld(playerid, random(39)+1);
you said earlier:



so I am checking if the player's world has gone to 0, it will reset his world..

instead of using random generator like you and infinity said.
Yes but you are unnecessarily checking that as you could just use

pawn Code:
SetPlayerVirtualWorld(playerid, random(39)+1);
And I'm not saying the random thing is a good way of spawn protection but I'm just pointing a mistake out.
Reply
#9

anyways thanks.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)