Create reusable code blocks with parameters and return values
Functions allow you to organize code into reusable blocks that can be called from anywhere in your scripts. They accept parameters and can return values.
## A simple broadcasting function example.# This demonstrates how to declare and run a simple function.#function say_message(message: text): broadcast {_message} # our message argument is available in `{_message}`.on first join: wait 1 second say_message("Welcome, %player%!") # Runs the `say_message` function.
Function parameters are available as local variables with an underscore prefix: {_parameter_name}
Functions can accept multiple parameters of different types:
function give_apple(name: text, amount: number) :: item: set {_item} to an apple set the name of {_item} to {_name} set the item amount of {_item} to {_amount} return {_item} # Gives this value to the code that called the function.command /appleexample: permission: skript.example.apple trigger: send "Giving you an apple!" set {_item} to give_apple("Banana", 4) give player {_item}
Functions can return a value using :: <type> and the return statement:
function give_apple(name: text, amount: number) :: item: set {_item} to an apple set the name of {_item} to {_name} set the item amount of {_item} to {_amount} return {_item}
The return type comes after :: in the function declaration. Use return <value> to send a value back to the caller.
# Store the returned valueset {_custom_item} to give_apple("Golden Apple", 5)give player {_custom_item}# Use directlygive player give_apple("Silver Apple", 1)# Use in conditionsif get_balance(player) > 100: send "You have enough money!"
Functions can call themselves for complex operations:
## An example of a recursive (self-calling) function that is used to repeat a complex task.# Please note that self-calling functions can loop infinitely, so use with caution.#function destroy_gold(source: block) :: blocks: add {_source} to {_found::*} break {_source} naturally using an iron pickaxe loop blocks in radius 1 of {_source}: loop-block is a gold ore block break loop-block naturally using an iron pickaxe if {_found::*} does not contain loop-block: add destroy_gold(loop-block) to {_found::*} return {_found::*}command /oreexample: permission: skript.example.ore trigger: if the player's target block is a gold ore block: send "Destroying all connected ore." set {_found::*} to destroy_gold(player's target block) send "Destroyed %size of {_found::*}% connected ores!" else: send "<red>You must be looking at an ore block!"
Recursive functions can cause infinite loops if not properly designed. Always include a base case that stops the recursion.
function example(input: text): set {_local} to {_input} # Parameter as local variable set {global} to {_input} # Global variable # {_local} is deleted when function ends # {global} persists
Functions are accessible from any script after being loaded:
shop.sk
command /buy <item>: trigger: set {_price} to 10 if has_balance(player, {_price}): deduct_balance(player, {_price}) give arg-1 to player send "Purchased for %format_money({_price})%" else: send "You need %format_money({_price})%!"
function teleport_safely(target: player, destination: location): if {_target} is not set: return # Exit if no player if {_destination} is not set: return # Exit if no location # Check if destination is safe if block at {_destination} is lava: send "Cannot teleport to lava!" to {_target} return teleport {_target} to {_destination}
function create_custom_item(name: text, type: item, lore_text: text) :: item: set {_item} to {_type} set name of {_item} to {_name} set line 1 of lore of {_item} to {_lore_text} return {_item}command /customitem: trigger: set {_sword} to create_custom_item("Legendary Sword", diamond sword, "A powerful weapon") give {_sword} to player
aliases: menu items = TNT, lava bucket, string, coal, oak planksfunction make_menu(name: text, rows: number) :: inventory: set {_gui} to a new chest inventory with {_rows} rows with name {_name} loop {_rows} * 9 times: # Fill the inventory with random items. set slot loop-number - 1 of {_gui} to random item out of menu items return {_gui}command /fancymenu: permission: skript.example.menu trigger: set {_menu} to make_menu("hello", 4) add {_menu} to {my inventories::*} # Prepare to listen to this inventory. open {_menu} to playeron inventory click: # Listen for players clicking in any inventory. if {my inventories::*} contains event-inventory: # Make sure it's our menu. cancel event send "You clicked slot %index of event-slot%!"on inventory close: # No longer need to listen to this inventory. {my inventories::*} contains event-inventory remove event-inventory from {my inventories::*}
function debug_example(value: text): broadcast "[DEBUG] Function called with: %{_value}%" set {_result} to {_value} broadcast "[DEBUG] Function returning: %{_result}%" return {_result}