diff --git a/resources/[jobs]/[weapons]/force-sling/client/events.lua b/resources/[jobs]/[weapons]/force-sling/client/events.lua index 82c99fe56..1debd244c 100644 --- a/resources/[jobs]/[weapons]/force-sling/client/events.lua +++ b/resources/[jobs]/[weapons]/force-sling/client/events.lua @@ -12,57 +12,73 @@ local function cleanupEntities() return false end - if Sling.object then - safeDelete(Sling.object) - Sling.object = nil - end - - for weaponName, attachment in pairs(Sling.cachedAttachments) do - if attachment then - safeDelete(attachment.obj) - safeDelete(attachment.placeholder) - Sling.cachedAttachments[weaponName] = nil + -- Sicherheitsprüfung für Sling + if Sling then + if Sling.object then + safeDelete(Sling.object) + Sling.object = nil end + + if Sling.cachedAttachments then + for weaponName, attachment in pairs(Sling.cachedAttachments) do + if attachment then + safeDelete(attachment.obj) + safeDelete(attachment.placeholder) + Sling.cachedAttachments[weaponName] = nil + end + end + end + + Sling.currentAttachedAmount = 0 end -- Cleanup other players weapons - for playerId, weapons in pairs(otherPlayersWeapons) do - for weaponName, _ in pairs(weapons) do - if Sling.cachedAttachments[weaponName] then - safeDelete(Sling.cachedAttachments[weaponName].obj) - safeDelete(Sling.cachedAttachments[weaponName].placeholder) + if otherPlayersWeapons then + for playerId, weapons in pairs(otherPlayersWeapons) do + for weaponName, _ in pairs(weapons) do + if Sling and Sling.cachedAttachments and Sling.cachedAttachments[weaponName] then + safeDelete(Sling.cachedAttachments[weaponName].obj) + safeDelete(Sling.cachedAttachments[weaponName].placeholder) + end end + otherPlayersWeapons[playerId] = nil end - otherPlayersWeapons[playerId] = nil end - Sling.currentAttachedAmount = 0 collectgarbage("collect") end -AddEventHandler('onResourceStart', function(resourceName) - if resourceName == GetCurrentResourceName() then - cleanupEntities() - end -end) +-- Verzögere die Registrierung der Events bis Sling initialisiert ist +CreateThread(function() + while not Sling do + Wait(100) + end -RegisterNetEvent('QBCore:Client:OnPlayerLoaded') -AddEventHandler('QBCore:Client:OnPlayerLoaded', function() - cleanupEntities() -end) + AddEventHandler('onResourceStart', function(resourceName) + if resourceName == GetCurrentResourceName() then + cleanupEntities() + end + end) -AddEventHandler('onResourceStop', function(resourceName) - if resourceName == GetCurrentResourceName() then - cleanupEntities() - end -end) + RegisterNetEvent('QBCore:Client:OnPlayerLoaded') + AddEventHandler('QBCore:Client:OnPlayerLoaded', function() + cleanupEntities() + end) -AddEventHandler('playerDropped', function() - cleanupEntities() + AddEventHandler('onResourceStop', function(resourceName) + if resourceName == GetCurrentResourceName() then + cleanupEntities() + end + end) + + AddEventHandler('playerDropped', function() + cleanupEntities() + end) end) RegisterNetEvent('force-sling:client:syncWeapons') AddEventHandler('force-sling:client:syncWeapons', function(playerId, weaponData, action) + if not Sling then return end if playerId == cache.serverId then return end if action == 'attach' then @@ -91,6 +107,7 @@ end) RegisterNetEvent('force-sling:client:cleanupPlayerWeapons') AddEventHandler('force-sling:client:cleanupPlayerWeapons', function(playerId) + if not Sling then return end if otherPlayersWeapons[playerId] then for weaponName, _ in pairs(otherPlayersWeapons[playerId]) do Utils:DeleteWeapon(weaponName) @@ -101,3 +118,4 @@ end) + diff --git a/resources/[jobs]/[weapons]/force-sling/fxmanifest.lua b/resources/[jobs]/[weapons]/force-sling/fxmanifest.lua index e84105c3d..75fd39f39 100644 --- a/resources/[jobs]/[weapons]/force-sling/fxmanifest.lua +++ b/resources/[jobs]/[weapons]/force-sling/fxmanifest.lua @@ -13,31 +13,28 @@ dependencies { shared_scripts { '@ox_lib/init.lua', - + 'client/shared.lua', -- Moved here to be loaded first "shared/*.lua", "config.lua", } server_scripts { 'version.lua', - "server/events.lua", "server/functions.lua", "server/main.lua", "server/misc/*.lua", "server/callbacks.lua" - } client_scripts { "client/utils.lua", + "client/functions.lua", -- Moved before events "client/events.lua", - "client/functions.lua", "client/main.lua", "client/custom/frameworks/*.lua", "client/custom/*.lua", - "client/misc/*.lua", - 'client/shared.lua', + "client/misc/*.lua" } files {