TransWikia.com

How to create a countdown timer that triggers an event on ending?

Arqade Asked on July 25, 2021

I found one article to set up a simple timer and that works fine. But Im trying to get something to execute when it gets to 0. Here is what I have so far:

I run this one time to create the timer variable:

scoreboard objectives add timerObjective dummy "Timer Objective"
scoreboard players set nonexistenuser timerObjective 100
scoreboard objectives setdisplay sidebar timerObjective

I then create a command block that runs every 20 ticks (1 second) to decrement the timer:

scoreboard players remove @a timerObjective 1
execute @a[name=nonexistenuser,scores={timerObjective=0}] ~ ~ ~ fill 1049 1 1050 1001 49 1049 air 

This does count down from 100 and I can see it in the display it shows the score for user nonexistentuser. But when it hits 0 it does not execute the command because there is no actual player called nonexistentuser. If I replace name with my own ID it works because I am an actual player. I tested the command manually and found the selector @a[name=nonexistenuser,scores={timerObjective=0}] returns no targets even though I can see the score for this "player" in the display. So is there some other way I can create a simple countdown timer that does something at the end?

2 Answers

You can instead use /scoreboard players test:

/scoreboard players test nonexistentuser timerObjective 0 0

The command succeeds if the number is 0, and fails if it is not. Just place a conditional chain command block after that with your /fill command and it will only execute if the command succeeds.

Correct answer by ExpertCoder14 on July 25, 2021

I found one option that I can live with for multiple timers and I can extend it to as many times as I want. It uses several function files but each timer can be tracked and works independently from the other timers. Solution requires just one repeating command block. You can start the timer via a second command block (/function timer1Start for example), call the timer1start function directly in console, or call timer1Start from some other function.

I am creating a build battle feature so there will just be some button the user clicks that will execute another function buildBattle. The buildBattle function in turn will directly call /function timer1Start. With 3 timers I can have 3 different build battles at the same time each on their own timer and each can execute a separate function when complete.

I guess the same could be set up without functions using a bunch of command blocks - but who wants to do that ;)

I created these files:

global:

  • timerInit.mcfunction: execute this one time to set up all timers
  • TimerTick.mcfunction: put this in a repeating command block that runs every 20 ticks

timer1 files

  • timer1start.mcfunction:
    • sets active timer to 1 and starts a 15 second timer
    • call /function timer1start to start the timer
  • timer1tick.mcfunction: anything in here will execute once per second
  • timer1done.mcfunction: anything in here will execute once after timer1 expires

timer2 files

  • timer2start.mcfunction:
    • sets active timer to 2 and starts a 30 second timer
    • call /function timer2start to start the timer
  • timer2tick.mcfunction: anything in here will execute once per second
  • timer2done.mcfunction: anything in here will execute once after timer2 expires

timer3 files

  • timer3start.mcfunction: sets active timer to 3 and starts a 45 second timer
    • sets active timer to 3 and starts a 45 second timer
    • call /function timer3start to start this timer
  • timer3tick.mcfunction: anything in here will execute once per second
  • timer3done.mcfunction: anything in here will execute once after timer3 expires

timerInit.mcfunction:

scoreboard objectives add timer1 dummy "Timer 1"
scoreboard objectives add timer2 dummy "Timer 2"
scoreboard objectives add timer3 dummy "Timer 3"
scoreboard objectives add activeTimer dummy "activeTimer"

TimerTick.mcfunction:

#update the 3 individual timers
scoreboard players remove timeTracker1 timer1 1
scoreboard players remove timeTracker2 timer2 1
scoreboard players remove timeTracker3 timer3 1

#synch each players timer with the time tracker 'player' 
scoreboard players operation @a timer1 = timeTracker1 timer1
scoreboard players operation @a timer2 = timeTracker2 timer2
scoreboard players operation @a timer3 = timeTracker3 timer3

#update all players to show them their active timer in the side bar (optional)
execute @a[scores={activeTimer=1}] ~ ~ ~ scoreboard objectives setdisplay sidebar timer1
execute @a[scores={activeTimer=2}] ~ ~ ~ scoreboard objectives setdisplay sidebar timer2
execute @a[scores={activeTimer=3}] ~ ~ ~ scoreboard objectives setdisplay sidebar timer3

#execute something every second for the timers (for instance show time or some other command)
function timer1Tick
function timer2Tick
function timer3Tick

#execute these functions ONE TIME when the timer expires
execute @p[scores={timer1=-1}] ~ ~ ~ function timer1Done
execute @p[scores={timer2=-1}] ~ ~ ~ function timer2Done
execute @p[scores={timer3=-1}] ~ ~ ~ function timer3Done

timer1start.mcfunction

scoreboard players set @a timer1 15
scoreboard players set timeTracker1 timer1 15
scoreboard players set @a activeTimer 1

timer1tick.mcfunction:

#execute @a[scores={activeTimer=1}] ~ ~ ~ say hello
title @a[scores={timer1=10,activeTimer=1}] actionbar 10 seconds left
title @a[scores={timer1=9,activeTimer=1}] actionbar 9
title @a[scores={timer1=8,activeTimer=1}] actionbar 8
title @a[scores={timer1=7,activeTimer=1}] actionbar 7
title @a[scores={timer1=6,activeTimer=1}] actionbar 6
title @a[scores={timer1=5,activeTimer=1}] actionbar 5
title @a[scores={timer1=4,activeTimer=1}] actionbar 4
title @a[scores={timer1=3,activeTimer=1}] actionbar 3
title @a[scores={timer1=2,activeTimer=1}] actionbar 2
title @a[scores={timer1=1,activeTimer=1}] actionbar 1
title @a[scores={timer1=0,activeTimer=1}] actionbar  Times up!

timer1Done.mcfunction

#do something when the timer completes
title @a[scores={timer1=..0}] actionbar timer 1 countdown complete

timer2start.mcfunction

scoreboard players set @a timer2 30
scoreboard players set timeTracker2 timer2 30
scoreboard players set @a activeTimer 2

timer2tick.mcfunction:

#do something when timer 2 executes

timer2Done.mcfunction:

#do something when timer 2 expires

timer3start.mcfunction

scoreboard players set @a timer3 45
scoreboard players set timeTracker3 timer3 45
scoreboard players set @a activeTimer 3

timer3tick.mcfunction:

#do something when timer 3 executes 

timer3Done.mcfunction:

#do something when timer 3 expires

Answered by George on July 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP