1
0
Fork 0
forked from Simnation/Main
This commit is contained in:
Nordi98 2025-06-12 00:07:24 +02:00
parent 857f210323
commit 90cf348723
2 changed files with 49 additions and 9 deletions

View file

@ -4,6 +4,7 @@ local function cleanupEntities()
if IsEntityAttachedToAnyPed(entity) then if IsEntityAttachedToAnyPed(entity) then
DetachEntity(entity, true, true) DetachEntity(entity, true, true)
end end
NetworkUnregisterNetworkedEntity(entity)
DeleteObject(entity) DeleteObject(entity)
SetEntityAsNoLongerNeeded(entity) SetEntityAsNoLongerNeeded(entity)
return true return true
@ -28,17 +29,25 @@ local function cleanupEntities()
collectgarbage("collect") collectgarbage("collect")
end end
AddEventHandler("onResourceStop", function(resource) -- Add these event handlers
if resource ~= GetCurrentResourceName() then AddEventHandler('onResourceStart', function(resourceName)
return if resourceName == GetCurrentResourceName() then
cleanupEntities()
end end
end)
Debug("info", "Resource stopping: " .. resource) RegisterNetEvent('QBCore:Client:OnPlayerLoaded')
AddEventHandler('QBCore:Client:OnPlayerLoaded', function()
cleanupEntities() cleanupEntities()
Debug("info", "Resource stopped: " .. resource) end)
AddEventHandler('onResourceStop', function(resourceName)
if resourceName == GetCurrentResourceName() then
cleanupEntities()
end
end) end)
AddEventHandler('playerDropped', function() AddEventHandler('playerDropped', function()
cleanupEntities() cleanupEntities()
Debug("info", "Player dropped")
end) end)

View file

@ -95,7 +95,10 @@ function Sling:WeaponThread()
Sling.cachedAttachments[weaponName] = {} Sling.cachedAttachments[weaponName] = {}
end 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 if DoesEntityExist(Sling.cachedAttachments[weaponName].obj) then
Utils:DeleteWeapon(weaponName) Utils:DeleteWeapon(weaponName)
end end
@ -113,14 +116,42 @@ function Sling:WeaponThread()
end end
CreateThread(function() CreateThread(function()
local lastVehicleState = false
while true do while true do
local weapon = GetSelectedPedWeapon(cache.ped) 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 while Sling.inPositioning do
Wait(1000) Wait(1000)
end end
for weaponName, weaponVal in pairs(Sling.cachedWeapons) do if not isInVehicle then
handleWeaponAttachment(weaponName, weaponVal, cache.ped, weapon) for weaponName, weaponVal in pairs(Sling.cachedWeapons) do
handleWeaponAttachment(weaponName, weaponVal, cache.ped, weapon)
end
end end
Wait(1000) Wait(1000)