#Trying to merge this with a rank system

1 messages · Page 1 of 1 (latest)

severe bobcat
#

I'm trying to merge a nametag system with a rank system, however my issue is I have no good way to retrieve players and their scoreboards. This is what I have currently:

system.runInterval(async => {
//    const players = world.getPlayers
    const white = world.getPlayers(({scoreOptions: [{minScore:0, maxScore:0, objective:"Team"}]}));
    const red = world.getPlayers(({scoreOptions: [{minScore:1, maxScore:1, objective:"Team"}]}));
    const blue = world.getPlayers(({scoreOptions: [{minScore:2, maxScore:2, objective:"Team"}]}));
//    const objective2 = world.scoreboard.getObjective("Rank")
//    const score2 = objective2.getScore(players);

//    if (score2 == 0) {
//        player.setDynamicProperty('playerRank', '')
//    }
//    const playerRank = player.getDynamicProperty('playerRank')
 for (const player of white) {
    player.nameTag = player.name;
 }
 for (const player of red) {
    player.nameTag = '§c' + player.name + '§7';
 }
 for (const player of blue) {
    player.nameTag = '§b' + player.name + '§7';
 }
0}
)

The commented code should work if I was able to retrieve the players in a better way. I'm really new to scripting so I'm not sure how to get this to work properly.

I was thinking of getting all players to execute a scriptevent, however I'd think there is a better way to do that. So I'm coming here first.

#

I just need a better way to retrieve players and their scoreboards mostly.

blazing coral
#

Dont use runInterval, make it like it gets set when they are joining the game and after switching teams, so it dont need to be runned everytime

severe bobcat
#

I figured runInterval was not optimized at all, but I have no way to detect if a score changes, or scores in a good way at all really

blazing coral
#

you could base the whole system on JS, would be better but ive read that you are new to JS

severe bobcat
#

Yeah..

#

I'm not very experienced at any of this

#

I could maybe make it do where once a scriptevent is sent it updates it. But I've had issues with getting scriptevent to work with retreiving player related data

blazing coral
#

right

#

would be a good option

severe bobcat
#

evd.initator and evd.sourceEntity seemed to have issues for whatever reason with working and I honestly don't know why

blazing coral
#

ill send a code soon

severe bobcat
#

Ah alright

#

when I tried using evd.sourceEntity it only returns "[object Object]" and evd.initiator returns undefined so I just dont really know what I'm doing wrong

blazing coral
#

Object Object is the players object

#

[Object object]

#

Means its an object, with more objects inside

#

Player is an object, with more objects

#

like
player.onScreen.setActionBar(...)

severe bobcat
#

So this would then solve the running constantly, but I still have no idea how to retrieve scoreboards properly

blazing coral
#

try executen the command in the npc as the player

#

Like:

execute as <player> run scriptevent ....
severe bobcat
#

I did that directly in chat. So I mean wouldn't it automatically be me?

blazing coral
#

hmm, it should be you

severe bobcat
#

It returns the same thing when executing as a player

blazing coral
#

try

evd.sourceEntity.name
severe bobcat
#

Okay yeah better now it returns the name

blazing coral
#

it cant display an object, thats why you got "[object object]

#

Yes then you can see if its you

#

@severe bobcat i found something interesting

severe bobcat
#

Hm?

#

So we're getting the player name but it fails to resolve a identity when attempting to access a score, could be because we only have the name

#

Ah okay it seems to work now actually when just using evd.sourceEntity

#

(as in for retrieving score data)

blazing coral
#

The values you are getting [read it]

export class ScriptEventCommandMessageAfterEvent {
    private constructor();
    /**
     * @remarks
     * Identifier of this ScriptEvent command message.
     *
     */
    readonly id: string;
    /**
     * @remarks
     * If this command was initiated via an NPC, returns the entity
     * that initiated the NPC dialogue.
     *
     */
    readonly initiator?: Entity;
    /**
     * @remarks
     * Optional additional data passed in with the script event
     * command.
     *
     */
    readonly message: string;
    /**
     * @remarks
     * Source block if this command was triggered via a block
     * (e.g., a commandblock.)
     *
     */
    readonly sourceBlock?: Block;
    /**
     * @remarks
     * Source entity if this command was triggered by an entity
     * (e.g., a NPC).
     *
     */
    readonly sourceEntity?: Entity;
    /**
     * @remarks
     * Returns the type of source that fired this command.
     *
     */
    readonly sourceType: ScriptEventSource;
}
blazing coral
#

