---
title: ลบผู้ใช้เฉพาะจากห้อง
slug: world-sdk-guide-th/KRx-LQkixXswv4WDEky-d
docTags: 
createdAt: 2024-09-02T08:34:13.135Z
---

คุณสามารถเตะผู้ใช้บางคนออกจากห้องในระหว่างการทำงานได้

:::hint{type="danger"}
- กรุณาสร้างโค้ดเซิร์ฟเวอร์ index.ts Sandbox
:::

## การกำหนดฟังก์ชัน

| kick(client: SandboxPlayer, reason?: string): Promise; | นี่คือฟังก์ชันที่ใช้เตะ SandboxPlayer หลังจากได้รับ SandboxPlayer ที่มี SessionID<br /><br />- หากคุณถูกเตะออก จะมีป๊อปอัพปรากฏขึ้นเพื่อแจ้งให้คุณทราบว่าคุณถูกเตะออก แต่ป๊อปอัพนั้นเป็น UI ของแอป ZEPETO และไม่สามารถตรวจสอบได้โดยผู้แก้ไข |
| ------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

:::hint{type="info"}
**📘&#x20;**&#xE01;รุณาอ้างอิงจากคู่มือต่อไปนี้ \[[ZEPETO.Multiplay(Server) API](https://developer.zepeto.me/docs/multiplay-server/classes/ZEPETO_Multiplay.Sandbox#kick)]
:::



ก่อนอื่น เพื่อให้ได้ข้อมูลผู้ใช้ ให้กำหนด Schema Types และ RoomState ดังนี้:

![](https://api.archbee.com/api/optimize/fCt3n1oCa8rgNJ8fw9I2N-oVK3n8knNlRPitGOhHD1y-20240904-102428.png)

## ตัวอย่างการใช้งาน

- ใช้ข้อมูลใน UserInfos เพื่อรับ session ID ของผู้ใช้ที่จะถูกเตะออกจาก userId.
- หลังจากนำเข้า sandbox player ด้วย session ID ให้เรียกใช้ฟังก์ชัน kick.
- ฉันสามารถบอกคุณได้ว่าใครถูกเตะออกผ่านการออกอากาศ.

```typescript
import { Sandbox, SandboxOptions, SandboxPlayer } from 'ZEPETO.Multiplay';
import { DataStorage, loadDataStorage } from 'ZEPETO.Multiplay.DataStorage';
import { UserInfo } from 'ZEPETO.Multiplay.Schema';

export default class extends Sandbox {

    onCreate(options: SandboxOptions) {
        this.onMessage("Kick", (client: SandboxPlayer, message: string) => {
            this.tryKick(client, message);
        });
    }

    onJoin(client: SandboxPlayer) {
        const user = new UserInfo();
        user.sessionId = client.sessionId;
        user.userId = client.userId;

        this.state.UserInfos.set(client.userId, user);
    }

    async tryKick(client: SandboxPlayer, userId: string) {
        let player: SandboxPlayer;
        if (userId == null) {
            player = client;
        } else {
            const kickPlayerSessionId: string = this.state.UserInfos.get(userId).sessionId;
            player = this.loadPlayer(kickPlayerSessionId);
        }

        console.log(`try kick : ${player.userId}`);
        await this.kick(player);

        this.broadcast("Log", `kick : ${player.userId}`);
    }
}
```

:::hint{type="success"}
**👍 โดยค่าเริ่มต้น ผู้ใช้ที่ถูกเตะออกสามารถเข้าห้องได้อีกครั้ง**

- เพื่อป้องกันไม่ให้เกิดขึ้นนี้ ให้ใช้ฟังก์ชันการจัดการ DataStorage ตาม UserId เพื่อเก็บข้อมูลห้องที่ถูกเตะออกโดยผู้ใช้.
- คุณสามารถดำเนินการโดยการเรียกใช้ kick เมื่อคุณพยายามเข้าห้อง.
:::

