Important Functions and Variables
This section details the main functions and variables used or exposed by the script.
Understanding these elements will help you customize or integrate the script with other resources.
Main functions
Section titled “Main functions”In order to set up the ammunition system for the stun dart rifle, you must add an event name and implement the function below, which will check whether the player has ammunition before allowing them to shoot.
In the configuration file
Section titled “In the configuration file”You must enter a name as shown in the example below in order to set up the use of ammunition :
Config.EventHaveItem = "DETECT_IF_I_HAVE_AMMO"The variable is located in the folder: /shared/config.lua.
Checking ammunition possession (server side)
Section titled “Checking ammunition possession (server side)”The function is located in the folder: /server/opensource.lua.
AddEventHandler(Config.EventHaveItem, function(target, cb) -- if true -- cb(true)
-- if false -- cb(false)end)Implementation example with ox_inventory
Section titled “Implementation example with ox_inventory”Here is an implementation example using ox_inventory:
AddEventHandler(Config.EventHaveItem, function(src, cb) local count = exports.ox_inventory:Search(src, 'count', 'ammo-dartrifle')
if not count or count == 0 then if Config.Debug then print("[ERROR] Could not retrieve ammo count for player " .. src .. ".") end cb(false) return end
if Config.Debug then print("[INFO] Player " .. src .. " have 1 ammo or more.") end cb(true)end)In the weapons.lua file, add the definition of the weapon and ammunition for the stun dart rifle:
Weapons = { ['WEAPON_DARTRIFLE'] = { label = 'Stun dart rifle', weight = 2500, durability = 0.1, ammoname = 'ammo-dartrifle', client = { image = 'stun_dart_rifle.png', export = 'ox_inventory.giveWeaponComponent', component = 'COMPONENT_AT_SCOPE_LARGE' } }, },
Ammo = { ["ammo-dartrifle"] = { label = 'Stun dart rifle ammunition', weight = 20, client = { image = 'stun_dart_rifle_ammo.png', } }, }Add the stun_dart_rifle.png and stun_dart_rifle_ammo.png images to the web/images folder of ox_inventory.
That’s it! You now have a functional ammunition system for the stun dart rifle.
Best practices
Section titled “Best practices”- Use local variables (
local) to limit the scope to the concerned function or script.