How to make random?
#1

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!
Reply
#2

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

Random function, examples are in the wiki,
Reply
#3

Код:
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;
}
Reply
#4

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.
Reply
#5

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;
}
Reply
#6

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 !
Reply
#7

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;
}
Reply
#8

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)