# Waiting a Tick

Sometimes waiting a tick is required in a situation to allow something to happen, or give the server time to process.

{% hint style="info" %}
What is a tick? A game tick is when things on a server are updated. Minecraft splits a second into 20 game ticks.

You've probably encountered ticks before if you've ever played with the `randomTickSpeed` gamerule.
{% endhint %}

***

## While loops

A wait (although not necessarily a 1 tick wait) is absolutely required in a while loop. While loops will run as fast as possible, and so without a wait (or limited number of iterations) they will overwork the server, leading to a crash.

If a while loop is running for a few iterations and is guaranteed to stop, the wait is not needed. This is helpful when returning, as waits cannot be used when returning.

{% hint style="info" %}
For more information on while loops, see the (not-yet-created) Loops page.
{% endhint %}

## Lag prevention

Since Skript tries to compress everything into one tick, adding a small delay can break up code or tasks into chunks to be evaluated separately one after another instead of all at once.

A prime example of this is setting large amounts of blocks. If you are setting a whole bunch of blocks at once, that can cause freezing.

Instead of setting all the blocks at once, break it up into segments the server can handle easily with a 1 tick wait in-between. One way to do so is to loop the blocks and use the following snippet to wait 1 tick every `{_number}` iterations (and thus blocks) inside of the loop.

{% code overflow="wrap" lineNumbers="true" %}

```python
if mod(loop-iteration, {_number}) = 0:
  wait 1 tick
```

{% endcode %}

## Delays

Certain events are called *just before* the event actually happens. An indication of this is whether or not the event is cancellable; cancellable events are called early, while non-cancellable events are called after the event happens, which is why they cannot be cancelled.

Some examples of events like this include `on teleport:` and `on pick up:`, where you'll need to wait a tick for the player to be teleported to the new location, or for the item to enter the player's inventory.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://x8ight.gitbook.io/syntask/utility/waiting-a-tick.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
