Skip to content

Build a Drug System

Build a complete drug system with growing, processing, selling, and police detection.

What You'll Build

  • Grow locations with progress timers
  • Processing minigame (hit the right key at the right time)
  • Selling to NPCs at randomized locations
  • Police alert system when selling
  • Contraband flagged in IInventory

Prerequisites

  • shiva-inventory enabled
  • shiva-economy enabled
  • shiva-jobs enabled (for police detection)

Overview

[Grow Stage]          [Process Stage]       [Sell Stage]
Player plants   →   Player processes  →   Player sells
seeds at spot       raw drugs at lab       to NPC buyer
    ↓                    ↓                     ↓
    DB timer             Minigame             Economy +
    fires                succeeds             Police alert

Steps

1. Scaffold

bash
shiva make module my-drugs

2. Define Items

Add item definitions for each stage:

lua
Config.items = {
    seeds = { label = 'Weed Seeds',     weight = 0.1 },
    raw   = { label = 'Raw Weed',       weight = 0.5, illegal = true },
    final = { label = 'Processed Weed', weight = 0.3, illegal = true },
}

3. Grow Spots

lua
Config.growSpots = {
    { id = 'spot1', coords = { x = 2570.0, y = 4641.0, z = 34.0 }, growTime = 900 },
}

4. Server-Side Grow Timer

Use a database-backed timer so progress persists through restarts:

lua
-- Store grow start time in DB
DB:execute('INSERT INTO drug_grows (player_id, spot, started_at) VALUES (?, ?, NOW())', playerId, spotId)

-- Check if harvest is ready
local grow = DB:first('SELECT * FROM drug_grows WHERE spot = ? AND player_id = ?', spotId, playerId)
local elapsed = os.time() - grow.started_at_unix
if elapsed >= Config.growSpots[spotId].growTime then
    -- ready to harvest
end

5. Police Detection

Emit an event when a sale happens, hook it from a police module:

lua
bus:emit('drugs:saleMade', {
    playerId = source,
    coords   = GetEntityCoords(GetPlayerPed(source)),
    amount   = amount,
})

Released under the MIT License.