# On Background Switch

```lua
function yourEvent(user, string_message)

    if user == nil then 
        -- if user is nil, that means the bg was changed by a non-user
        -- (or manually by streamer via level select dropdown)
        log('the level has switched to: ' .. string_message);
    else
        log(user.displayName .. ' has switched the level to: ' .. string_message); -- changed via command
    end
    
    if string_message ~= 'forest' then --prevent infinite loop of event procing
    
        runCommand('!background forest nightbot'); 
        -- runs the background command to switch level as the broadcaster, but also passes along who issued the command as well.
        -- this is useful for making custom commands to allow viewers to switch levels and passing along the information that they issued it.
    end
end

return function()
    addEvent('backgroundSwitch', 'yourEvent'); -- attaches the event to yourEvent()
    keepAlive();
end
```

{% hint style="info" %}
Events are asynchronous coroutines.
{% 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://docs.streamavatars.com/lua-scripting-api/api-reference-and-tips/events/on-background-switch.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.
