forked from Simnation/Main
Update client.lua
This commit is contained in:
parent
f6093270e7
commit
3b9ba99aa3
1 changed files with 108 additions and 61 deletions
|
@ -1,13 +1,19 @@
|
|||
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, {
|
||||
-- Wait a bit before adding new targets
|
||||
Wait(200)
|
||||
|
||||
-- Add targets one by one with delay between each
|
||||
for _, propName in ipairs(Config.VendingProps) do
|
||||
exports['qb-target']:AddTargetModel(propName, {
|
||||
options = {
|
||||
{
|
||||
type = "client",
|
||||
|
@ -49,13 +55,19 @@ function InitializeTargets()
|
|||
distance = 2.0
|
||||
})
|
||||
|
||||
print("^2[VENDING]^7 Added targets to " .. #Config.VendingProps .. " vending machine props")
|
||||
print("^2[VENDING]^7 Added target for prop: " .. propName)
|
||||
Wait(100) -- Wait between each prop to avoid race conditions
|
||||
end
|
||||
|
||||
-- Add targets to all vending machine props with multiple attempts (Option 1)
|
||||
print("^2[VENDING]^7 Finished adding targets to " .. #Config.VendingProps .. " vending machine props")
|
||||
end
|
||||
|
||||
-- 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({
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue