#Ok so why dose my brake block no work

1 messages · Page 1 of 1 (latest)

keen geyser
#
import { world } from "@minecraft/server";

const GEN_BLOCKS = [
    "es:amethyst_gen",
    "es:clay_gen",
];

const MAX_GENS = 15;

world.afterEvents.playerPlaceBlock.subscribe(async (event) => {
    const { player, block } = event;

    if (!GEN_BLOCKS.includes(block.typeId)) return;

    const currentCount = getScore("totalGens", player);

    if (currentCount >= MAX_GENS) {
        player.sendMessage(`§cYou've reached the max generator limit of ${MAX_GENS}!`);
        const placedBlock = player.dimension.getBlock(block.location);
        if (placedBlock) {
            placedBlock.setType("minecraft:air");
        }
        return;
    }

    await player.runCommand(`scoreboard players add "${player.name}" totalGens 1`);
});

world.afterEvents.playerBreakBlock.subscribe(async (event) => {
    const { player, block } = event;

    if (!GEN_BLOCKS.includes(block.typeId)) return;

    try {
        await player.runCommand(`scoreboard players remove "${player.name}" totalGens 1`);
        player.sendMessage(`§aYou broke a generator block! Reducing your generator count by 1.`);
        
        console.log(`Player ${player.name} broke a generator block. totalGens decreased by 1.`);
    } catch (error) {
        console.error("Error updating scoreboard: ", error);
    }
});

function getScore(objective, target, useZero = true) {
    try {
        const obj = world.scoreboard.getObjective(objective);
        if (!obj) return '';
        if (typeof target === 'string') {
            const participant = obj.getParticipants().find(v => v.displayName === target);
            if (!participant) return 0;
            return obj.getScore(participant.scoreboardIdentity);
        }
        const isParticipant = obj.hasParticipant(target);
        if (!isParticipant) return 0;
        return obj.getScore(target.scoreboardIdentity);
    } catch (error) {
        console.error(error);
        return useZero ? 0 : NaN;
    }
}
potent zealot
# keen geyser ```js import { world } from "@minecraft/server"; const GEN_BLOCKS = [ "es:a...

I have two possible theories first of all I don't know if you can run asynchronous but it seems like it works for place block for you but anyway my other idea is try using broken block permutation instead of block I recently found out that you can come across an issue with the break block event if you use block rather than broken block permutation where it returns air because it's firing after the block is broken rather than the block that was broken if that makes sense sorry for the wordy response

#

@keen geyser

#

So yeah if it's not that asynchronous which I don't think it is try using broken block permutation instead of block and for broken block permutation you're going to need to use matches rather than typeId as it isn't a thing for broken block permutation

keen geyser
potent zealot
potent zealot
#

Sorry had to go look for I forgot what it was lol

keen geyser
potent zealot
potent zealot
keen geyser
potent zealot
potent zealot
potent zealot
# keen geyser Remove a Score for the said block

here is a working script that removes score when you break specifically stone the code ```js
import { world } from "@minecraft/server";

world.afterEvents.playerBreakBlock.subscribe((data) => {
const block = data.block
const player = data.player
const brokenblock = data.brokenBlockPermutation
if (brokenblock.matches("minecraft:stone")) {
player.runCommand("scoreboard players remove @s test 1")
}
})``` and i also have a video of it working in game