Tutorial by: Blueyescat

What are Functions?

Functions are an useful way to make reusable sections of code. If you have a bunch of code that is often repeated, instead of copying and pasting the code in many places you can just put it in a function then call the function when you need to run the code.
In many cases this allows for cleaner code, reduces the complexity of the system, and improves performance.

Creating a Function

When creating a function, the code is written like a normal event in Skript.
The following code pattern can be used to create a function. Bold parts can be changed.

function functionName(parameterName: type = defaultValue) :: returnType:

Input parameters are taken as local variables in the function code. For example a parameter named player would be {_player}.

To return a value return %objects% effect is used. Also this effect would stop the code like using the stop effect.

Calling a Function

Calling a function is like using an effect. But if the function has ability to return something, can also be used like an expression.


Example Functions

Example 1 - Clear Chat

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

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


command /clearMyChat:

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 2nd parameter to the player in 1st parameter for the number in 3th parameter times.
If the 3th parameter is not entered the {_times} variable will be 1 by automatically.


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}


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.


on load: # Of course this event is not required

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


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, returns true, if doesn't then returns false.


if listContains(("a", "b" and "c"), "d") is true: # Checks if "a", "b" and "c" contains "d" (no)
    send "True"
    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 list of them with their syntaxes, descriptions and examples here.

Did you find Blueyescat's tutorial helpful?

You must be logged in to comment