CMD:buyticket(playerid, params[])
{
if(LotteryIsOn == 1) {
new string[128];
PlayerLotteryNumber[playerid] = random(1000)+5555;
format(string, sizeof(string), "LOTTERY: Your number is %d, the lottery draw is in 1 minute. Good luck.", PlayerLotteryNumber[playerid]);
SendClientMessage(playerid, COLOR_WHITE, string);
EnterLotto[playerid] = 1;
}
else {
SendClientMessage(playerid, COLOR_WHITE, "INFO: The lottery isn't on.");
}
return 1;
}
forward LottoDraw();
public LottoDraw() {
foreach(Player, i) {
if(EnterLotto[i] != 0) {
new LottoWinner1 = random(PlayerLotteryNumber[i]);
if(LottoWinner[i] == LottoWinner1) {
new string[128];
format(string, sizeof(string), "LOTTERY: The lottery winner is %s(%d) with the ticket %d, they have won a total of $150!", Name(i), i, PlayerLotteryNumber[i]);
SendClientMessageToAll(COLOR_WHITE, string);
LotteryIsOn = 0;
EnterLotto[i] = 0;
}
else {
SendClientMessageToAll(COLOR_WHITE, "INFO: There was no winner.");
}
}
else {
SendClientMessageToAll(COLOR_WHITE, "INFO: The lottery was canceled due to nobody entering.");
}
}
return 1;
}
You can only loop through connected players. If I did understand correct, you want to loop through all players that have a lottery ticket (even those who are not connected) which is not possible.
|
Hello, i've made a lottery script which isn't working correctly, I need it to loop through all the players who have entered the lottery draw, here's my codes; |
forward LottoDraw();
public LottoDraw()
{
new LotteryParticipants;
foreach(Player, i)
{
if(EnterLotto[i] != 0)
{
new LottoWinner1 = random(PlayerLotteryNumber[i]);
if(LottoWinner[i] == LottoWinner1)
{
new string[128];
format(string, sizeof(string), "LOTTERY: The lottery winner is %s(%d) with the ticket %d, they have won a total of $150!", Name(i), i, PlayerLotteryNumber[i]);
SendClientMessageToAll(COLOR_WHITE, string);
LotteryIsOn = 0;
EnterLotto[i] = 0;
}
LotteryParticipants ++;
}
}
if(!LotteryParticipants) // We didn't iterate through any players with EnterLotto set to anything but 0
SendClientMessageToAll(COLOR_WHITE, "INFO: The lottery was canceled due to nobody entering.");
if(LotteryIsOn) // Lottery hasn't ended, means we have no winner!
SendClientMessageToAll(COLOR_WHITE, "INFO: There was no winner.");
return 1;
}
forward LottoDraw();
public LottoDraw()
{
new somebodywon = 0, lottowinner = randomEx(1,100); //Pick some numbers to choose between, then we'll loop through the players to see if anybody had that ticket.
foreach(Player,i)
{
if(PlayerLotteryNumber[i] == lottowinner) // Somebody won, give them their prize and tell the server that they've won.
{
somebodywon = 1;
new string[128];
format(string, sizeof(string), "LOTTERY: The lottery winner is %s(%d) with the ticket %d, they have won a total of $150!", Name(i), i, PlayerLotteryNumber[i]);
SendClientMessageToAll(COLOR_WHITE, string);
LotteryIsOn = 0;
EnterLotto[i] = 0;
}
}
if(somebodywon == 0) //Nobody won, tell the server so.
{
SendClientMessageToAll(COLOR_WHITE, "INFO: There was no winner.");
}
return 1;
}
stock randomEx(min, max)
{
new rand = random(max-min)+min;
return rand;
}
if(EnterLotto[i] != 0)
if(EnterLotto[i] == 1)
new winningNumber = random(100) + 1, LotteryParticipants;
foreach(Player, i)
{
if(EnterLotto[i] == 0)
continue; // Don't do anything else for this player, he is not participating in the lottery
// The player is in the lotto, however might not win. Still consider them as a participant!
LotteryParticipants ++;
// Now compare the winning number to the one generated when player joined the lottery
if(winningNumber == PlayerLotteryNumber[i])
{
// yay, set the lottery state to ended!
LotteryIsOn = 0;
}
}
// Now, check if there were no participants.
if(!LotteryParticipants)
SendClientMessageToAll(COLOR_WHITE, "INFO: The lottery was canceled due to nobody entering.");
// And if there were participants, but no-one won (in this case the LotteryIsOn variable is still 1 because we didn't reset it in the loop)
if(LotteryIsOn == 1)
SendClientMessageToAll(COLOR_WHITE, "INFO: There was no winner.");
new winning = random(100);
if(winning == 0)
{
return LottoDraw();
}
new winning = random(100) + 1;
if(lwiner == -1)
{
//THERE IS NO WINNER
return 1;
}
if(lwiner != -1)
{
// WINNER ID IS STORED WITHIN THE VAR 'lwiner'
return 1;
}
if(lwiner == -1)
{
// No winner
return 1;
}
// winner id is stored in var 'lwiner'