SA-MP Forums Archive
How to make random? - 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)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: How to make random? (/showthread.php?tid=248451)



How to make random? - Ben7544 - 13.04.2011

Well, I want to make that, when player press KEY_CROUCH it'll activate freeze for random player, for 10 seconds.

I done this to check if OnPlayerKeySateChange works, it works, now how I make it as I wanted up there ^:

Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(newkeys & KEY_CROUCH)
    {
		SendClientMessage(playerid,0xFFFFFFFF, "WORKING");
		}
	return 1;
}
Thank you!


Re: How to make random? - DarrenReeder - 14.04.2011

https://sampwiki.blast.hk/wiki/Random

Random function, examples are in the wiki,


Re: How to make random? - Shelby - 14.04.2011

Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(newkeys & KEY_CROUCH)
    {
	SendClientMessage(playerid,0xFFFFFFFF, "WORKING");
	new randomid = random(500);//500=number of players
	TogglePlayerControllable(randomid, false);
	SendClientMessage(randomid,0xFFFFFFFF, "You've been freezed by someone.");
    }
    return 1;
}



Re: How to make random? - DarrenReeder - 14.04.2011

Quote:
Originally Posted by Larceny
Посмотреть сообщение
Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(newkeys & KEY_CROUCH)
    {
	SendClientMessage(playerid,0xFFFFFFFF, "WORKING");
	new randomid = random(500);//500=number of players
	TogglePlayerControllable(randomid, false);
	SendClientMessage(randomid,0xFFFFFFFF, "You've been freezed by someone.");
    }
    return 1;
}
He wouldn't be able to use this. It would only work if all 500 player slots were filled.

Not quite sure how you could do this. I'm sure a more experienced Pawn Programmer will know how to do this properly.


Re: How to make random? - Shelby - 14.04.2011

Quote:
Originally Posted by DarrenReeder
Посмотреть сообщение
He wouldn't be able to use this. It would only work if all 500 player slots were filled.

Not quite sure how you could do this. I'm sure a more experienced Pawn Programmer will know how to do this properly.
Yes, it was just an example. To do this, could be used variables.
Example:

Код:
new totalplayers;

public OnPlayerConnect(playerid)
{
    if(totalplayers < GetMaxPlayers()) totalplayers++;
    return 1;
}

public OnPlayerDisconnect(playerid)
{
    if(totalplayers > 0) totalplayers--;
    return 1;
}

public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(newkeys & KEY_CROUCH)
    {
	SendClientMessage(playerid,0xFFFFFFFF, "WORKING");
	new randomid = random(totalplayers);
	TogglePlayerControllable(randomid, false);
	SendClientMessage(randomid,0xFFFFFFFF, "You've been freezed by someone.");
    }
    return 1;
}



Re: How to make random? - Ben7544 - 14.04.2011

Thank you it helped me alot !
Now I'll try to do it by my self ,using Larency's example and Wiki's guide ( Darren's link )

Thank you both,
Best regard,
Ben7544 !


Re: How to make random? - Mean - 14.04.2011

Quote:
Originally Posted by Larceny
Посмотреть сообщение
Yes, it was just an example. To do this, could be used variables.
Example:

Код:
new totalplayers;

public OnPlayerConnect(playerid)
{
    if(totalplayers < GetMaxPlayers()) totalplayers++;
    return 1;
}

public OnPlayerDisconnect(playerid)
{
    if(totalplayers > 0) totalplayers--;
    return 1;
}

public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(newkeys & KEY_CROUCH)
    {
	SendClientMessage(playerid,0xFFFFFFFF, "WORKING");
	new randomid = random(totalplayers);
	TogglePlayerControllable(randomid, false);
	SendClientMessage(randomid,0xFFFFFFFF, "You've been freezed by someone.");
    }
    return 1;
}
This will ALSO have bugs. If there are 2 players, and one has ID 3, it will exclude him, and it might pick 2 which is not connected. Best way is using foreach.
pawn Код:
#include < foreach >
public OnPlayerKeyStateChange( playerid, newkeys, oldkeys )
{
    if( newkeys & KEY_CROUCH )
    {
        SendClientMessage( playerid,0xFFFFFFFF, "WORKING" );
        new randomid = Iter_Random( Player );
        TogglePlayerControllable( randomid, false );
        SendClientMessage( randomid,0xFFFFFFFF, "You've been frozen by someone." ); // It's frozen not freezed :P
    }
    return 1;
}



Re: How to make random? - Shelby - 14.04.2011

Quote:
Originally Posted by Mean
Посмотреть сообщение
This will ALSO have bugs. If there are 2 players, and one has ID 3, it will exclude him, and it might pick 2 which is not connected. Best way is using foreach.
Oh... I forgot that. thanks!

pawn Код:
// It's frozen not freezed
loool, thanks again.