Bot Player Creation Guide
Bot players are used to fill in when there are not enough people to start a multiplayer world, or when a player leaves during a world.
How the bot player behaves needs to be implemented for each content.
This guide describes the general method of creating a Bot player.
📘 The Bot Player creation guide is based on the multiplayer guide. [Multiplay Tutorial]
1-1. Add a boolean value called IsBot to the multiplayer schema.

1-2. Define the function below to create a Bot player in the server script index.ts and call it at the desired point.
👍 Tips
- The userId of a specific user is stored in advance for the Bot player character to be created.
- You can check the UserId of a specific user by checking the userId of the client connecting to OnJoin on the server. After writing the script below in the server script, connect from the relevant world.
2-1. If you have the server create a Bot player at some point, the client will recognize it as a new player in OnJoinPlayer().
- Create Project > Create > ZEPETO > TypeScript and rename it to BotPlayerManager.
- Add logic in OnAddedPlayer() to create each player, and add logic to distinguish bot players and create their ZEPETO characters.
2-2. Write the SetBotPlayer function to add tags and synchronization components to bot players and create scripts to control them.
- Set _botMapData to save bot player data in Map format to manage bot players.
👍 Tips You can add additional scripts or settings to SetBotPlayer() to control the behavior of bot players.
For worlds that require a certain number of players to start, sometimes there are not enough players and you have to wait a long time for the world to start.
In this case, you can start the world by adding a Bot player.
3-1. Register a function to execute CreateBot() when the server receives a message from the client in index.ts.
3-2. In the client script BotPlayerManager.ts, write a function to send the "CreateBot" message to the server.
- The way to execute a function is to send a message by pressing a button.
- Send the user ID of the Bot player to be created as a string through the message.
3-3. Now, when you run the server and the runtime, you can see that bot players are created when you press the button.

When there are not enough players to start the world, you can add bot players to initiate the world.
4-1. In the server script, add the following code during OnJoin to check the number of players and start the world when there are at least four players.
- Add a function to check the number of players in CreateBot().
- Add a counter for the number of plays in the StartWorld() function.
- On the server, OnJoin is executed when a real player joins the room. So, when a Bot player is created via CreateBot and when a player enters via OnJoin, checkPlayerNumber() adds the number of people.
4-2. In the client script, BotPlayerManager.ts, write StartWorld(), which is executed when the message StartWorld is received from the server.
4-3. At runtime, when there are more than 4 players, including Bot players, you can see a log called World Start pops up on the server console and on the client's console.

Below is sample code that moves the added Bot players to the local player position and synchronizes the movement position.
5-1. First, write the code to move the Bot players when the message MoveBot is received from the client in index.ts of the server.
5-2. In the client script, BotPlayerManager.ts, write SendBotPosition() that sends the local player position to the server when buttonCallBot is pressed.
- Then write code to move all Bot players to the location information included in the message when the message MoveBotToPosition is received from the server.
5-3. Now if you create a Bot player at runtime and press the buttonCallBot button you should see the created Bot player move to the local player character position.


