#Could anyone pls help me with this script pls?
1 messages · Page 1 of 1 (latest)
function getScore(target, objective) {
try {
target.runCommand(`scoreboard players add @s ${objective} 0`);
return world.scoreboard.getObjective(objective).getScore(target);
} catch {
return 0;
}
}
system.runInterval(() => {
world.scoreboard.removeObjective(`Koth`)
world.scoreboard.clearObjectiveAtDisplaySlot(DisplaySlotId.Sidebar)
world.scoreboard.addObjective(`Koth`).displayName(`§6KOTH`)
world.scoreboard.setObjectiveAtDisplaySlot(DisplaySlotId.Sidebar, {objective: `Koth`})
world.sendMessage(`§6The Koth event has started!`);
}, 24000);
system.runInterval(() => {
for (const player of world.getPlayers()) {
if (getScore(player, "Koth") > 99) {
world.scoreboard.getObjective(`balance`).addScore(player, 5000)
world.sendMessage(`§e${player.name} §6has won the Koth event!`);
world.scoreboard.removeObjective(`Koth`)
world.scoreboard.clearObjectiveAtDisplaySlot(DisplaySlotId.Sidebar)
}
}
}, 20);```
what is the issue exactly
line 13 "world.scoreboard.removeObjective("Koth")
it says errors and it doesnt seem to be working every 20 mins
what the error say
it says it every 20mins
but i can only remember the line number
it was like issue with removing the scoreboard
when idk why?
alr
let me see
function getScore(target, objective) {
try {
target.runCommand(`scoreboard players add @s ${objective} 0`);
return world.scoreboard.getObjective(objective).getScore(target);
} catch {
return 0;
}
}
system.runInterval(() => {
if(world.scoreboard.getObjective(`balance`)) world.scoreboard.removeObjective(`Koth`)
world.scoreboard.clearObjectiveAtDisplaySlot(DisplaySlotId.Sidebar)
world.scoreboard.addObjective(`Koth`).displayName(`§6KOTH`)
world.scoreboard.setObjectiveAtDisplaySlot(DisplaySlotId.Sidebar, {objective: `Koth`})
world.sendMessage(`§6The Koth event has started!`);
}, 24000);
system.runInterval(() => {
for (const player of world.getPlayers()) {
if (getScore(player, "Koth") > 99) {
world.scoreboard.getObjective(`balance`).addScore(player, 5000)
world.sendMessage(`§e${player.name} §6has won the Koth event!`);
if(world.scoreboard.getObjective(`balance`)) world.scoreboard.removeObjective(`Koth`)
world.scoreboard.clearObjectiveAtDisplaySlot(DisplaySlotId.Sidebar)
}
}
}, 20);```
@boreal crown
yh?
you need to check if the objective is there before trying to remove it
if(world.scoreboard.getObjective(balance))
i added it to [the code above](#1306023735471636502 message)
send the full code
ok
import {world,system} from "@minecraft/server";
function getScore(target, objective) {
try {
target.runCommand(`scoreboard players add @s ${objective} 0`);
return world.scoreboard.getObjective(objective).getScore(target);
} catch {
return 0;
}
}
system.runInterval(() => {
if(world.scoreboard.getObjective(`balance`)) world.scoreboard.removeObjective(`Koth`)
world.scoreboard.clearObjectiveAtDisplaySlot(DisplaySlotId.Sidebar)
world.scoreboard.addObjective(`Koth`).displayName(`§6KOTH`)
world.scoreboard.setObjectiveAtDisplaySlot(DisplaySlotId.Sidebar, {objective: `Koth`})
world.sendMessage(`§6The Koth event has started!`);
}, 3000);
system.runInterval(() => {
for (const player of world.getPlayers()) {
if (getScore(player, "Koth") > 99) {
world.scoreboard.getObjective(`balance`).addScore(player, 5000)
world.sendMessage(`§e${player.name} §6has won the Koth event!`);
if(world.scoreboard.getObjective(`balance`)) world.scoreboard.removeObjective(`Koth`)
world.scoreboard.clearObjectiveAtDisplaySlot(DisplaySlotId.Sidebar)
}
}
}, 20);```
import {world,system} from "@minecraft/server";
function getScore(target, objective) {
try {
target.runCommand(`scoreboard players add @s ${objective} 0`);
return world.scoreboard.getObjective(objective).getScore(target);
} catch {
return 0;
}
}
system.runInterval(() => {
if(world.scoreboard.getObjective(`Koth`)) world.scoreboard.removeObjective(`Koth`)
world.scoreboard.clearObjectiveAtDisplaySlot(DisplaySlotId.Sidebar)
world.scoreboard.addObjective(`Koth`).displayName(`§6KOTH`)
world.scoreboard.setObjectiveAtDisplaySlot(DisplaySlotId.Sidebar, {objective: `Koth`})
world.sendMessage(`§6The Koth event has started!`);
}, 3000);
system.runInterval(() => {
for (const player of world.getPlayers()) {
if (getScore(player, "Koth") > 99) {
world.scoreboard.getObjective(`balance`).addScore(player, 5000)
world.sendMessage(`§e${player.name} §6has won the Koth event!`);
if(world.scoreboard.getObjective(`Koth`)) world.scoreboard.removeObjective(`Koth`)
world.scoreboard.clearObjectiveAtDisplaySlot(DisplaySlotId.Sidebar)
}
}
}, 20);
@median mountain
import {world,system} from "@minecraft/server";
function getScore(target, objective) {
try {
target.runCommand(`scoreboard players add @s ${objective} 0`);
return world.scoreboard.getObjective(objective).getScore(target);
} catch {
return 0;
}
}
system.runInterval(() => {
if(world.scoreboard.getObjective(`Koth`)) world.scoreboard.removeObjective(`Koth`)
world.scoreboard.clearObjectiveAtDisplaySlot("Sidebar")
world.scoreboard.addObjective(`Koth`).displayName(`§6KOTH`)
world.scoreboard.setObjectiveAtDisplaySlot("Sidebar", {objective: `Koth`})
world.sendMessage(`§6The Koth event has started!`);
}, 3000);
system.runInterval(() => {
for (const player of world.getPlayers()) {
if (getScore(player, "Koth") > 99) {
world.scoreboard.getObjective(`balance`).addScore(player, 5000)
world.sendMessage(`§e${player.name} §6has won the Koth event!`);
if(world.scoreboard.getObjective(`Koth`)) world.scoreboard.removeObjective(`Koth`)
world.scoreboard.clearObjectiveAtDisplaySlot("Sidebar")
}
}
}, 20);
import {world,system} from "@minecraft/server";
function getScore(target, objective) {
try {
target.runCommand(`scoreboard players add @s ${objective} 0`);
return world.scoreboard.getObjective(objective).getScore(target);
} catch {
return 0;
}
}
system.runInterval(() => {
if(world.scoreboard.getObjective(`Koth`)) world.scoreboard.removeObjective(`Koth`)
world.scoreboard.clearObjectiveAtDisplaySlot("Sidebar")
world.scoreboard.addObjective(`Koth`,`§6KOTH`)
world.scoreboard.setObjectiveAtDisplaySlot("Sidebar", {objective: `Koth`})
world.sendMessage(`§6The Koth event has started!`);
}, 3000);
system.runInterval(() => {
for (const player of world.getPlayers()) {
if (getScore(player, "Koth") > 99) {
world.scoreboard.getObjective(`balance`).addScore(player, 5000)
world.sendMessage(`§e${player.name} §6has won the Koth event!`);
if(world.scoreboard.getObjective(`Koth`)) world.scoreboard.removeObjective(`Koth`)
world.scoreboard.clearObjectiveAtDisplaySlot("Sidebar")
}
}
}, 20);
@median mountain
import {world,system} from "@minecraft/server";
const koth_scoreboard = world.scoreboard.getObjective(`Koth`) || world.scoreboard.addObjective(`Koth`,`§6KOTH`);
const BALANCE_SCOREBOARD = world.scoreboard.getObjective(`balance`);
system.runInterval(() => {
if (koth_scoreboard) {
world.scoreboard.removeObjective(`Koth`)
world.scoreboard.clearObjectiveAtDisplaySlot("Sidebar")
}
world.scoreboard.setObjectiveAtDisplaySlot("Sidebar", {objective: koth_scoreboard})
world.sendMessage(`§6The Koth event has started!`);
}, 3000);
system.runInterval(() => {
for (const player of world.getPlayers()) {
if (getScore(player, `Koth`) > 99) {
BALANCE_SCOREBOARD.addScore(player, 5000);
world.sendMessage(`§e${player.name} §6has won the Koth event!`);
world.scoreboard.removeObjective(`Koth`);
world.scoreboard.clearObjectiveAtDisplaySlot("Sidebar")
}
}
}, 20);
import {world,system} from "@minecraft/server";
const koth_scoreboard = world.scoreboard.getObjective(`Koth`) || world.scoreboard.addObjective(`Koth`,`§6KOTH`);
const BALANCE_SCOREBOARD = world.scoreboard.getObjective(`balance`)??world.scoreboard.addObjective(`balance`,`§6BALANCE`);
system.runInterval(() => {
if (koth_scoreboard) {
world.scoreboard.removeObjective(`Koth`)
world.scoreboard.clearObjectiveAtDisplaySlot("Sidebar")
}
world.scoreboard.setObjectiveAtDisplaySlot("Sidebar", {objective: koth_scoreboard})
world.sendMessage(`§6The Koth event has started!`);
}, 3000);
system.runInterval(() => {
for (const player of world.getPlayers()) {
if (koth_scoreboard.getScore(player, `Koth`) > 99) {
BALANCE_SCOREBOARD.addScore(player, 5000);
world.sendMessage(`§e${player.name} §6has won the Koth event!`);
world.scoreboard.removeObjective(`Koth`);
world.scoreboard.clearObjectiveAtDisplaySlot("Sidebar")
}
}
}, 20);
import {world,system} from "@minecraft/server";
const koth_scoreboard = world.scoreboard.getObjective(`Koth`) || world.scoreboard.addObjective(`Koth`,`§6KOTH`);
const BALANCE_SCOREBOARD = world.scoreboard.getObjective(`balance`)??world.scoreboard.addObjective(`balance`,`§6BALANCE`);
system.runInterval(() => {
if (koth_scoreboard) {
world.scoreboard.removeObjective(`Koth`)
world.scoreboard.clearObjectiveAtDisplaySlot("Sidebar")
}
world.scoreboard.setObjectiveAtDisplaySlot("Sidebar", {objective: koth_scoreboard})
world.sendMessage(`§6The Koth event has started!`);
}, 3000);
system.runInterval(() => {
for (const player of world.getPlayers()) {
if (koth_scoreboard.getScore(player) > 99) {
BALANCE_SCOREBOARD.addScore(player, 5000);
world.sendMessage(`§e${player.name} §6has won the Koth event!`);
world.scoreboard.removeObjective(`Koth`);
world.scoreboard.clearObjectiveAtDisplaySlot("Sidebar")
}
}
}, 20);
🤔 You're ensuring the objective doesn't exist and then immediately attempting to display it?
i removed it by mistake when trying to clean up the code
-# sorry
ok
import {world,system} from "@minecraft/server";
const koth_scoreboard = world.scoreboard.getObjective(`Koth`) || world.scoreboard.addObjective(`Koth`,`§6KOTH`);
const BALANCE_SCOREBOARD = world.scoreboard.getObjective(`balance`)??world.scoreboard.addObjective(`balance`,`§6BALANCE`);
system.runInterval(() => {
if (koth_scoreboard) {
world.scoreboard.removeObjective(`Koth`)
world.scoreboard.clearObjectiveAtDisplaySlot("Sidebar")
}
world.scoreboard.addObjective(`Koth`,`§6KOTH`)
world.scoreboard.setObjectiveAtDisplaySlot("Sidebar", {objective: koth_scoreboard})
world.sendMessage(`§6The Koth event has started!`);
}, 3000);
system.runInterval(() => {
for (const player of world.getPlayers()) {
if (koth_scoreboard.getScore(player) > 99) {
BALANCE_SCOREBOARD.addScore(player, 5000);
world.sendMessage(`§e${player.name} §6has won the Koth event!`);
world.scoreboard.removeObjective(`Koth`);
world.scoreboard.addObjective(`Koth`,`§6KOTH`)
world.scoreboard.clearObjectiveAtDisplaySlot("Sidebar")
}
}
}, 20);
So the goal is to clear all participants from the scoreboard? That's what I assume you intend by deleting the objective and reintroducing it.
Yes
Isn't there a native method for just that?
reset i think
For clearing all the participants in a scoreboard objective.
removeParticipant?
Hm, I suppose that would do.
const KOTHObj = world.scoreboard.getObjective('Koth');
for(const participant of KOTHObj.getParticipants()) {
KOTHObj.removeParticipant(participant);
}
No need to delete the objective and reintroduce it, or to clear the sidebar and set it again
why are they not shown on the scoreboard?
with a default score of 0
@median mountain
what dose the getScore function do?
"scoreboard players add @s ${objective} 0"?
Every second, you want the player's score to initialize to 0 or stay the same value. Why not do the following?
function getScoreOrInit(target, objective, initial = 0) {
const OBJ = world.scoreboard.getObjective(objective);
const score = OBJ.getScore(target) ?? initial;
OBJ.setScore(target, score);
return score;
}
import {world,system} from "@minecraft/server";
const koth_scoreboard = world.scoreboard.getObjective(`Koth`) || world.scoreboard.addObjective(`Koth`,`§6KOTH`);
const BALANCE_SCOREBOARD = world.scoreboard.getObjective(`balance`)??world.scoreboard.addObjective(`balance`,`§6BALANCE`);
system.runInterval(() => {
if (koth_scoreboard) {
world.scoreboard.removeObjective(`Koth`)
world.scoreboard.clearObjectiveAtDisplaySlot("Sidebar")
}
world.scoreboard.addObjective(`Koth`,`§6KOTH`)
world.scoreboard.setObjectiveAtDisplaySlot("Sidebar", {objective: koth_scoreboard})
world.scoreboard.player.addScore.koth_scoreboard(player, 0)
world.sendMessage(`§6The Koth event has started!`);
}, 300);
system.runInterval(() => {
for (const player of world.getPlayers()) {
if (koth_scoreboard.getScore(player) > 99) {
BALANCE_SCOREBOARD.addScore(player, 5000);
world.sendMessage(`§e${player.name} §6has won the Koth event!`);
world.scoreboard.removeObjective(`Koth`);
world.scoreboard.addObjective(`Koth`,`§6KOTH`)
world.scoreboard.clearObjectiveAtDisplaySlot("Sidebar")
}
}
}, 20);```
why is it wrong?
Let's revisit your original sample. The intention for the function, as I understand it, is to do the following:
- Every 20 minutes, reset the KOTH scoreboard and announce the KOTH event.
- Every second, if one player has a KOTH score of 100 or more, announce them as the winner, award that player 5000 balance, then clear the KOTH score from the sidebar.
I'm not sure why the scripting engine is throwing an error for something that is supposed to be synchronous. Let's assume that removeObjective and addObjective are fallible in this regard.
this is the best i can think of not knowing your compete plan
import { world, system } from "@minecraft/server";
const scoreboard = world.scoreboard;
const KOTHObj = scoreboard.getObjective("Koth") ?? scoreboard.addObjective("Koth", "§6KOTH");
const balanceObj = scoreboard.getObjective("balance") ?? scoreboard.addObjective("balance", "§6Balance");
system.runInterval(() => {
for (const participant of KOTHObj.getParticipants()) {
KOTHObj.removeParticipant(participant);
KOTHObj.setScore(participant,0)
}
scoreboard.setObjectiveAtDisplaySlot("Sidebar", { objective: KOTHObj });
world.sendMessage(`§6The Koth event has started!`);
}, 3000);
system.runInterval(() => {
for (const player of world.getPlayers()) {
const KothScore = KOTHObj.getScore(player);
if(KothScore === undefined) KOTHObj.setScore(player,0)
if(KOTHObj.getScore(player) > 99){
balanceObj.addScore(player, 5000);
world.sendMessage(`§e${player.name} §6has won the Koth event!`);
scoreboard.clearObjectiveAtDisplaySlot("Sidebar");
}
}
}, 20);
perfect
thankyou @median mountain
your wlc
thanks to sprunkles too
umm i have a problem
@median mountain
i only want it to say it once
idk why it says it like so many times
In the 20-tick interval, you probably want to reset all the KOTH participants' scores again once one player wins.
how would i do that?
See the bit in the 3000-tick interval's function that starts with for(const participant of KOTHObj.getParticipants()) {? That's precisely what this loop is doing.
Could just copy and paste that loop
import { world, system } from "@minecraft/server";
let isKothEvent = false;
const scoreboard = world.scoreboard;
const KOTHObj = scoreboard.getObjective("Koth") ?? scoreboard.addObjective("Koth", "§6KOTH");
const balanceObj = scoreboard.getObjective("balance") ?? scoreboard.addObjective("balance", "§6Balance");
system.runInterval(() => {
if(isKothEvent) return
scoreboard.setObjectiveAtDisplaySlot("Sidebar", { objective: KOTHObj });
world.sendMessage(`§6The Koth event has started!`);
isKothEvent = true
}, 3000);
system.runInterval(() => {
for (const player of world.getPlayers()) {
const KothScore = KOTHObj.getScore(player);
if(KothScore === undefined) KOTHObj.setScore(player,0)
if(KOTHObj.getScore(player) > 99){
balanceObj.addScore(player, 5000);
world.sendMessage(`§e${player.name} §6has won the Koth event!`);
scoreboard.clearObjectiveAtDisplaySlot("Sidebar");
isKothEvent = false
for (const participant of KOTHObj.getParticipants()) {
KOTHObj.removeParticipant(participant);
KOTHObj.setScore(participant,0)
}
}
}
}, 20);
the reset is only needed when player win
also added isKothEvent so the next event don't trigger without someone wining
i edited the code
cool
with?
This is a normal function of Bedrock; real players tied to a scoreboard will display as "Player Offline" once they leave.
so it doesnt say players offline
i want it to only show the guys in the game
i have no idea if that possible, sorry
i can think of a work around, save his score when he leave and remove hem from the obj, and add hem back when he join
then you have to save the score value when they leave somehow
Have two objectives: One that holds scores internally, then one that displays scores on the sidebar. Periodically delete all display scores, then copy all online players to it.
In commands this would look like the following:
# restore_display.mcfunction
scoreboard players reset * display
execute as @a run scoreboard players operation @s display = @s internal