# Creating a Continuous Behaviour Script

A `ContinuousBehaviorScript` is a module that defines recurring behavior logic for an AI agent. This logic is executed based on a specified frequency set when initializing the agent.

When the behavior runs, the aiAgent instance is passed as a parameter to allow access to its methods and data.

***

#### Requirements

The behavior module **must implement** the following structure:

`local replicatedStorage = game:GetService("ReplicatedStorage")`\
&#x20;`local aiCommandMap = require(replicatedStorage.AIAgentModule.AICommandMap)` &#x20;

&#x20;`local module = {}`

&#x20;`-- Called before the behavior is run`\
&#x20;`function module.InitializeBehavior()`\
&#x20;`-- Perform any setup required before the behavior starts`\
&#x20;`end`

`-- Called before each behavior execution cycle`\
&#x20;`-- Return true to run the behavior, false to skip`\
&#x20;`function module.BehaviorConditionsCheck(aiAgent: AIAgent): boolean`\
&#x20;   `-- Add logic to determine if behavior should run`\
&#x20;`return true`\
&#x20;`end`<br>

`-- Called when conditions pass; executes behavior logic`\
`function module.RunContinuousBehavior(aiAgent: AIAgent)`\
&#x20;   `-- Add your custom behavior script here` \
&#x20;   `-- Sends current game state to the AI server`\
&#x20;   `-- Expects a response which may include a message or command`\
&#x20;   `local response, status = aiAgent.SendQueryToAgent({ aiAgent.GetGameState() })`\
&#x20;   `if not response then`\
&#x20;       `return false`\
&#x20;   `end`\
\
&#x20;   `-- Executes command based on AI response`\
&#x20;   `if response.command then`\
&#x20;       `local commandResponse = aiCommandMap[response.command.functionName](response.command.parameters)`\
&#x20;   `end`\
`end`\
`return module`
