diff --git a/resources/[inventory]/nordi_vending/client.lua b/resources/[inventory]/nordi_vending/client.lua index ace50a61c..07313048b 100644 --- a/resources/[inventory]/nordi_vending/client.lua +++ b/resources/[inventory]/nordi_vending/client.lua @@ -1,61 +1,73 @@ local QBCore = exports['qb-core']:GetCoreObject() --- Function to initialize targets +-- Function to initialize targets with more robust approach function InitializeTargets() - -- Remove existing targets first to avoid duplicates - exports['qb-target']:RemoveTargetModel(Config.VendingProps) - Wait(100) + -- First, remove any existing targets + for _, propName in ipairs(Config.VendingProps) do + exports['qb-target']:RemoveTargetModel(propName) + Wait(50) -- Small wait to ensure removal completes + end - -- Add targets - exports['qb-target']:AddTargetModel(Config.VendingProps, { - options = { - { - type = "client", - event = "vending:client:buyMachine", - icon = "fas fa-dollar-sign", - label = "Automaten kaufen ($" .. Config.VendingMachinePrice .. ")", - canInteract = function(entity) - return not isRegisteredMachine(entity) - end - }, - { - type = "client", - event = "vending:client:openBuyMenu", - icon = "fas fa-shopping-cart", - label = "Kaufen", - canInteract = function(entity) - return isRegisteredMachine(entity) - end - }, - { - type = "client", - event = "vending:client:openOwnerMenu", - icon = "fas fa-cog", - label = "Verwalten", - canInteract = function(entity) - return canManageMachine(entity) - end - }, - { - type = "client", - event = "vending:client:startRobbery", - icon = "fas fa-mask", - label = "Aufbrechen", - canInteract = function(entity) - return isRegisteredMachine(entity) and not canManageMachine(entity) - end - } - }, - distance = 2.0 - }) + -- Wait a bit before adding new targets + Wait(200) - print("^2[VENDING]^7 Added targets to " .. #Config.VendingProps .. " vending machine props") + -- Add targets one by one with delay between each + for _, propName in ipairs(Config.VendingProps) do + exports['qb-target']:AddTargetModel(propName, { + options = { + { + type = "client", + event = "vending:client:buyMachine", + icon = "fas fa-dollar-sign", + label = "Automaten kaufen ($" .. Config.VendingMachinePrice .. ")", + canInteract = function(entity) + return not isRegisteredMachine(entity) + end + }, + { + type = "client", + event = "vending:client:openBuyMenu", + icon = "fas fa-shopping-cart", + label = "Kaufen", + canInteract = function(entity) + return isRegisteredMachine(entity) + end + }, + { + type = "client", + event = "vending:client:openOwnerMenu", + icon = "fas fa-cog", + label = "Verwalten", + canInteract = function(entity) + return canManageMachine(entity) + end + }, + { + type = "client", + event = "vending:client:startRobbery", + icon = "fas fa-mask", + label = "Aufbrechen", + canInteract = function(entity) + return isRegisteredMachine(entity) and not canManageMachine(entity) + end + } + }, + distance = 2.0 + }) + + print("^2[VENDING]^7 Added target for prop: " .. propName) + Wait(100) -- Wait between each prop to avoid race conditions + end + + print("^2[VENDING]^7 Finished adding targets to " .. #Config.VendingProps .. " vending machine props") end --- Add targets to all vending machine props with multiple attempts (Option 1) +-- More aggressive target initialization CreateThread(function() + -- Initial delay to ensure everything is loaded + Wait(3000) + -- First attempt - Wait(2000) InitializeTargets() -- Second attempt after a delay @@ -65,11 +77,45 @@ CreateThread(function() -- Third attempt after server is fully loaded Wait(10000) InitializeTargets() + + -- Fourth attempt after even longer delay + Wait(20000) + InitializeTargets() + + -- Set up a repeating check every 5 minutes + while true do + Wait(300000) -- 5 minutes + InitializeTargets() + end end) --- Event-based initialization (Option 2) +-- Force refresh targets when player moves between areas +CreateThread(function() + local lastArea = 0 + + while true do + Wait(5000) + local playerPed = PlayerPedId() + local coords = GetEntityCoords(playerPed) + local currentArea = math.floor(coords.x / 100) * 1000 + math.floor(coords.y / 100) + + if currentArea ~= lastArea then + print("^2[VENDING]^7 Player moved to new area, refreshing targets") + InitializeTargets() + lastArea = currentArea + end + end +end) + +-- Add a command to manually refresh targets with notification +RegisterCommand('fixvending', function() + InitializeTargets() + QBCore.Functions.Notify('Vending machine targets refreshed', 'success') +end, false) + +-- Register for core events that might indicate a good time to refresh RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function() - Wait(1000) + Wait(2000) InitializeTargets() end) @@ -80,16 +126,22 @@ end) -- Listen for resource start/stop events AddEventHandler('onResourceStart', function(resourceName) if resourceName == 'qb-target' or resourceName == GetCurrentResourceName() then - Wait(1000) + Wait(2000) InitializeTargets() end end) --- Command to manually refresh targets -RegisterCommand('refreshvendingtargets', function() +-- Listen for target-specific events if they exist +RegisterNetEvent('qb-target:client:refreshTargets', function() + Wait(500) InitializeTargets() - QBCore.Functions.Notify('Vending machine targets refreshed', 'success') -end, false) +end) + +-- Refresh targets when a new machine is registered +RegisterNetEvent('vending:client:refreshTargets', function() + Wait(500) + InitializeTargets() +end) -- Get precise coordinates for entity function getPreciseCoords(entity) @@ -639,11 +691,6 @@ RegisterNetEvent('vending:client:policeAlert', function(coords, streetName) QBCore.Functions.Notify('Verkaufsautomat Aufbruch gemeldet: ' .. streetName, 'error', 8000) end) --- Refresh targets (called when new machine is registered) -RegisterNetEvent('vending:client:refreshTargets', function() - InitializeTargets() -end) - -- Management menu (alternative opening method) RegisterNetEvent('vending:client:openManagement', function(machine) lib.registerContext({