diff --git a/resources/[jobs]/[weapons]/force-sling/client/events.lua b/resources/[jobs]/[weapons]/force-sling/client/events.lua index 45446c270..5aa09f79a 100644 --- a/resources/[jobs]/[weapons]/force-sling/client/events.lua +++ b/resources/[jobs]/[weapons]/force-sling/client/events.lua @@ -4,6 +4,7 @@ local function cleanupEntities() if IsEntityAttachedToAnyPed(entity) then DetachEntity(entity, true, true) end + NetworkUnregisterNetworkedEntity(entity) DeleteObject(entity) SetEntityAsNoLongerNeeded(entity) return true @@ -28,17 +29,25 @@ local function cleanupEntities() collectgarbage("collect") end -AddEventHandler("onResourceStop", function(resource) - if resource ~= GetCurrentResourceName() then - return +-- Add these event handlers +AddEventHandler('onResourceStart', function(resourceName) + if resourceName == GetCurrentResourceName() then + cleanupEntities() end +end) - Debug("info", "Resource stopping: " .. resource) +RegisterNetEvent('QBCore:Client:OnPlayerLoaded') +AddEventHandler('QBCore:Client:OnPlayerLoaded', function() cleanupEntities() - Debug("info", "Resource stopped: " .. resource) +end) + +AddEventHandler('onResourceStop', function(resourceName) + if resourceName == GetCurrentResourceName() then + cleanupEntities() + end end) AddEventHandler('playerDropped', function() cleanupEntities() - Debug("info", "Player dropped") end) + diff --git a/resources/[jobs]/[weapons]/force-sling/client/functions.lua b/resources/[jobs]/[weapons]/force-sling/client/functions.lua index b340d626d..dca526d37 100644 --- a/resources/[jobs]/[weapons]/force-sling/client/functions.lua +++ b/resources/[jobs]/[weapons]/force-sling/client/functions.lua @@ -95,7 +95,10 @@ function Sling:WeaponThread() Sling.cachedAttachments[weaponName] = {} end - if weapon == weaponVal.name then + -- Check if player is in vehicle + local isInVehicle = IsPedInAnyVehicle(playerPed, false) + + if weapon == weaponVal.name or isInVehicle then if DoesEntityExist(Sling.cachedAttachments[weaponName].obj) then Utils:DeleteWeapon(weaponName) end @@ -113,14 +116,42 @@ function Sling:WeaponThread() end CreateThread(function() + local lastVehicleState = false + while true do local weapon = GetSelectedPedWeapon(cache.ped) + local isInVehicle = IsPedInAnyVehicle(cache.ped, false) + + -- Check for vehicle state change + if lastVehicleState ~= isInVehicle then + lastVehicleState = isInVehicle + if isInVehicle then + -- Remove all weapons when entering vehicle + for weaponName, _ in pairs(Sling.cachedAttachments) do + if DoesEntityExist(Sling.cachedAttachments[weaponName].obj) then + Utils:DeleteWeapon(weaponName) + end + end + else + -- Reattach weapons when exiting vehicle + for weaponName, weaponVal in pairs(Sling.cachedWeapons) do + if not DoesEntityExist(Sling.cachedAttachments[weaponName]?.obj) then + local coords = Sling.cachedPositions[weaponName] or Sling.cachedPresets[weaponName] or + { coords = { x = 0.0, y = -0.15, z = 0.0 }, rot = { x = 0.0, y = 0.0, z = 0.0 }, boneId = DEFAULT_BONE } + Utils:CreateAndAttachWeapon(weaponName, weaponVal, coords, cache.ped) + end + end + end + end + while Sling.inPositioning do Wait(1000) end - for weaponName, weaponVal in pairs(Sling.cachedWeapons) do - handleWeaponAttachment(weaponName, weaponVal, cache.ped, weapon) + if not isInVehicle then + for weaponName, weaponVal in pairs(Sling.cachedWeapons) do + handleWeaponAttachment(weaponName, weaponVal, cache.ped, weapon) + end end Wait(1000)