Random Player ? - 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: Random Player ? (
/showthread.php?tid=534177)
Random Player ? -
Venice - 28.08.2014
How to give cash, scores, etc to Random Player when player spawn or something else
Re: Random Player ? -
Clad - 28.08.2014
pawn Код:
forward RandomMoney();
new Players;
pawn Код:
public OnGameModeInit()
{
SetTimer("RandomMoney,100000,1); Timer to give the random money
return 1;
}
pawn Код:
public OnPlayerConnect(playerid)
{
Players++; // Each time player joins the server
return 1;
}
pawn Код:
public OnPlayerDisconnect(playerid)
{
Players--; // Each time player leaves the server
return 1;
}
pawn Код:
public RandMoney()
{
GivePlayerMoney(random(Players),50); // Giver random player 50$
}
You can do the same thing with Score.
Re: Random Player ? -
IceCube! - 28.08.2014
EDIT: Too late.
Should have read the above first as now I have to rewrite this. If you want it on player spawn, and just for that player say: he has a 3/10 chance of getting score, money etc when he spawns. Just reply asking in more detail what you want. As I deleted my post.
Re: Random Player ? -
iFarbod - 28.08.2014
Quote:
Originally Posted by Clad
pawn Код:
forward RandomMoney(); new Players;
pawn Код:
public OnGameModeInit() { SetTimer("RandomMoney,100000,1); Timer to give the random money return 1; }
pawn Код:
public OnPlayerConnect(playerid) { Players++; // Each time player joins the server return 1; }
pawn Код:
public OnPlayerDisconnect(playerid) { Players--; // Each time player leaves the server return 1; }
pawn Код:
public RandMoney() { GivePlayerMoney(random(Players),50); // Giver random player 50$ }
You can do the same thing with Score.
|
This way is bad, since player IDs are not sorted.
You can see player IDs are like this: 0, 3, 4, 5, 7, 15, 19, 41.
In the above example, total connected players is : 8
So random ( 8 ) can't return the correct 'random' player ID.
I Have a better way:
pawn Код:
stock RandomPlayer()
{
new
ConnectedPlayers[MAX_PLAYERS] = {INVALID_PLAYER_ID, ...},
idx;
for(new i = 0; i != MAX_PLAYERS; ++i)
{
if(IsPlayerConnected(i))
{
ConnectedPlayers[idx] = i;
idx++;
}
}
if(!idx)
return INVALID_PLAYER_ID;
return ConnectedPlayers[random(idx)];
}
Re: Random Player ? -
DobbysGamertag - 28.08.2014
Use Iter_Random.
https://sampforum.blast.hk/showthread.php?tid=92679
Re: Random Player ? -
Venice - 28.08.2014
Quote:
Originally Posted by Clad
pawn Код:
forward RandomMoney(); new Players;
pawn Код:
public OnGameModeInit() { SetTimer("RandomMoney,100000,1); Timer to give the random money return 1; }
pawn Код:
public OnPlayerConnect(playerid) { Players++; // Each time player joins the server return 1; }
pawn Код:
public OnPlayerDisconnect(playerid) { Players--; // Each time player leaves the server return 1; }
pawn Код:
public RandMoney() { GivePlayerMoney(random(Players),50); // Giver random player 50$ }
You can do the same thing with Score.
|
Thanks for your help..
Re: Random Player ? -
Venice - 28.08.2014
Quote:
Originally Posted by iFarbod
This way is bad, since player IDs are not sorted.
You can see player IDs are like this: 0, 3, 4, 5, 7, 15, 19, 41.
In the above example, total connected players is : 8
So random ( 8 ) can't return the correct 'random' player ID.
I Have a better way:
pawn Код:
stock RandomPlayer() { new ConnectedPlayers[MAX_PLAYERS] = {INVALID_PLAYER_ID, ...}, idx; for(new i = 0; i != MAX_PLAYERS; ++i) { if(IsPlayerConnected(i)) { ConnectedPlayers[idx] = i; idx++; } }
if(!idx) return INVALID_PLAYER_ID;
return ConnectedPlayers[random(idx)]; }
|
How use this any example ?
Re: Random Player ? -
iFarbod - 28.08.2014
pawn Код:
public RandMoney()
{
GivePlayerMoney(RandomPlayer(), 50); // Giver random player 50$
}
Re: Random Player ? -
Clad - 28.08.2014
Quote:
Originally Posted by Venice
Thanks for your help..
|
Didn't work ?