#running math on custom block usage

14 messages · Page 1 of 1 (latest)

covert vine
#
BlockEvents.rightClicked(event => {
  let item = event.player.getHeldItem('main_hand');
  
  if(event.block.id == 'kubejs:xp_atm'){
    coins_convert([1000, 250, 100, 50, 10], convert_xp_levels(event.player.xpLevel) + event.player.xp).forEach(coin => {
      if (coin == 10){
        event.player.give("minecraft:oak_button")
      }else if (coin == 50){
        event.player.give("minecraft:acacia_button")
      }else if (coin == 100){
        event.player.give("minecraft:birch_button")
      }else if (coin == 250){
        event.player.give("minecraft:jungle_button")
      }else if (coin == 1000){
        event.player.give("minecraft:stone_button")
      }
    })
    event.player.xp = 0
  }
})
function convert_xp_levels(xp_levels){
  if (xp_levels <= 16){
    return Math.pow(xp_levels, 2) + 6 * xp_levels
  }else if(xp_levels <= 31){
    return 2.5 * Math.pow(xp_levels, 2) - 40.5 * xp_levels + 360
  }else{
    return 4.5 * Math.pow(xp_levels, 2) - 162.5 * xp_levels + 2220
  }
}

function coins_convert(coins, amount) {
  let result_coins = []  
  coins.forEach(coin => {
    let amount_this_coin = Math.floor(amount / coin)
    result_coins = result_coins.concat(Array(amount_this_coin).fill(coin))
    amount -= amount_this_coin * coin
  })
  return amount == 0 ? result_coins : -1
}

error im getting is

#5: Error occurred while handling event 'BlockEvents.rightClicked': TypeError: Cannot find function forEach in object -1. (server_scripts:xpconvert.js#5)

fading sierraBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

sonic anchor
#

coins_convert() is returning -1 which is not a list

#

then line 5 tries to run -1.forEach()

#

which is not a thing

#

JS basics :p

covert vine
#

but i have 23 levels?

#

it shouldnt be doing -1

#

wait so it works with 1

#

but not 10

sonic anchor
#

always debug your code

#

console.log() is your best friend

lone frigate
covert vine
#

so ill have to add a 1