# LOTTERY SYSTEM

{% embed url="<https://youtu.be/wKbttnt5aq8?si=8Tp7wfBsACouMaK4>" %}

**INSTALLATION GUIDE**

1. Download from [KEYMASTER ](https://keymaster.fivem.net/login?return_url=/asset-grants)and Unzip the **`forge-lottery.pack.zip`** and place this folder in your server's resource folder.
2. Add the resource to your server start config: **`ensure forge-lottery`**,the name of the folder must not be changed or the resource will not function correctly.
3. **Install the SQL** that comes with the script. If you had version V.1 of the script installed, make sure to delete the old columns related to the Lottery and install the new ones.
4. Clear the cache of your server and also of your own FiveM.
5. Reboot the entire server with the forge script well ensured in your server.cfg.
6. <mark style="color:red;">**Do not rename this script, this may cause it to fail when opening the interface.**</mark>

{% hint style="warning" %}
When you have configured everything correctly — including the WEBHOOKS and all other settings — you can run the `testprize` command to simulate a prize delivery for the Weekly Lottery. This test helps ensure everything is working properly without needing to wait for the actual draw.

**Note:** The command must be executed **server-side**, in the **TX console**, and **without the slash ( / )**, as slashes are not used in the console.
{% endhint %}

{% hint style="info" %}
**Remember**: The **WEEKLY LOTTERY** will not deliver a prize if there is only one participant, to prevent someone from taking advantage and winning easily. It will notify through the logs and everywhere that there will be no prize.
{% endhint %}

**CONFIG**

The following will explain all the settings, one of the most important things that I recommend you spend a few minutes to understand in order to offer your users the best possible experience.

{% tabs %}
{% tab title="CONFIG" %}
Fill all the **CONFIG** very carefully. &#x20;

{% hint style="danger" %}

1. **WARNING!**: Carefully read all the variables. The common issue is with the `Currency` variables as the default is `"money"` but this is only for ESX. For QB, you will need to set it to `"cash"` or `"bank"`. Keep this in mind, or it will fail when you try to purchase.
2. **Check the prizes carefully**. If you set it to "mixed" mode, you will need to install or configure items that you have on your server; otherwise, it will fail.
3. **Check the amounts!** Be cautious because people can get rich quickly! We advise setting a low chance of winning and offering low-value prizes.
   {% endhint %}

{% code lineNumbers="true" %}

```lua
Config = { }

--  _____ _____ _   _ ______ _____ _____ _   _______  ___ _____ _____ _____ _   _
-- /  __ \  _  | \ | ||  ___|_   _|  __ \ | | | ___ \/ _ \_   _|_   _|  _  | \ | |
-- | /  \/ | | |  \| || |_    | | | |  \/ | | | |_/ / /_\ \| |   | | | | | |  \| |
-- | |   | | | | . ` ||  _|   | | | | __| | | |    /|  _  || |   | | | | | . ` |
-- | \__/\ \_/ / |\  || |    _| |_| |_\ \ |_| | |\ \| | | || |  _| |_\ \_/ / |\  |
--  \____/\___/\_| \_/\_|    \___/ \____/\___/\_| \_\_| |_/\_/  \___/ \___/\_| \_/


-- Framework Configuration
Config.Framework = 'esx'  -- Choose between 'esx' or 'qbcore'
Config.SQLWrapper = 'oxmysql'  -- Choose the MySQL wrapper

-- Target System Configuration
Config.UseTarget = true  -- Enable or disable target system
Config.TargetSystem = 'ox_target'  -- Choose target system

-- Daily Gift Configuration
Config.DailyGift = {
    GiftType = 'diamond' -- Type of daily prize of scrath & win roll ('diamond' or 'number')
}

Config.SocietyPayments = { -- The money spent on the lottery will go to the society
    Enabled = false,
    Destinations = { -- 0.1 = 10% and 1.0 = 100%
        ['casino'] = 0.7, -- 70% of the money will go to the casino
        ['lotto'] = 0.3 -- 30% of the money will go to the lottery
    } -- You cand add more destinations
}

Config.SocietyWithdraws = { -- The prize money comes from one society or more than one
    Enabled = true, -- Enable or disable society withdraws
    ScratchAndWinEnabled = true, -- You can enable or disable it. If you disable it, the prize money for this minigame will be auto-generated and won't come from any society.
    WheelEnabled = true, -- You can enable or disable it. If you disable it, the prize money for this minigame will be auto-generated and won't come from any society.
    WeeklyEnabled = true, -- You can enable or disable it. If you disable it, the prize money for this minigame will be auto-generated and won't come from any society.
    Sources = { -- 0.1 = 10% and 1.0 = 100%
        ['casino'] = 0.7, -- 70% of the prize money will come from the casino
        ['lotto'] = 0.3 -- 30% of the prize money will come from the lottery
    }
}

Config.SocietyMethod = "ESX" -- ESX, QB, or FORGE (you can purchase the FORGE BOSS MENU at https://codeforge.tebex.io/)

-- Weekly Lottery Configuration
Config.WeeklyLottery = {
    DrawTime = 120, -- Every x hour it will draw
    PrizeType = 'money', -- Type of prize: 'mixed', 'money', or 'items'
    PrizeAmount = { min = 100000, max = 300000 }, -- Randomized prize amount
    PrizeCurrency = 'money', -- Currency type for the prize: 'money', 'money', or 'bank'
    Items = { -- Items that can be won
        { Name = 'gold', Quantity = { 2, 4 }, Chance = 40 }, -- 40% chance to win gold
        { Name = 'iron', Quantity = { 5, 15 }, Chance = 100 } -- 100% chance to win iron
    },
    Tickets = { -- Lottery Ticket Prices and Chances
        Bronze = {
            Price = 5000,
            Currency = 'money', -- Currency for the ticket: 'money', 'cash', 'bank' or other...
            ChanceToWin = 20 -- Chance to win. 100% win every time
        },
        Silver = {
            Price = 10000,
            Currency = 'money',
            ChanceToWin = 30 -- Chance to win. 100% win every time
        },
        Gold = {
            Price = 30000,
            Currency = 'money',
            ChanceToWin = 50 -- Chance to win. 100% win every time
        }
    },
}

-- Scratch and Win Configuration
Config.ScratchAndWin = {
    SpamCheck = true, -- Enable / disable spam check
    SpamDuration = 20, -- Max time to open the menu before it counts as spam 
    SpamCount = 5, -- Number of allowed scratch card purchases before cooldown
    Cooldown = 20, -- Cooldown duration in seconds after spam limit is reached
    DiamondParty = {
        Price = 1000,
        Currency = 'money', -- Currency for the ticket: 'money', 'cash', 'bank' or other...
        PrizeAmount = { min = 10000, max = 50000 },
        ChanceToWin = 100, -- Chance to win. 100% win every time
        PrizeType = 'items', -- Type of prize: 'mixed', 'money', or 'items'
        PrizeCurrency = 'money', -- Currency type for the prize
        Items = { -- Items that can be won
            { Name = 'gold', Quantity = { 2, 4 }, Chance = 40 }, -- 40% chance to win gold
            { Name = 'silver', Quantity = { 1, 3 }, Chance = 30 }, -- 30% chance to win silver
            { Name = 'diamond', Quantity = { 1, 2 }, Chance = 20 } -- 20% chance to win diamond
        }
    },
    KingOfNumbers = {
        Price = 1000,
        Currency = 'money',
        PrizeType = 'mixed',
        PrizeCurrency = 'money',
        Prizes = { -- Prizes based on the number of matching numbers
            [0] = { -- 0 matching numbers
                Chance = 50, -- Chance to get 0 matching numbers. 100% win every time
                Items = { { Name = 'gold', Quantity = { 2, 4 }, Chance = 40 } }, -- Chance to win gold
                PrizeAmount = { min = 1000, max = 5000 } -- Money quantity
            },
            [1] = { -- 1 matching number
                Chance = 30, -- Chance to get 1 matching number. 100% win every time
                Items = { { Name = 'gold', Quantity = { 2, 4 }, Chance = 40 } }, -- Chance to win gold
                PrizeAmount = { min = 1000, max = 5000 } -- Money quantity
            },
            [2] = { -- 2 matching numbers
                Chance = 15, -- Chance to get 2 matching numbers. 100% win every time
                Items = { { Name = 'silver', Quantity = { 1, 3 }, Chance = 30 } }, -- Chance to win silver
                PrizeAmount = { min = 5000, max = 10000 } -- Money quantity
            },
            [3] = { -- 3 matching numbers
                Chance = 5, -- Chance to get 3 matching numbers. 100% win every time
                Items = { { Name = 'diamond', Quantity = { 1, 2 }, Chance = 20 } }, -- Chance to win diamond
                PrizeAmount = { min = 10000, max = 50000 } -- Money quantity
            }
        }
    }
}

-- Fortune Wheel Configuration
Config.FortuneWheel = {
    SpamCheck = true, -- Enable / disable spam check
    SpamDuration = 5, -- Max time to open the menu before it counts as spam
    SpamCount = 2, -- Number of allowed menu toggles before cooldown
    Cooldown = 10, -- Cooldown duration in seconds after spam limit is reached
    TicketPrice = 1000,
    Currency = 'money', -- Currency for the ticket: 'money', 'cash', 'bank' or other...
    Prizes = {
        [1] = { -- Winning number
            PrizeType = 'money', -- Type of prize: 'mixed', 'money', or 'items'
            PrizeCurrency = 'money', -- Currency type for the prize
            PrizeAmount = { min = 100, max = 500 }, -- Randomized prize amount
            Items = { 
                { Name = 'gold', Quantity = { 2, 4 }, Chance = 40 }, -- Chance to win gold
                { Name = 'silver', Quantity = { 1, 3 }, Chance = 30 }, -- Chance to win silver
                { Name = 'diamond', Quantity = { 1, 2 }, Chance = 20 } -- Chance to win diamond
            }
        },
        [2] = {
            PrizeType = 'money',
            PrizeCurrency = 'money',
            PrizeAmount = { min = 1000, max = 5000 },
            Items = { { Name = 'gold', Quantity = { 2, 4 }, Chance = 40 } }
        },
        [3] = {
            PrizeType = 'money',
            PrizeCurrency = 'money',
            PrizeAmount = { min = 100, max = 500 },
            Items = { { Name = 'gold', Quantity = { 2, 4 }, Chance = 40 } }
        },
        [4] = {
            PrizeType = 'money',
            PrizeCurrency = 'money',
            PrizeAmount = { min = 10, max = 50 },
            Items = { { Name = 'gold', Quantity = { 2, 4 }, Chance = 40 } }
        },
        [5] = {
            PrizeType = 'money',
            PrizeCurrency = 'money',
            PrizeAmount = { min = 10, max = 50 },
            Items = { { Name = 'gold', Quantity = { 2, 4 }, Chance = 40 } }
        },
        [6] = {
            PrizeType = 'money',
            PrizeCurrency = 'money',
            PrizeAmount = { min = 1, max = 5 },
            Items = { { Name = 'gold', Quantity = { 2, 4 }, Chance = 40 } }
        },
        [7] = {
            PrizeType = 'money',
            PrizeCurrency = 'money',
            PrizeAmount = { min = 100, max = 3000 },
            Items = { { Name = 'gold', Quantity = { 2, 4 }, Chance = 40 } }
        },
        [8] = {
            PrizeType = 'money',
            PrizeCurrency = 'money',
            PrizeAmount = { min = 1000, max = 2000 },
            Items = { { Name = 'gold', Quantity = { 2, 4 }, Chance = 40 } }
        },
        [9] = {
            PrizeType = 'mixed',
            PrizeCurrency = 'money',
            PrizeAmount = { min = 300, max = 450 },
            Items = { { Name = 'gold', Quantity = { 2, 4 }, Chance = 40 } }
        },
        [10] = {
            PrizeType = 'mixed',
            PrizeCurrency = 'money',
            PrizeAmount = { min = 1000, max = 5000 },
            Items = { { Name = 'gold', Quantity = { 2, 4 }, Chance = 40 } }
        }
    }
}

-- Interface Configuration
Config.UI = {
    primaryColor = '#ffad00',
    red = '#ff2945',
    borderColor = '#d7d7d7',
    translation = {
        lotterytitle = 'LOTTERY',
        lotterydesc = 'Where getting rich is legal',
        home = 'HOME',
        weeklylottery = 'WEEKLY LOTTERY',
        scratchadnwin = 'SCRATCH & WIN',
        fortunewheel = 'FORTUNE WHEEL',
        lotterytitle2 = 'Lottery and Gaming Company - <span>LOTTO</span>',
        winbig = 'WIN BIG',
        winbigdesc = 'Earn <span>money</span> legally, take a chance on fate, andsavor the thrill of <span>pure adrenaline</span>!',
        playres = 'Play responsibly. Enjoy the excitement, but know your limits',
        leaderboard = 'LEADERBOARD',
        freeticket = 'A FREE <span>SCRATCH & WIN</span> TICKET!',
        unlock = 'To Unlock',
        weeklylottery2 = 'WEEKLY LOTTERY',
        bronzeticket = '<strong>Bronze</strong> Ticket',
        bronzeticketdesc = 'You have chosen the <span>Bronze Ticket</span>. Buy it and get a chance to win:<span>LOW</span>.',
        bronzeticketprice = 'Price: <strong>'..Config.WeeklyLottery.Tickets.Bronze.Price..'$</strong>',
        buybronze = 'Buy',
        silverticket = '<strong>Silver</strong> Ticket',
        silverticketdesc = 'You have chosen the <span>Silver Ticket</span>. Buy it and get a chance to win:<span>MEDIUM</span>.',
        silverticketprice = 'Price: <strong>'..Config.WeeklyLottery.Tickets.Silver.Price..'$</strong>',
        buysilver = 'Buy',
        goldticket = '<strong>Gold</strong> Ticket',
        goldticketdesc = 'You have chosen the <span>Gold Ticket</span>. Buy it and get a chance to win:<span>HIGH</span>.',
        goldticketprice = 'Price: <strong>'..Config.WeeklyLottery.Tickets.Gold.Price..'$</strong>',
        buygold = 'Buy',
        ticketlimit = 'You can only buy 1 ticket per week to combat gambling addiction',
        winningticket = 'Winning Ticket',
        lastwinnertitle = 'The latest <span>Weekly Lottery</span> winner is:',
        checkprize = 'Claim Prize',
        knownextwin = 'TIME TO KNOW THE <br /> <span>NEXT WINNER</span>:',
        scratchandwin2 = 'SCRATCH & WIN',
        scratchandwindesc = 'Buy a <span>Scratch & Win ticket</span> and get great prizes instantly. Click on one of the types:',
        diamondparty = '<span>Diamond Party:</span> Get 4 matching diamond symbols',
        diamondpartyprice = 'Price: <strong>'..Config.ScratchAndWin.DiamondParty.Price..'$</strong>',
        numberparty = '<span>Number Party:</span> Match the given numbers',
        numberpartyprice = 'Price: <strong>'..Config.ScratchAndWin.KingOfNumbers.Price..'$</strong>',
        scratchandwinb = 'The prize is given automatically and in money. All you have to do is scratch!',
        fortunewheel2 = 'Fortune Wheel',
        fortunewheeldesc = 'Yes! The first <span>Fortune Wheel</span> that isn\'t round! There\'s always a prize! Each number is linked to a secret reward.',
        fortunewheelprice = 'Price: <strong>'..Config.FortuneWheel.TicketPrice..'$</strong>',
        fortunewheelb = 'The prize is secret but the number 10 has a special prize',
        scratcheach = 'Scratch each rectangle with the cursor to reveal the symbol underneath.',
        incircles = 'In the circles, you need to find the winning numbers.'
    }
}

-- Locations for Lottery Purchase
Config.Locations = {
    { 
        Position = vector3(299.736267, -574.325256, 43.248291), -- Coordinates for the ticket station, in case you do not use target
        MarkerSettings = {
            Enabled = true, -- Show marker
            Type = 2, -- Marker type
            ColorR = 222, -- Red component
            ColorG = 186, -- Green component
            ColorB = 77, -- Blue component
            Alpha = 255 -- Transparency
        },
        BlipSettings = {
            Enabled = true, -- Show blip on the map
            Id = 369, -- Blip ID
            Color = 17, -- Blip color
            Scale = 0.7, -- Blip scale
            Display = 2, -- Blip display type
            Name = 'Lottery' -- Blip name
        }
    }
}

-- Functions for Drawing Markers and Texts
Config.Functions = {

    -- Function to draw text on the screen
    DrawText = function(text)
        AddTextEntry('ALERT_MESSAGE', text)
        BeginTextCommandDisplayHelp('ALERT_MESSAGE')
        EndTextCommandDisplayHelp(0, false, false, -1)
    end,

    -- Function to show a notification based on the framework being used (ESX or QBCore)
    Notify = function(text)
        if Config.Framework == 'esx' then
            ESX.ShowNotification(text)
        elseif Config.Framework == 'qbcore' then
            QBCore.Functions.Notify(text)
        end
    end,

    -- Function to add money to a society account based on the method being used (FORGE, QB, or ESX)
    AddSocietyMoney = function(society, amount, title, reason)
--  In your version here you have pre-created functions for ESX, QB, FORGE etc.
    end,

    -- Function to remove money(clamped) from a society account based on the method being used (FORGE, QB, or ESX)
    RemoveSocietyMoney = function(society, amount, title, reason)
-- In your version here you have pre-created functions for ESX, QB, FORGE etc.
    end

}

-- Target Items Configuration
Config.TargetItems = {
    -38797076, -- Lottery sign
    -1816283392 -- Lottery stand
}

-- Function to Add Target Models
Config.TargetFunctions = {

    AddBuyLotteryTarget = function()
        if Config.TargetSystem == 'qb-target' then
            exports['qb-target']:AddTargetModel(Config.TargetItems, {
                options = {
                    {
                        icon = 'fas fa-hands',
                        label = 'Buy Lottery',
                        action = function(entity)
                            TriggerEvent('forge-lottery:open')
                        end
                    }
                },
                distance = 2.5
            })
        elseif Config.TargetSystem == 'ox_target' then
            exports['ox_target']:addModel(Config.TargetItems, {
                {
                    name = 'buylottery',
                    icon = 'fas fa-hands',
                    label = 'Buy Lottery',
                    onSelect = function(data)
                        TriggerEvent('forge-lottery:open')
                    end
                }
            })
        end
    end

}

Config.Translation = {
    openmenutext = 'Press ~INPUT_CONTEXT~ to open the lottery menu',
    logstitle = 'New Lottery Win',
    logsusername = 'Lottery Logs',
    diamondsbuy = '%s has purchased a diamond party scratch card',
    numbersbuy = '%s has purchased a king of numbers scratch card',
    wheelbuy = '%s has purchased a fortune wheel ticket',
    diamondwin = '%s won %s from a diamond party scratch card',
    numberswin = '%s won %s from a king of numbers scratch card',
    wheelwin = '%s won %s from the fortune wheel',
    weeklybuy = '%s has purchased a %s weekly lottery ticket',
    weeklywin = '%s won the weekly lottery prize',
    noparticipants = 'This week there were not enough participants in the Weekly Lottery, so there were no prizes for anyone.'
}

-- The translations for the notifications and some other things.
Config.Notifications = {
    nomoney = 'You don\'t have enough money to buy this.',
    purchaseSuccess = 'You have successfully purchased a lottery ticket.',
    loseNotification = 'You did not win any prize this time. Best luck next time!',
    winNotification = 'Congratulations! You have won a prize of %s.',
    dailyGiftClaimed = 'You have claimed your daily gift.',
    spamWarning = 'You are performing actions too quickly. Please slow down.',
    cooldownActive = 'You are on cooldown. Please wait %s seconds before trying again.',
    noTickets = 'You don\'t have any lottery tickets to check.',
    ticketBought = 'You have bought a %s lottery ticket',
    prizeClaimed = 'You have claimed your prize of %s. Congratulations!',
    noPrize = 'You did not win any prize this time. Bad luck!',
    fortuneWheelWin = 'Congratulations! You have won a prize of %s! in fortune wheel',
    ticketLimitReached = 'You have reached the maximum number of tickets you can buy.',
    noWinner = 'The winner for this week has not been announced yet.'
}
```

{% endcode %}
{% endtab %}

{% tab title="SQL" %}

```sql
CREATE TABLE `lottery_daily` (
	`id` INT(11) NOT NULL AUTO_INCREMENT,
	`identifier` LONGTEXT NULL DEFAULT NULL COLLATE 'utf8mb3_general_ci',
	`time` INT(11) NULL DEFAULT NULL,
	PRIMARY KEY (`id`) USING BTREE
)
COLLATE='utf8mb3_general_ci'
ENGINE=InnoDB
;

CREATE TABLE `lottery_week` (
	`id` INT(11) NOT NULL AUTO_INCREMENT,
	`identifier` LONGTEXT NULL DEFAULT NULL COLLATE 'utf8mb3_general_ci',
	`name` LONGTEXT NULL DEFAULT NULL COLLATE 'utf8mb3_general_ci',
	`type` LONGTEXT NOT NULL COLLATE 'utf8mb3_general_ci',
	PRIMARY KEY (`id`) USING BTREE
)
COLLATE='utf8mb3_general_ci'
ENGINE=InnoDB
;

CREATE TABLE `lottery_week_time` (
	`time` INT(11) NULL DEFAULT NULL
)
COLLATE='utf8mb3_general_ci'
ENGINE=InnoDB
;

CREATE TABLE `lottery_week_win` (
	`identifier` LONGTEXT NULL DEFAULT NULL COLLATE 'utf8mb3_general_ci',
	`name` LONGTEXT NULL DEFAULT NULL COLLATE 'utf8mb3_general_ci',
	`claimed` INT(11) NULL DEFAULT NULL
)
COLLATE='utf8mb3_general_ci'
ENGINE=InnoDB
;

CREATE TABLE `lottery_win` (
	`id` INT(11) NOT NULL AUTO_INCREMENT,
	`identifier` LONGTEXT NULL DEFAULT NULL COLLATE 'utf8mb3_general_ci',
	`name` LONGTEXT NULL DEFAULT NULL COLLATE 'utf8mb3_general_ci',
	`amount` INT(11) NULL DEFAULT NULL,
	PRIMARY KEY (`id`) USING BTREE
)
COLLATE='utf8mb3_general_ci'
ENGINE=InnoDB
;
```

{% endtab %}
{% endtabs %}

{% hint style="success" %}
**If you want to edit the aesthetics or design. You have the HTML open so you can modify the style and everything as you want.**

The script is **RESPONSIVE** for all resolutions as well.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://codeforge.gitbook.io/codeforge/special-editions/lottery-system.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
