SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 26/06/2015, 08:43 AM   #1
Youssef221
Big Clucker
 
Join Date: Jul 2013
Posts: 195
Reputation: 11
Default Simple Spawn Protection

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.

Last edited by Youssef221; 26/06/2015 at 09:19 AM.
Youssef221 is offline   Reply With Quote
Old 26/06/2015, 08:59 AM   #2
Infinity
High-roller
 
Infinity's Avatar
 
Join Date: Jul 2009
Posts: 1,536
Reputation: 975
Default Re: Simple Spawn Protection

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 apologize for the stupidity of my fellow members

Quote:
Originally Posted by !damo!spiderman View Post
Stand for something that's going to help the world rather than fill it with more hate. Give someone a hug, tell someone you care... Do something, do anything but do it with love not hate.
Infinity is online now   Reply With Quote
Old 26/06/2015, 09:02 AM   #3
Youssef221
Big Clucker
 
Join Date: Jul 2013
Posts: 195
Reputation: 11
Default Re: Simple Spawn Protection

Misunderstood at that time..
Youssef221 is offline   Reply With Quote
Old 26/06/2015, 09:02 AM   #4
Sellize
Gangsta
 
Sellize's Avatar
 
Join Date: Jan 2013
Location: (ง ͠ ͟ل͜ ͡)ง fight me irl
Posts: 903
Reputation: 293
Default Re: Simple Spawn Protection

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
__________________
dead
Sellize is offline   Reply With Quote
Old 26/06/2015, 09:05 AM   #5
Youssef221
Big Clucker
 
Join Date: Jul 2013
Posts: 195
Reputation: 11
Default Re: Simple Spawn Protection

@Infinity, @Sellize, you mean Like this?:
pawn Code:
world = playerid
?
Youssef221 is offline   Reply With Quote
Old 26/06/2015, 09:08 AM   #6
Sellize
Gangsta
 
Sellize's Avatar
 
Join Date: Jan 2013
Location: (ง ͠ ͟ل͜ ͡)ง fight me irl
Posts: 903
Reputation: 293
Default Re: Simple Spawn Protection

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;

__________________
dead
Sellize is offline   Reply With Quote
Old 26/06/2015, 09:22 AM   #7
Youssef221
Big Clucker
 
Join Date: Jul 2013
Posts: 195
Reputation: 11
Default Re: Simple Spawn Protection

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.
Youssef221 is offline   Reply With Quote
Old 26/06/2015, 09:26 AM   #8
Sellize
Gangsta
 
Sellize's Avatar
 
Join Date: Jan 2013
Location: (ง ͠ ͟ل͜ ͡)ง fight me irl
Posts: 903
Reputation: 293
Default Re: Simple Spawn Protection

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.
__________________
dead
Sellize is offline   Reply With Quote
Old 26/06/2015, 09:29 AM   #9
Youssef221
Big Clucker
 
Join Date: Jul 2013
Posts: 195
Reputation: 11
Default Re: Simple Spawn Protection

anyways thanks.
Youssef221 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Spawn protection Brandon001 Scripting Help 1 18/01/2015 06:38 PM
[FilterScript] Simple spawn protection Glossy42O Filterscripts 46 28/12/2014 07:06 PM
Spawn protection ScRipTeRi Scripting Help 5 19/12/2013 07:09 PM
Spawn Protection + Spawn Health Help (rep++) iOmar Scripting Help 2 13/04/2012 12:53 PM
[FilterScript] Spawn Protection Filterscript [K665's Spawn Protection] Kerlan Filterscripts 17 28/11/2011 01:02 PM


All times are GMT. The time now is 08:57 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.