# Debugging

{% hint style="info" %}
**"What is debugging?"**

Debugging is a wonderful skill to help you find any (logic) issues in your code. Debugging is a process where you systematically go through code and test it, to figure out where your code breaks and why it stops working.
{% endhint %}

Whenever you have an issue, debugging should be one of the first things you try, as more often than not, debugging will give you sufficient information to pinpoint and solve mistakes in your code. If you cant find an issue just by looking at the code, it's time to debug.

{% hint style="success" %}
Debugging isn't just information to pass on to others when you ask them to help with your code.

Instead of creating a help post or ticket, try debugging first. (Most) people aren't wizards, they cant magically find an error. Debugging, on the other hand, is a fool-proof way to at the very least pinpoint a logic error.

After debugging, you know the exact point your issue occurs. *What are you going to do about that?* With new information, do your best to come up with a solution.
{% endhint %}

***

## How to debug a script

To debug a script, simply send broadcasts at key points and of key values. Then, based on what shows up you should be able to figure out what your code is doing, and more importantly, *why*. Then you can make adjustments accordingly.

There are two main segments of debugging:

* **Checking what sections of code are being run:**\
  This will tell you if the script is properly running an event, calling a function, or passing a condition. Simply send a message or broadcast within that section, and see whether or not it shows up. If it does, the section is being triggered, if it doesn't show up, the section is not being run.
* **Asking Skript for confirmation on the value of an object:**\
  This will let you know if Skript is acting on and with information you think it should, or working with something completely different. To check the value of an object, send a message or broadcast containing the object. Then, compare what that outputs with what the value is supposed to be.

{% hint style="info" %}
Neat and clear code is much easier to look through and debug. See the Organisation page to learn how to write pristine code.\
(The Organisation page is currently a work in progress)
{% endhint %}

## Example

Here is an example of a logic issue discovered and solved through the debugging process. Feel free to copy-and-paste the code and try working on it on your own, or along with the example.

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

```python
command giveMeACustomSword:
  trigger:
    set {_coolSword} to (1 of iron sword)
    set ({_coolSword}'s name) to "&aMy Super Cool Sword"
    give player {_coolSword}

on right-click with iron sword:
  if (name of player's tool) = "&bMy Super Cool Sword":
    strike lightning effect at player's location
    broadcast "Zap!"
```

{% endcode %}

At first glance, this code looks fine, and it almost is. It loads without a single error, and players can obtain a sword through the command. However, when trying to summon a lightning bolt, the code fails, and doesn't strike lightning or send the broadcast.

{% hint style="info" %}
Since there are no error messages, that means this is a logic error. Not sure what a logic error is? See the [Errors page](/syntask/issues/errors.md).
{% endhint %}

The debugging process is started by checking what sections the script runs:

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

```python
command giveMeACustomSword:
  trigger:
    set {_coolSword} to (1 of iron sword)
    set ({_coolSword}'s name) to "&aMy Super Cool Sword"
    give player {_coolSword}
    
on right-click with iron sword:
  broadcast "Debugging: Right-click detected"
  if (name of player's tool) = "&bMy Super Cool Sword":
    broadcast "Debugging: Custom sword detected"
    strike lightning effect at player's location
    broadcast "Zap!"
```

{% endcode %}

{% hint style="info" %}
Since the player receives the item, we know the command works just fine and there's no need to debug it.

The issue only happens when trying to summon a lightning bolt, so only the `on right-click:` event is broken and needs to be debugged.
{% endhint %}

Now, when you try the debugged code, you should see the first message (line 8) broadcasted, but not the second (line 10). This means Skript detects the event, but doesn't realise the player is holding the custom item. Now we know the exact line where the code fails: the if statement.

If you can figure out the cause of the issue from here, great! If not, shed more light by broadcasting the values you're working with:

{% hint style="info" %}
Debug a broken line's components, or the values within that line. In this case, `name of player's tool` and `"&bMy Super Cool Sword"`.
{% endhint %}

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

```python
command giveMeACustomSword:
  trigger:
    set {_coolSword} to (1 of iron sword)
    set ({_coolSword}'s name) to "&aMy Super Cool Sword"
    give player {_coolSword}

on right-click with iron sword:
  broadcast "Debugging: Right-click detected"
  broadcast "Debugging: Name of player's tool = %name of player's tool%"
  broadcast "Debugging: Name of custom item = &bMy Super Cool Sword"
  if (name of player's tool) = "&bMy Super Cool Sword":
    broadcast "Debugging: Custom sword detected"
    strike lightning effect at player's location
    broadcast "Zap!"
```

{% endcode %}

{% hint style="warning" %}
Make sure to send the values *before* the if statement. Broadcasting within the if statement's section would be illogical; you've already discovered that section of code is not run and so we won't see the debug messages.
{% endhint %}

Now, it should be really obvious what the issue is. When the code gives the sword, the name is coloured `&a`, however, when the script checks for the name, it is checking against a name coloured `&b`. Now you know what the issue is, and can go fix it!

{% hint style="success" %}
Don't forget to remove your debugging messages after you fix the issue.
{% endhint %}

***

## Effect commands

Effect commands allow players to run an effect directly in-game through the chat bar. This means they are pretty useful when debugging code, like broadcasting the value of a variable or expression.

{% hint style="info" %}
To see more about effect commands and how to enable them, see the [Effect Commands page](/syntask/utility/effect-commands.md).
{% endhint %}


---

# 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/issues/debugging.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.
