Utils = {} function Utils:CreateAndAttachWeapon(weaponName, weaponVal, coords, playerPed) if Sling.currentAttachedAmount >= Config.MaxWeaponsAttached then Debug("warn", "Max weapons attached reached") return false end if not weaponVal or not weaponVal.name then Debug("error", "Invalid weapon data") return false end -- Initialize the cached attachments for this weapon if it doesn't exist if not Sling.cachedAttachments[weaponName] then Sling.cachedAttachments[weaponName] = {} end local weaponObject = CreateWeaponObject(weaponVal.name, 0, coords.coords.x, coords.coords.y, coords.coords.z, true, 1.0, 0) if not weaponObject then Debug("error", "Failed to create weapon object") return false end if NetworkGetEntityIsNetworked(weaponObject) then NetworkUnregisterNetworkedEntity(weaponObject) end SetEntityCollision(weaponObject, false, false) if Config.UseWeaponAttachments then weaponVal.attachments = Inventory:GetWeaponAttachment(weaponName) end for _, component in pairs(weaponVal.attachments or {}) do GiveWeaponComponentToWeaponObject(weaponObject, component) end lib.requestModel(weaponVal.model) local placeholder = CreateObjectNoOffset(weaponVal.model, coords.coords.x, coords.coords.y, coords.coords.z, true, true, false) SetEntityCollision(placeholder, false, false) SetEntityAlpha(placeholder, 0, false) local boneIndex = GetPedBoneIndex(playerPed, (coords.boneId or DEFAULT_BONE)) AttachEntityToEntity(placeholder, playerPed, boneIndex, coords.coords.x, coords.coords.y, coords.coords.z, coords.rot.x, coords.rot.y, coords.rot.z, true, true, false, true, 2, true) AttachEntityToEntity(weaponObject, placeholder, GetEntityBoneIndexByName(placeholder, "gun_root"), 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, true, true, false, true, 2, true) Sling.cachedAttachments[weaponName].obj = weaponObject Sling.cachedAttachments[weaponName].placeholder = placeholder Sling.currentAttachedAmount = Sling.currentAttachedAmount + 1 SetModelAsNoLongerNeeded(weaponVal.model) return true end function Utils:DeleteWeapon(weaponName) if not Sling.cachedAttachments[weaponName] then return end local attachment = Sling.cachedAttachments[weaponName] if attachment.obj and DoesEntityExist(attachment.obj) then if NetworkGetEntityIsNetworked(attachment.obj) then NetworkUnregisterNetworkedEntity(attachment.obj) end DeleteObject(attachment.obj) end if attachment.placeholder then if IsEntityAttachedToAnyPed(attachment.placeholder) then DetachEntity(attachment.placeholder, true, false) end if DoesEntityExist(attachment.placeholder) then DeleteObject(attachment.placeholder) end end Sling.cachedAttachments[weaponName] = nil Sling.currentAttachedAmount = math.max(0, Sling.currentAttachedAmount - 1) end