It replaces everything in all the other slots but not in the last slot.
I found adding %player.tool[%i] = %ranWeapon; fixes the issue but then the player's inventory doesn't update.
Without that it replaces the item but not the last item.
function QuestionBoxItem::onPickup(%this, %obj, %player){
Parent::onPickup(%this, %obj, %player);
%weaponCount = "";
for(%i=0; %i<DatablockGroup.getCount(); %i++){
%obj = DatablockGroup.getObject(%i);
if(%obj.getClassname() $= "ItemData" && %obj.getName() !$= "QuestionBoxItem")
%weapon[%weaponCount++] = %obj;
}
%ranWeapon = %weapon[getRandom(0,%weaponCount)];
for(%i=0; %i<=4; %i++){
if(%player.tool[%i] == %this){
messageClient(%player,'MsgItemPickup','',%i,%ranWeapon);
break;
}
}
}