CREATE YOUR WORLD
Multiplay
Removing a specific user from Room
4min
You can kick certain users out of the room at runtime.
- Please create the server code index.ts Sandbox.
kick(client: SandboxPlayer, reason?: string): Promise; | It is a function to kick out the SandboxPlayer after receiving the SandboxPlayer with SessionID. - If you are kicked out, a pop-up will appear informing you that you are kicked out, but the pop-up is the UI of the ZEPETO app and cannot be checked by the editor. |
---|
📘 Please refer to the following guide. [ZEPETO.Multiplay(Server) API]
First, to get user information, define Schema Types and RoomState as follows:

- Use the data in UserInfos to get the session ID of the user who will be kicked out of the userId.
- After importing the sandbox player with the session ID, call the kick function.
- I can tell you who was kicked out through broadcast.
TypeScript
1import { Sandbox, SandboxOptions, SandboxPlayer } from 'ZEPETO.Multiplay';
2import { DataStorage, loadDataStorage } from 'ZEPETO.Multiplay.DataStorage';
3import { UserInfo } from 'ZEPETO.Multiplay.Schema';
4
5export default class extends Sandbox {
6
7 onCreate(options: SandboxOptions) {
8 this.onMessage("Kick", (client: SandboxPlayer, message: string) => {
9 this.tryKick(client, message);
10 });
11 }
12
13 onJoin(client: SandboxPlayer) {
14 const user = new UserInfo();
15 user.sessionId = client.sessionId;
16 user.userId = client.userId;
17
18 this.state.UserInfos.set(client.userId, user);
19 }
20
21 async tryKick(client: SandboxPlayer, userId: string) {
22 let player: SandboxPlayer;
23 if (userId == null) {
24 player = client;
25 } else {
26 const kickPlayerSessionId: string = this.state.UserInfos.get(userId).sessionId;
27 player = this.loadPlayer(kickPlayerSessionId);
28 }
29
30 console.log(`try kick : ${player.userId}`);
31 await this.kick(player);
32
33 this.broadcast("Log", `kick : ${player.userId}`);
34 }
35}
👍 By default, users who have been kicked out can enter the room again
- To prevent this, use the DataStorage management function based on the UserId to store the room information that was kicked out by the user.
- You can implement it by calling a kick when you try to enter the room.
Updated 10 Oct 2024

Did this page help you?