Script lose random items EXCEPT key items

Hello, I am creating an event where the player loses random items on gameover and I found this script here

JavaScript:
var items = $gameParty.allItems();
var count = items.length;
if (count) $gameParty.loseItem(items[Math.randomInt(count)], 1);

This script works really well in RMMZ, but it removes key items too. I do not want the player to lose key items, how should I change that script?



And a second request, it's not necessary but I would appreciate it if you can fulfill this too: What if I want to make the player lose half or any other amount of the items he has except key items?
Code:
if ($gameParty.allItems().filter(item => item.itypeId == 2).length < $gameParty.allItems().length)
{
    let itemIndex;

    do
    {
        itemIndex = Math.randomInt($gameParty.allItems().length);
    }
    while ($gameParty.allItems()[itemIndex].itypeId == 2)

    $gameParty.loseItem($gameParty.allItems()[itemIndex], 1);
}

What if I want to make the player lose half or any other amount of the items he has except key items?
What do you mean? Do you mean, like, they have 10 potions and they lose 5? Or of the 72 total items they lose 36? What happens with weapons and armor? Because the code you posted originally will include equipment.
(@ATT_Turan - typo in the while clause: it's itypeId, not itemtypeId.)

Alternative suggestion:
JavaScript:
const sieve = item => item.itypeId !== 2;           // exclude Items in Key category
const items = $gameParty.allItems().filter(sieve);  // items eligible for removal
const count = items.length;                         // total eligible for removal
if (count) {
  const item = items[Math.randomInt(count)];        // item to remove
  const qty  = 1;                                   // quantity to remove
  $gameParty.loseItem(item, qty);
}
Spoiler: reasoning
Storing the allItems().filter result in a variable avoids the need to re-evaluate it multiple times and (just in case) ensures it's always the same item pool, no matter what happens. Referencing the filtered array also means there's no need to re-check validity on selection.

Separating stuff like sieve and minus onto their own lines helps to clarify what each part does, for ease of editing/learning.

The quantity of an item can be obtained using $gameParty.numItems(item), e.g.
JavaScript:
  const qty  = Math.floor($gameParty.numItems(item) / 2);  // half, rounded down
To affect the entire item pool, rather than a random entry from it, a loop can be used, e.g.
JavaScript:
const sieve = item => item.itypeId !== 2;
const items = $gameParty.allItems().filter(sieve);
for (const item of items) {
  const qty = Math.floor($gameParty.numItems(item) / 2);
  $gameParty.loseItem(item, qty);
}
(@ATT_Turan - typo in the while clause: it's itypeId, not itemtypeId.)
Thanks, fixed 😮‍💨
Both of your scripts work really well! Thank you so much😂

To affect the entire item pool, rather than a random entry from it, a loop can be used, e.g.
JavaScript:
const sieve = item => item.itypeId !== 2;
const items = $gameParty.allItems().filter(sieve);
for (const item of items) {
  const qty = Math.floor($gameParty.numItems(item) / 2);
  $gameParty.loseItem(item, qty);
}
This one is nice too but it just takes half of every item's quantity away without it being random. What I need is adding the quantity each item I have together, divide that value by half and that new value is how many times it should pick 1 item at random.
Basically take away half of all items and equipment I have but it's very random which items get picked.

I can do that myself with the first script you made, loops, and variables. I just wanted to see if there is a way to make it easier with one script if it is not too hard for you guys to script
What I need is adding the quantity each item I have together, divide that value by half and that new value is how many times it should pick 1 item at random.
Ah, OK! Maybe this (untested):
JavaScript:
const sieve = item => item.itypeId !== 2;           // exclude Items in Key category
const items = $gameParty.allItems().filter(sieve);  // items eligible for removal
const total = items.reduce(
  (a, c) => a + $gameParty.numItems(c)              // total eligible quantity
, 0);
for (let n = 1 + Math.floor(total / 2); --n;) {     // repeat ⌊total/2⌋ times
  const item = items[Math.randomInt(items.length)];
  $gameParty.loseItem(item, 1);
  if ($gameParty.numItems(item) === 0)              // remove from pool if no more left
    items.splice(items.indexOf(item), 1);
}
Probably could be neater, but I think that'll work. 😴
Ah, OK! Maybe this (untested):
JavaScript:
const sieve = item => item.itypeId !== 2;           // exclude Items in Key category
const items = $gameParty.allItems().filter(sieve);  // items eligible for removal
const total = items.reduce(
  (a, c) => a + $gameParty.numItems(c)              // total eligible quantity
, 0);
for (let n = 1 + Math.floor(total / 2); --n;) {     // repeat ⌊total/2⌋ times
  const item = items[Math.randomInt(items.length)];
  $gameParty.loseItem(item, 1);
  if ($gameParty.numItems(item) === 0)              // remove from pool if no more left
    items.splice(items.indexOf(item), 1);
}
Probably could be neater, but I think that'll work. 😴
It works well!! Thank you so much again 😍
This thread is closed due to being reported as solved. If you need it reopened, report it to let us know. Thank you!

Original thread: https://forums.rpgmakerweb.com/threads/script-lose-random-items-except-key-items.173210/