diff --git a/resources/[inventory]/nordi_vending/client.lua b/resources/[inventory]/nordi_vending/client.lua index 6b7c6d536..a6c7167a3 100644 --- a/resources/[inventory]/nordi_vending/client.lua +++ b/resources/[inventory]/nordi_vending/client.lua @@ -1,9 +1,12 @@ local QBCore = exports['qb-core']:GetCoreObject() --- Add targets to all vending machine props -CreateThread(function() - Wait(2000) +-- Function to initialize targets +function InitializeTargets() + -- Remove existing targets first to avoid duplicates + exports['qb-target']:RemoveTargetModel(Config.VendingProps) + Wait(100) + -- Add targets exports['qb-target']:AddTargetModel(Config.VendingProps, { options = { { @@ -45,8 +48,49 @@ CreateThread(function() }, distance = 2.0 }) + + print("^2[VENDING]^7 Added targets to " .. #Config.VendingProps .. " vending machine props") +end + +-- Add targets to all vending machine props with multiple attempts (Option 1) +CreateThread(function() + -- First attempt + Wait(2000) + InitializeTargets() + + -- Second attempt after a delay + Wait(5000) + InitializeTargets() + + -- Third attempt after server is fully loaded + Wait(10000) + InitializeTargets() end) +-- Event-based initialization (Option 2) +RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function() + Wait(1000) + InitializeTargets() +end) + +RegisterNetEvent('QBCore:Client:OnPlayerUnload', function() + -- Nothing to do here, but good to have for completeness +end) + +-- Listen for resource start/stop events +AddEventHandler('onResourceStart', function(resourceName) + if resourceName == 'qb-target' or resourceName == GetCurrentResourceName() then + Wait(1000) + InitializeTargets() + end +end) + +-- Command to manually refresh targets +RegisterCommand('refreshvendingtargets', function() + InitializeTargets() + QBCore.Functions.Notify('Vending machine targets refreshed', 'success') +end, false) + -- Check if machine is registered function isRegisteredMachine(entity) local coords = GetEntityCoords(entity) @@ -582,8 +626,7 @@ end) -- Refresh targets (called when new machine is registered) RegisterNetEvent('vending:client:refreshTargets', function() - -- Targets are automatically updated by qb-target - -- This event can be used for additional refresh logic if needed + InitializeTargets() end) -- Management menu (alternative opening method) @@ -614,22 +657,6 @@ RegisterNetEvent('vending:client:openManagement', function(machine) lib.showContext('vending_management') end) --- Debug commands -RegisterCommand('vendingdebug', function() - local playerPed = PlayerPedId() - local coords = GetEntityCoords(playerPed) - - QBCore.Functions.TriggerCallback('vending:server:getMachineByCoords', function(machine) - if machine then - print('Machine found:', json.encode(machine)) - QBCore.Functions.Notify('Machine data logged to console', 'primary') - else - print('No machine found at current location') - QBCore.Functions.Notify('No machine found here', 'error') - end - end, coords) -end, false) - -- Debug command to check props RegisterCommand('checkvendingprops', function() local playerPed = PlayerPedId() @@ -672,40 +699,18 @@ RegisterCommand('checkvendingprops', function() QBCore.Functions.Notify('Found ' .. foundProps .. ' vending machines nearby', 'primary') end, false) --- Command to manually refresh targets -RegisterCommand('refreshvendingtargets', function() - exports['qb-target']:RemoveTargetModel(Config.VendingProps) - Wait(500) +-- Debug commands +RegisterCommand('vendingdebug', function() + local playerPed = PlayerPedId() + local coords = GetEntityCoords(playerPed) - exports['qb-target']:AddTargetModel(Config.VendingProps, { - options = { - { - type = "client", - event = "vending:client:buyMachine", - icon = "fas fa-dollar-sign", - label = "Automaten kaufen ($" .. Config.VendingMachinePrice .. ")" - }, - { - type = "client", - event = "vending:client:openBuyMenu", - icon = "fas fa-shopping-cart", - label = "Kaufen" - }, - { - type = "client", - event = "vending:client:openOwnerMenu", - icon = "fas fa-cog", - label = "Verwalten" - }, - { - type = "client", - event = "vending:client:startRobbery", - icon = "fas fa-mask", - label = "Aufbrechen" - } - }, - distance = 2.0 - }) - - QBCore.Functions.Notify('Vending machine targets refreshed', 'success') + QBCore.Functions.TriggerCallback('vending:server:getMachineByCoords', function(machine) + if machine then + print('Machine found:', json.encode(machine)) + QBCore.Functions.Notify('Machine data logged to console', 'primary') + else + print('No machine found at current location') + QBCore.Functions.Notify('No machine found here', 'error') + end + end, coords) end, false)