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;
}
}
#Ok so why dose my brake block no work
1 messages · Page 1 of 1 (latest)
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
Ok even thing works but the brake block
Ok so it is the broken block permutation thing then so use that instead of blocking that should work
What is the event iditefier
Bc I dnot know that one
It's just brokenBlockPermutation
Sorry had to go look for I forgot what it was lol
Ok
still dont wor
Then try removing the async because I don't think you're supposed to use async anyway for those
wee
What are you trying to do with the break block
Remove a Score for the said block
But blocks can't have scores as they are not entities unless you created a fake block
thats not what he meant
Yeah I realise that now guess my brain was still asleep when I wrote that
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
or maybe just use before event