From 5b0effa7af8f8268d15193be0e18e170161ce60e Mon Sep 17 00:00:00 2001 From: Nordi98 Date: Wed, 6 Aug 2025 18:07:18 +0200 Subject: [PATCH] ed --- .../nordi_antidespawn/client/main.lua | 69 +++++++++++++++++-- 1 file changed, 63 insertions(+), 6 deletions(-) diff --git a/resources/[carscripts]/nordi_antidespawn/client/main.lua b/resources/[carscripts]/nordi_antidespawn/client/main.lua index 87fdff777..a9d3cf84c 100644 --- a/resources/[carscripts]/nordi_antidespawn/client/main.lua +++ b/resources/[carscripts]/nordi_antidespawn/client/main.lua @@ -179,10 +179,14 @@ end) -- Spawne gespeicherte Fahrzeuge RegisterNetEvent('vehicle-persistence:client:spawnSavedVehicles', function(vehicles) if Config.Debug then - print(string.format("Attempting to spawn %d saved vehicles", #vehicles)) + print(string.format("Received %d vehicles to spawn", #vehicles)) end for _, vehicleData in pairs(vehicles) do + if Config.Debug then + print(string.format("Processing vehicle: %s", vehicleData.plate)) + end + local position = json.decode(vehicleData.position) local rotation = json.decode(vehicleData.rotation) @@ -195,18 +199,26 @@ RegisterNetEvent('vehicle-persistence:client:spawnSavedVehicles', function(vehic modelHash = GetHashKey(modelHash) end + if Config.Debug then + print(string.format("Requesting model: %s (Hash: %s)", vehicleData.model, modelHash)) + end + RequestModel(modelHash) local timeout = 0 - while not HasModelLoaded(modelHash) and timeout < 50 do + while not HasModelLoaded(modelHash) and timeout < 100 do Wait(100) timeout = timeout + 1 end if HasModelLoaded(modelHash) then + if Config.Debug then + print(string.format("Model loaded, creating vehicle at: %.2f, %.2f, %.2f", position.x, position.y, position.z)) + end + local vehicle = CreateVehicle(modelHash, position.x, position.y, position.z, rotation.z, true, false) if DoesEntityExist(vehicle) then - Wait(500) + Wait(1000) -- Längere Wartezeit -- Setze Fahrzeugdaten SetVehicleNumberPlateText(vehicle, vehicleData.plate) @@ -222,22 +234,33 @@ RegisterNetEvent('vehicle-persistence:client:spawnSavedVehicles', function(vehic -- Setze Mods if vehicleData.mods then - local mods = json.decode(vehicleData.mods) - SetVehicleMods(vehicle, mods) + local success, mods = pcall(json.decode, vehicleData.mods) + if success then + SetVehicleMods(vehicle, mods) + end end -- Verhindere Despawn SetEntityAsMissionEntity(vehicle, true, true) SetVehicleHasBeenOwnedByPlayer(vehicle, true) + SetVehicleOnGroundProperly(vehicle) playerDrivenVehicles[vehicleData.plate] = vehicle if Config.Debug then print(string.format("Successfully spawned saved vehicle: %s", vehicleData.plate)) end + else + if Config.Debug then + print(string.format("Failed to create vehicle: %s", vehicleData.plate)) + end end SetModelAsNoLongerNeeded(modelHash) + else + if Config.Debug then + print(string.format("Failed to load model for vehicle: %s", vehicleData.plate)) + end end end) else @@ -245,6 +268,10 @@ RegisterNetEvent('vehicle-persistence:client:spawnSavedVehicles', function(vehic playerDrivenVehicles[vehicleData.plate] = existingVehicle SetEntityAsMissionEntity(existingVehicle, true, true) SetVehicleHasBeenOwnedByPlayer(existingVehicle, true) + + if Config.Debug then + print(string.format("Vehicle already exists: %s", vehicleData.plate)) + end end end end) @@ -262,10 +289,32 @@ end -- Lade Fahrzeuge beim Spawn RegisterNetEvent('QBCore:Client:OnPlayerLoaded', function() - Wait(10000) + if Config.Debug then + print("Player loaded, waiting before loading vehicles...") + end + + Wait(15000) -- Längere Wartezeit + + if Config.Debug then + print("Loading vehicles...") + end + TriggerServerEvent('vehicle-persistence:server:loadVehicles') end) +-- Lade Fahrzeuge auch beim Resource Start (falls Spieler bereits online) +CreateThread(function() + Wait(20000) -- Noch längere Wartezeit beim Resource Start + + local playerData = QBCore.Functions.GetPlayerData() + if playerData and playerData.citizenid then + if Config.Debug then + print("Resource started, loading vehicles for existing player...") + end + TriggerServerEvent('vehicle-persistence:server:loadVehicles') + end +end) + -- jg-advanced-garage Events RegisterNetEvent('jg-advancedgarages:client:vehicle-stored', function(data) if data and data.plate and playerDrivenVehicles[data.plate] then @@ -285,6 +334,14 @@ RegisterNetEvent('jg-advancedgarages:client:vehicle-spawned', function(data) end end) +-- Debug Command zum manuellen Laden +RegisterCommand('loadvehicles', function() + if Config.Debug then + print("Manual vehicle load triggered...") + TriggerServerEvent('vehicle-persistence:server:loadVehicles') + end +end, false) + -- Cleanup beim Disconnect AddEventHandler('onResourceStop', function(resourceName) if resourceName == GetCurrentResourceName() then