---
title: 在多人游戏中暂时限制房间访问
slug: world-sdk-guide-ch/Z9y0AlbjJ38h8Vcm6LkwV
docTags: 
createdAt: 2024-09-02T08:34:13.135Z
---

您可以使用锁定和解锁功能在您希望的时间限制或允许房间访问。

:::hint{type="info"}
**📘&#x20;**&#x8BF7;参考以下指南。 \[[ZEPETO.Multiplay(Server) API](https://developer.zepeto.me/docs/multiplay-server/classes/ZEPETO_Multiplay.Sandbox#lock)]
:::

## 锁定

锁定功能是限制房间访问的功能。

```typescript
await this.lock();
```



## 解锁

解锁功能是允许房间访问的功能。
但是，如果当前连接的人数与房间允许的最大人数相同，则房间无法解锁。

```typescript
await this.unlock();
```



## 示例

以下是一个使用锁定和解锁功能的示例代码。

```typescript
import { Sandbox, SandboxOptions, SandboxPlayer } from 'ZEPETO.Multiplay';

export default class extends Sandbox {

    onCreate(options: SandboxOptions) {

    }

    async onJoin(client: SandboxPlayer) {
        try {
            if (this.clients.length > 8) {
                await this.lock();
            }
        } catch(e) {
            console.error(e);
        }
    }

    async onLeave(client: SandboxPlayer, consented?: boolean){
        try {
            if (this.clients.length <= 8) {
                await this.unlock();
            }
        } catch(e) {
            console.error(e);
        }
    }
}
```

:::hint{type="success"}
## 👍 锁定和私有功能之间的区别

- 锁定的多人房间在 ZEPETO 房间列表中不可见，且无法通过邀请进入。
- 私有多人房间在 ZEPETO 房间列表中不显示，但可以接受邀请。
:::