You can use this maybe:

// # region [ imports ]
import { world, system } from '@minecraft/server';
// #endregion

//? add objective
const TeamObjective = world.scoreboard.getObjective('Team');
if (!TeamObjective) { throw new Error('Team objective doesnt exists!'); };

//? team colors
const TeamColors = {
    0: '§a', // white
    1: '§a', // red
    2: '§a', // blue
};

//? the default color
const defaultColor = '§7';

function getColoredName (player) {
    try {
        //* get score
        const teamNumber = TeamObjective.getScore(player);
        //* get color prefix
        const colorPrefix = TeamColors[teamNumber] || defaultColor;
        //* color name and return
        return `${colorPrefix}${player.name}§r`;
    } catch {
        //* if player dont haves a score, it wiill throw an error
        return `${defaultColor}${player.name}§r`;
    };
};

//? after player spawn
world.afterEvents.playerSpawn.subscribe(({ player, initialSpawn }) => {
    if (initialSpawn) {
        //* color name after initialSpawn
        player.nameTag = getColoredName(player);
    };
});

//! trigger this after his score got changed
//? trigger color change, must be executen as player:
//? /execute as <player> run scriptevent "pfx:colorChanged" ""
system.afterEvents.scriptEventReceive.subscribe((evd) => {
    const { initiator, id, message } = evd;

    if (initiator) {
        // switch works like if.. if else.. else
        switch (id) {
            case 'pfx:colorChanged': {
                initiator.nameTag = getColoredName(initiator);
                break;
            };

            case 'pfx:otherEvent': {
                // other code
                break;
            };
        };
    };
}, {
    namespaces: [
        'pfx' // means only when the id namespace starts with "pfx" it will be executed
              // like: /scriptevent "pfx:colorChanged" ""
    ]
});
severe bobcat
blazing coral
#

ohh my bad

#

it works with evd.sourceEntity because to resolve, it needs the player, and not his name

severe bobcat
#

Okay that makes sense

blazing coral
#

you could also use
evd.sourceEntity.scoreboardIdentity to resolve

severe bobcat
#

I'm doing just

        const player = evd.sourceEntity
        const objective = world.scoreboard.getObjective("Rank")
        const score = objective.getScore(player);
        world.sendMessage(`${score}`);

this rn probably do if (score == 0) and change it from there

blazing coral
#

if you want to do ranks, do it with tags

severe bobcat
#

Im using dynamic properties for ranks

blazing coral
#

Or like this

#

sometimes dynamic properties are annoing

#

annoying*

severe bobcat
#

I'm eventually going to be trying to use only dynamic properties mostly for the pros of them

#

Since I've had scoreboards bug out alot on me before

blazing coral
#

I made a Scoreboard Database ( only because i dont want to lose datas after changing the pack )

severe bobcat
#

Ah

#

I just heard its alot faster as well overall. Which I've had my share of lag when messing with things. So any optimizations i'd take

blazing coral
#

Maybe i should use dynamic property

severe bobcat
#

Realistically I could if I ever need just change the players dynamicProperty with another scriptevent. So it can be used like...identically to scoreboards I think

blazing coral
#

yep

severe bobcat
#

I might keep the teams as a score though for the sake of detecting them via commands

blazing coral
#

for a beginner its a good option

#

you could also use tags

severe bobcat
#

I try to only use tags when its like

#

Unneccessary to use scoreboards, being for example a 1 or 0 lol

blazing coral
#

hmm right, and with scoreboards you can alaways reset them

severe bobcat
#

Yeah I just find them easier to work with when it comes to messing with multiple states, such as how teams can be

blazing coral
#

right

severe bobcat
#

I don't want to have like TeamRed, TeamBlue, and TeamDefault. potentially even more depending on how many teams you use

blazing coral
#

relatable

#

best option would be a DataBase ( scoreboard / dynamicProperty ) so you can have like endless teams ( isnt needed ig )

severe bobcat
#

Yeah I'm thinking I might do that to make things easier

blazing coral
#

isnt that hard to make a small database

#

can't be harder than making a perfect minecraft circle by script ( i didnt got it, eve with chatGPT )

severe bobcat
#

Yeah lol

#

I still find it crazy how good the worldedit script is

blazing coral
#

i need a worldedit to steal script

#

anyways

#

do you still need help with anything?

severe bobcat
#

Nope! I appreciate the help