SA-MP Forums Archive
Object problem. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Object problem. (/showthread.php?tid=609045)



Object problem. - XtremeRz - 08.06.2016

Well, I need help in this code, This code is literally made for a sniper class. But when i type /tower it builds the tower.

And it's supposed from the code when i type /tower again, it will remove the tower and send me down.

But nope, instead it builds another tower ontop of the first one.. :/

Код:
CMD:tower(playerid,params[])
{
	new bool:objectz,tower,Float:x,Float:y,Float:z;
	if(objectz == false)
	{
		GetPlayerPos(playerid, x, y, z);
		tower = CreateObject(11461,x,y,z, 0.0, 0.0, 96.0);
		SetPlayerPos(playerid, x, y, z + 27.0);
		SendClientMessage(playerid,COLOR_GREY,"SERVER: You have created a tower.");
		objectz = true;
	}
	else if(objectz == true)
	{
        GetPlayerPos(playerid, x, y, z);
		SetPlayerPos(playerid,x,y,z - 27.0);
		DestroyObject(tower);
		SendClientMessage(playerid,COLOR_GREY,"SERVER: You have destroyed a tower.");
		objectz = false;
	}
	return 1;
}



Re: Object problem. - cuzido - 08.06.2016

Try this, maybe it does the trick for you.

PHP код:
CMD:tower(playerid,params[])
{
    new 
bool:objectz,tower,Float:x,Float:y,Float:z;
    if(
objectz == false)
    {
        
GetPlayerPos(playeridxyz);
        
tower CreateObject(11461,x,y,z0.00.096.0);
        
SetPlayerPos(playeridxy27.0);
        
SendClientMessage(playerid,COLOR_GREY,"SERVER: You have created a tower.");
        
objectz true;
        return 
1;
    }
    if(
objectz == true)
    {
                
GetPlayerPos(playeridxyz);
        
SetPlayerPos(playerid,x,y,27.0);
        
DestroyObject(tower);
        
SendClientMessage(playerid,COLOR_GREY,"SERVER: You have destroyed a tower.");
        
objectz false;
        return 
1;
    }
    return 
1;




Re: Object problem. - XtremeRz - 08.06.2016

Nope, won't work.


Re: Object problem. - MTGCrane - 08.06.2016

Код:
new tower;
CMD:tower(playerid,params[])
{
	new Float:x,Float:y,Float:z;
	if(!IsValidObject(tower))
	{
		GetPlayerPos(playerid, x, y, z);
		tower = CreateObject(11461,x,y,z, 0.0, 0.0, 96.0);
		SetPlayerPos(playerid, x, y, z + 27.0);
		SendClientMessage(playerid,COLOR_GREY,"SERVER: You have created a tower.");
	}
	else
	{
               GetPlayerPos(playerid, x, y, z);
		SetPlayerPos(playerid,x,y,z - 27.0);
		DestroyObject(tower);
		SendClientMessage(playerid,COLOR_GREY,"SERVER: You have destroyed a tower.");
	}
	return 1;
}



Re: Object problem. - BornHuman - 08.06.2016

You were defining tower locally so when the command was called it was automatically set to 0. You need to define it globally so the script remembers it. Otherwise, then the command is executed it creates the variable locally and therefor as a default value of 0.


Re: Object problem. - andrejc999 - 08.06.2016

Try to put [MAX_PLAYERS] for towers and make one for each player... So do it like this:

Код:
    tower[playerid] = CreateObject(...);
And when a player disconnects just destroy the tower if that player had one...
Код:
//Under public OnPlayerDisconnect(playerid)
DestroyObject(tower[playerid]);



Re: Object problem. - XtremeRz - 08.06.2016

Worked, Thanks everyone.