#Reading scoreboard objectives in JSON UI

1 messages · Page 1 of 1 (latest)

muted radish
#

I'm making a profile ui for my Minecraft server, but I'm unable to get the death counter to work because I have no clue how to get my scoreboard objective's value. Anyone know how? (In the attachment the Death: 0 is just a place holder, it doesnt do anything)

neon temple
#
function getScore(target, objective, useZero = false) {
    try {
        const oB = world.scoreboard.getObjective(objective);
        if (typeof target == 'string') {
            return oB.getScore(oB.getParticipants().find(pT => pT.displayName == target));
        }
        return oB.getScore(target.scoreboardIdentity);
    } catch {
        return useZero ? 0 : NaN;
    }
}
```just paste this in ur scripts file, then in ur string just do ${getScore(player, "objectiveName")}
muted radish
neon temple
muted radish
neon temple
#

well isnt ur json ui opened from scripts

#

custom form

muted radish
#

Yea but I thought you needed to use binding

neon temple
#

oh no lmao

muted radish
#

Thanks for helping!

neon temple
#

this is how i do mine and kinda see it at the top right

muted radish
#

Its not a button its a body

neon temple
#

then just add it in their and ur good to go

muted radish
neon temple
#

uhh

#

😭

#

u do know what JS is right

muted radish
#

yes

neon temple
#

okay and u script right

muted radish
muted radish
neon temple
#

okay so how are you opened that UI

#

is that it there mainUI?

muted radish
#

Ok

#

When you click profile, you get sent to that page

neon temple
muted radish
#

so gui.js?

neon temple
#

nono the profile

muted radish
#

or server_form

neon temple
#

not json ui

muted radish
neon temple
#

oh

#

okay show the code

#

of the profile

muted radish
neon temple
#

oh lord okay

muted radish
#

(I got cody to comment everything)

neon temple
#

rq make a config.js file

muted radish
#

In root or script?

neon temple
#

this is like a utility file for stuff that might need to be used in many fles

neon temple
muted radish
#

Gotcha

#

done

neon temple
#

then paste this code in their

export function getScore(target, objective, useZero = false) {
    try {
        const oB = world.scoreboard.getObjective(objective);
        if (typeof target == 'string') {
            return oB.getScore(oB.getParticipants().find(pT => pT.displayName == target));
        }
        return oB.getScore(target.scoreboardIdentity);
    } catch {
        return useZero ? 0 : NaN;
    }
}
muted radish
#

cooked

neon temple
#

?

muted radish
#

you edited your messaged 3 times

neon temple
#

oh yeh remove the js

muted radish
#

got it

neon temple
#

import { world } from "@minecraft/server";

#

add that to the top of taht file

#

because we need world for world.scoreboard

muted radish
#

should I import config.js from my main.js?

neon temple
#

nono okay so because we are exporting the fucntion getScore

#

we now import that into gui.js

#

so we can get the getScore function

muted radish
#

ah gotcha (I started Script Api on may 10th)

neon temple
#

all good nw

#

im just etraching

#

teaching

muted radish
#

this is what I need rn

neon temple
#

reason i made u make a config file is so u dont have to have the getScore function in multiple files

#

just cleaner code

muted radish
#

Oh so should I delete the old code you sent in main.js?

neon temple
#

so importing it into ur gui.js will look like import { getScore } from "filepathing"

muted radish
neon temple
#

its already in config so we can import it into afile when we need to use it

muted radish
#

Got it!

neon temple
#

so have u imported it into your gui.js?

muted radish
#

Yup!

neon temple
#

make sure the filepathing is correct too

muted radish
neon temple
#

okay show the code for the profile again

muted radish
neon temple
#

i think that is just cant tell

#

can u vc? this would be alot easier

muted radish
muted radish
neon temple
#

?

muted radish
neon temple
#

oh okay

#

ima try and clean up ur code alot

muted radish
muted radish
#

{
"deaths_panel": {
"type": "panel",
"size": ["30%", "100%"],
"anchor_from": "center",
"anchor_to": "center",
"controls": [
{
"deaths_icon": {
"type": "image",
"size": [24, 24],
"offset": [0, -15],
"texture": "textures/ui/icon_recipe_nature",
"layer": 2
}
},
{
"deaths_label": {
"type": "label",
"text": "Deaths",
"color": [1.0, 0.3, 0.3],
"offset": [0, 5],
"layer": 2
}
},
{
"deaths_value": {
"type": "label",
"text": "",
"color": [1.0, 0.3, 0.3],
"offset": [0, 25],
"layer": 2
}
}
]
}
},

neon temple
muted radish
# neon temple

Yo! Js wanted to tell you (with your help) I got it to have multiple bodys!

neon temple
uncut horizon
#

Like just to get the players score from that specific scoreboard