Skip to content

Example Functions

LucFr edited this page Jan 23, 2023 · 1 revision

Example 1 - Clear Chat

function clearChat(p: player):
    loop 100 times:
        send "" to {_p}

This function assigns the player in 1st parameter to the variable {_p}. The 2nd line creates a loop that will run the code 100 times. The latest line sends an empty message to the player in the {_p} variable.

Usage

command /clearMyChat:
    trigger:
        clearChat(player)

Runs the clearChat function using the player for the first parameter.


Example 2 - Sending Messages

function sendMessage(players: players, message: text, times: number = 1):
    loop {_times} times:
        send {_message} to {_players::*}

This function sends the message in the 2nd parameter to the player in the 1st parameter for the number in the 3rd parameter times. If the 3rd parameter is not entered the {_times} variable will be 1automatically.

Usages

sendMessage(player, "message", 10) #10 Times
 
sendMessage(all players, "&cmessage") #1 Time

Example 3 - Total of Numbers

function total(numbers: numbers) :: number:
    broadcast "total function called"
    loop {_numbers::*}:
        add loop-value to {_total}
    return {_total}

Usages

set {_numbersList::*} to 6, 3 and 9
set {_result} to total({_numbersList::*})
 
if total(1, 6 and 9) is 16:
    send "True"
 
send "%total({_numbers::*})%"

Example 4 - Function Without Parameters

function setVariables():
    set {variables::1} to "value"
    set {variables::list::2} to true
    set {variables::3} to 3

Creates a function without any parameter.

Usage

on load: # Of course this event is not required
    setVariables()

Example 5 - Reverse List

function reversed(list: objects) :: objects:
    loop size of {_list::*} times:
        set {_index} to size of {_list::*} - loop-number - 1
        add {_list::%{_index}%} to {_reversed::*}
    return {_reversed::*}

This function reverses the list in 1st parameter and returns it back. For example 1, 2 and 3 --> 3, 2 and 1

Usage

set {_list::*} to reversed({_list::*})

Example 6 - List Contains Check

function listContains(list: objects, search: object) :: boolean:
    loop {_list::*}:
        if loop-value is {_search}:
            return true
    return false # If return true is triggered it will stop the code so this line will not be triggered.

This function checks if the list in the first parameter contains the value in the second parameter or not. If contains, return true, if doesn't then return false.

Usages

if listContains(("a", "b" and "c"), "d") is true: # Checks if "a", "b" and "c" contains "d" (no)
    send "True"
else:
    send "False"
 
set {_list::*} to "a", "b" and "c"
send "Check: %listContains({_list::*}, ""c"")%"

Default Functions

Skript registers many functions by default. You can find a list of them with their syntaxes, descriptions, and examples here.

📜 Wiki Navigation


  • ❓ What is Minecraft?
  • ❓ What is coding?
  • ❓ What is program language?
  • ❓ Why is coding Minecraft?
  • ❓ What needs to be prepared?
  • ❓ What needs to do while coding?

📖 Tutorials

Minecraft Server setup

Skript coding

Project setup

📃 Other Info

Clone this wiki locally