SetTimer("Lottery", 3600000, 1);
forward Lottery();
public Lottery()
{
new winner, str[128], pname[MAX_PLAYER_NAME];
new prize = (random(4000000)+1000000); //create random number between 1 and 5 million.
for(new i; i<MAX_PLAYERS; i++) // Start loop.
{
winner = random(MAX_PLAYERS); // Pick a random ID
if(IsPlayerConnected(winner)) // Check of that ID is online
{
GivePlayerMoney(winner, prize); // Give the winner the money
GetPlayerName(winner, pname, sizeof(pname)); // Get the winners name
format(str, sizeof(str), "[Lottery]: %s(%d) has won $%d! Congratulations!", pname, winner, prize); //format string
SendClientMessageToAll(-1, str); // Send message to all online players
break; // Stop the loop
}
}
return 1;
}
Edit: Sorry, this code is a little bit flawed, it might not always pick a winner. I'll try to fix it.
//OnGameModeInit() pawn Код:
pawn Код:
|
forward Lottery();
public Lottery()
{
new str[128],winner = random(MAX_PLAYERS); // Pick a random ID
if(!IsPlayerConnected(winner)) // Check of that ID is online or not
{
format(str, sizeof(str), "{f0f000}>> The winning random ID was %i but no such ID online so no prize", winner);
return SendClientMessagetoAll(-1, str);
}
new pname[MAX_PLAYER_NAME];
new prize = (random(4000000)+1000000); //create random number between 1 and 5 million.
GivePlayerMoney(winner, prize); // Give the winner the money
GetPlayerName(winner, pname, sizeof(pname)); // Get the winners name
format(str, sizeof(str), "[Lottery]: %s(%d) has won $%d! Congratulations!", pname, winner, prize); //format string
SendClientMessageToAll(-1, str); // Send message to all online players
return 1;
}
That is not the right way to do it, there is no need for a loop.
It would be like this - |
forward Lottery();
public Lottery()
{
new winner, str[128], pname[MAX_PLAYER_NAME], lotteryarray[MAX_PLAYERS];
new prize = (random(4000000)+1000000); //create random number between 1 and 5 million.
for(new s; s<MAX_PLAYERS; s++) lotteryarray[s] = s;
ShuffleArray(lotteryarray, sizeof(lotteryarray));
for(new i; i<MAX_PLAYERS; i++)
{
winner = lotteryarray[i];
if(IsPlayerConnected(winner)) // Check of that ID is online
{
GivePlayerMoney(winner, prize); // Give the winner the money
GetPlayerName(winner, pname, sizeof(pname)); // Get the winners name
format(str, sizeof(str), "[Lottery]: %s(%d) has won $%d! Congratulations!", pname, winner, prize); //format string
SendClientMessageToAll(-1, str); // Send message to all online players
break; // Stop the loop
}
}
return 1;
}
//ShuffleArray by Slice
stock ShuffleArray(array[], size = sizeof(array)) {
if (size < 2) return;
for (new i = 0; i < size - 1; i++) {
new slot = random(size);
if (slot == i) continue;
new temp = array[slot];
array[slot] = array[i];
array[i] = temp;
}
}
function Lottery()
{
new winner, str[128], str2[128],pname[MAX_PLAYER_NAME];
for(new i; i<MAX_PLAYERS; i++) // Start loop.
{
winner = random(MAX_PLAYERS); // Pick a random ID
if(IsPlayerConnected(winner)) // Check of that ID is online
{
GivePlayerMoney(winner, prize); // Give the winner the money
GetPlayerName(winner, pname, sizeof(pname)); // Get the winners name
format(str, sizeof(str), "[Lottery]: %s(%d) has won $%d! Congratulations!", pname, winner, prize); //format string
SendClientMessageToAll(-1, str); // Send message to all online players
break; // Stop the loop
}
if(!IsPlayerConnected(winner))
{
format(str2,sizeof(str2),"LOTTERY SYSTEM: WINNER ID(%d). Nobody won lotto prize : $%d ",winner,prize);
SendClientMessageToAll(-1,str2);
}
}
return 1;
}
I want to make it so it will be a random id of max players of the server
Example if the server has 200 max players it will pick random ids between 0-200 if the one of hte ids which is chosen is online he will get prize |
Read what he said, he doesn't want the winner every time
There is no need for a loop Use the code that I gave earlier |