parkour time (stopwatch)

  • Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the community!

    Now, what are you waiting for? Join the community now!

  • LOOKING FOR A VERSION OF SKRIPT?

    You can always check out skUnity Downloads for downloads and any other information about Skript!

chiifu

Member
Mar 17, 2024
3
0
1
Hi there! I'm new to skripting and I'm not sure how to get the variables and such to work. What I want to do is make a parkour stopwatch (counting up) start for the player when they step on a gold block, and for it to also display on the scoreboard. I already have a scoreboard set up but I am not sure how to get the variables/placeholders to work. I also want the stopwatch to stop when they step on a diamond block.
 
Hello! Welcome to skript.

You can do this by setting a variable to (defined as {variable}) to the time the player steps on the gold block (now) and then using a while loop until they hit a diamond block. I prefer not to spoonfeed code but I think that if I provide the code and then break it down, it'll be easier to understand and learn from.

Python:
on step on gold block:
    set {_stopwatch} to now
    {timed::%player's uuid%} is not set:
        set {timed::%player's uuid%} to 0
    else:
        message "&cYou already started the parkour!" to player

    while {timed::%player's uuid%} is set:
        set {timed::%player's uuid%} to difference between now and {_stopwatch}
        set line <number> of player's scoreboard to "%{timed::%player's uuid%}%"
        wait 1 second
 
on step on diamond block:
    message "&aFinal time: &c%{timed::%player's uuid%}%" to player
    clear {timed::%player's uuid%}

Line 1: Simple detection of when they step on a gold block
Line 2: setting a local variable (has _ before the variable name and gets cleared when the event finishes, used to not take up space in your files)
Line 3: condition to check if the variable is set or not (if they have started the parkour or not)
Line 4: setting the variable that tells us if they are being timed and allows us to send the final time/scoreboard time. We use the player's uuid so that every player has a distinct variable, especially if more than one person is using the parkour at once.
Line 6: Message sent if they are already in the parkour
Line 7 (ignore the blank space, used to make it more readable): the while loop. This loop is designed to execute until we clear/delete the timed variable
Line 8: subtracting the old "now" (stored in the {_stopwatch} variable) from the new "now". This returns the time since we set the {_stopwatch} variable
Line 9: Setting the line of the player's scoreboard to the stopwatch value. Please change <number> to whatever line you want it to be. Line numbers are displayed in red to the right of the text. This line requires the SkBee addon for skript, which you can download here, as base skript does not have direct support for changing the player's screen-side display.
Line 10: Detecting when they step on a diamond block
Line 11: Sending the player's final time in chat (only for them). If you don't want this to send in chat, change "message" to "send action bar"
Line 12: Deleting the timed variable, to stop the while loop and reset their time for the next run at the parkour.

The code is formatted in python for the colors that make it even more readable. It has been tested several times by myself.

By the way, the percentage signs (%) used in the variables and messages mean getting a value.
If you set {hello} to 6 and then did message "{hello}" to player, it would just send "{hello}" but using percentage signs, like this message "%{hello}%" to player will instead send "6". Similarly, {timed::%player's uuid%} doesnt register as {timed::player's uuid}, it registers as {timed::ec4ee844-beb4-443b-bd17-cfaeb01bfa54} (that long string of characters changes depending on the player)

I apologize if this was confusing in any way or if I explained information you already knew.
 
Last edited:
Ah, thank you so much! I understand most of it besides Line 8 where it says:
set {timed::%player's uuid%} to difference between now and {_stopwatch}

Could you explain what the "between now" means and what setting a variable "to now" would do?
 
"now" refers to the current time, for example. if it were 08:46:05 (8 hours, 46 minutes, 5 seconds) when you step on the gold block, and then you subtract that from the time a second later (08:46:06), you get a result of 1 second. Thats what this does. It sets a variable to the time you step on the gold block and then subtracts that time from the current time and returns the value. The "difference between now and {_stopwatch}" is the result of subtracting {_stopwatch} from the current time.
 
Also I forgot to include info about the real line 10: the wait 1 second.
This isn't very complicated...it waits 1 second. (If you would like a timer that also counts the tenths of a second you can use wait 2 ticks. If you want one that counts tenths and hundredths, wait 1 tick.) The important thing about this line is if you don't include a delay in a while loop...your server will crash. This is because it will execute the loop as many times as it can during every tick. It could execute millions of times in a second, rather than once (with 1 second delay), 10 times (2 tick delay), or 20 times (1 tick delay)
 
Awesome! If you ever need anything more, most of the members here are a lot more active on the discord. Of course, ask anywhere, one of us forum mains will answer you here but help in the discord will probably come a bit quicker.