File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // Script examples for ScriptAPI
2+ // Author: Jayly#1397 <Bedrock Add-Ons>
3+
4+ import { Entity , ScoreboardObjective , DisplaySlotId , world } from "@minecraft/server" ;
5+ /**
6+ * Set entity score and fetch scoreboard objective display
7+ * @param {Entity } entity
8+ * @param {ScoreboardObjective } objective
9+ * @param {number } score
10+ */
11+ function setScore ( entity , objective , score ) {
12+ // If entity doesnt have scoreboard property, run command
13+ if ( ! entity . scoreboard ) entity . runCommandAsync ( 'scoreboard players set @s ' + objective . id + ' ' + score ) ;
14+ else entity . scoreboard . setScore ( objective , score ) ;
15+
16+ // fetch scoreboard objective display
17+ /**
18+ * @type {(keyof typeof DisplaySlotId)[] }
19+ */
20+ const displaySlots = [ DisplaySlotId . belowname , DisplaySlotId . list , DisplaySlotId . sidebar ] ;
21+
22+ for ( const displaySlotId of displaySlots ) {
23+ const displaySlot = world . scoreboard . getObjectiveAtDisplaySlot ( DisplaySlotId [ displaySlotId ] ) ;
24+
25+ if ( displaySlot . objective === objective ) {
26+ world . scoreboard . clearObjectiveAtDisplaySlot ( DisplaySlotId . sidebar ) ;
27+ world . scoreboard . setObjectiveAtDisplaySlot ( DisplaySlotId . sidebar , displaySlot ) ;
28+ }
29+ } ;
30+ } ;
31+
32+ export { setScore } ;
Original file line number Diff line number Diff line change 1+ // Script examples for ScriptAPI
2+ // Author: Jayly#1397 <Jayly Discord>
3+
4+ import { world } from "@minecraft/server" ;
5+ import { setScore } from "./index" ;
6+
7+ const objective = world . scoreboard . getObjective ( 'messages' ) ;
8+
9+ world . events . chat . subscribe ( ( event ) => {
10+ if ( ! event . sender . scoreboard ) return ;
11+
12+ const score = event . sender . scoreboard . getScore ( objective ) ;
13+ setScore ( event . sender , objective , score + 1 ) ;
14+ } )
You can’t perform that action at this time.
0 commit comments