I am trying to make this simple loot system to work but I keep getting this TypeError and I have tried everything I could think of.
Everything is explained in the code I added below. It was working yesterday but it seems like today is no longer working...
Here is the code in the JS file:
console.info("Working!");
var items;
//here I get the json
$.getJSON('js/items.json', function(data) {
return items = data;
});
//run the function cuz laziness to put it every time in the console
loot();
function loot() {
//these are the drops from the mob
const ids = [1, 2, 3, 4, 5, 6, 7];
//define a random number to see what loot it drops
let rand = Math.floor((Math.random() * 100) + 1);
var loot;
//if the chance is between 1 and 5(including them) do that
if (rand >= 1 && rand <= 5) {
var legends = [];
//here I loop inside the ids constant to see what items are legendary and if they are push them to the legends array
for (var id in ids) {
//HERE IS THE PROBLEM I get Uncaught TypeError here and I have tried everything
if (items[ids[id]].rarity == "Legendary") {
legends.push(ids[id]);
};
};
console.log(`legends: ${legends}`);
//then I random the items inside legends array to get the loot
loot = legends[Math.floor(Math.random() * legends.length)];
};
console.warn(`You looted a ${items[loot].name} | ${items[loot].rarity}`);
};
and here is the JSON file:
{
"1": {
"id": 1,
"name": "Sword of Heaven",
"rarity": "Legendary"
},
"2": {
"id": 2,
"name": "Wooden Sword",
"rarity": "Common"
},
"3": {
"id": 3,
"name": "Glass of the Gods",
"rarity": "Rare"
},
"4": {
"id": 4,
"name": "Minor HP Potion",
"rarity": "Common"
},
"5": {
"id": 5,
"name": "The Enchiridion!",
"rarity": "Legendary"
},
"6": {
"id": 6,
"name": "Major MP Potion",
"rarity": "Rare"
},
"7": {
"id": 7,
"name": "Helm of the Forsaken",
"rarity": "Rare"
}
}