#Increasing chance with Math.random and item count in inventory

47 messages · Page 1 of 1 (latest)

south solar
#

I have a basic code for getting diamonds (with chance) when right clicking dirt. My goal is to increase the chance (sum or multiply) by the amount of diamonds i have in my inventory.

here is my current code

onEvent('block.right_click', event => {
    let diamondcount = block.createEntity('minecraft:diamond')
    if (event.block.id != 'minecraft:dirt' && event.item.id != Item.empty || Math.random() > 0.01 + event.diamondcount.getCount() ) return
    event.block.set('dirt')
    event.block.popItem('diamond')
    })```
twilit needleBOT
#

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

tawdry geyser
#

??helpyou

limber prairieBOT
# tawdry geyser ??helpyou

╰( ͡° ͜ʖ ͡° )つ──☆:・゚˜”°•.˜”°• Many Help •°”˜.•°”˜*
Please provide a description of your issue with as much detail as possible. If you have an issue with a script provide the script. Explain what you can see happening and what you expect to happen. Be specific!

Tell us what is happening, we already know it "doesn't work".
Avoid using words like "it", tell us exactly what "it" is.
Don't assume anyone knows what you are talking about, be specific.

Provide screenshots or video of the issue, not the code or chat, if possible.

Provide the log.

south solar
#

The base chance is 0.01, if i have 1 diamond that chance increases to 0.02, 3 diamonds 0.03 and so on

#

A full stack and the chance is 0.64

#

Math.random() > 0.01 + event.diamondcount.getCount()

#

What i'm trying to do is math, 0.01 plus the amount of items in the player inventory

#

diamonds specifically

tawdry geyser
#

try ```js
Math.min([0.01 * player.inventory.count("diamond"), 0.64])

south solar
#

Do i have to include the amount?

lavish wagon
#

no hmmm

south solar
#

Yeah i don't think the code worked

lavish wagon
#

??kjslogs

limber prairieBOT
# lavish wagon ??kjslogs

You can find your KubeJS server log in /minecraft/logs/kubejs/server.log.
If you are on 1.18 or below it will be called server.txt.
Please send it if asked, as it contains helpful information.

south solar
#

[12:57:06] [ERR ] Error occurred while handling event 'block.right_click': ReferenceError: "player" is not defined. (server_scripts:rightclickinteract.js#4)

lavish wagon
#

ah yes

#

send your script

#

??code

limber prairieBOT
# lavish wagon ??code

🗒️**Send the code!**🗒️
You may have an issue with a KubeJS script and you explain it to the best of your ability yet without the actual code in question we have very little to go off of in trying to assist you.

south solar
#

deleted the diamondcount variable but here you go

onEvent('block.right_click', event => {
    if (event.block.id != 'minecraft:dirt' && event.item.id != Item.empty || Math.min([0.01 * player.inventory.count("diamond")]) ) return
    event.block.set('dirt')
    event.block.popItem('diamond')
    })```
lavish wagon
#

try this:

onEvent('block.right_click', e => {
    if (e.block.id != 'minecraft:dirt' && e.item.id != Item.empty || Math.min([0.01 * e.player.inventory.count("diamond")])) return
    e.block.set('dirt')
    e.block.popItem('diamond')
})
south solar
#

Ok 1 sec

#

Btw currently have 64 diamonds in my inventory

#

Yeah, also does nothing. let me check the log

#

There's also getCount

lavish wagon
south solar
#

Yeah that code also doesn't work, neither with .count or .getCount

#

.count gives me no errors but .getCount does

limber prairieBOT
#

Paste version of server.txt from @south solar

lavish wagon
south solar
#

I mean, it gave me an interesting error tbh lol

lavish wagon
#

there are like... no errors in your log?

south solar
#

i mean the code does nothing

lavish wagon
south solar
#

getCount fetched all the items in my inventory

#

One sec let me try something

south solar
#

I got it!

#

here's the code

lavish wagon
#

where? despair

south solar
#
onEvent('block.right_click', event => {
    if (event.block.id != 'minecraft:dirt' && event.item.id != Item.empty || Math.random() > 0.01 * event.player.inventory.count('diamond')) return
    event.block.set('dirt')
    event.block.popItem('diamond')
})```
lavish wagon
#

??closeticket

limber prairieBOT
# lavish wagon ??closeticket

Please close your ticket (with </ticket close:1054771505520717835> or the button atop this thread) once you resolved your issue! This also helps others that would like to help out, as they don't have to look into this thread to check if it has been resolved by now.

Do you have any other questions regarding your issue? Feel free to ask!
Note: You generally should create a new post for unrelated issues.