在体验中找到朋友可能是一个挑战。SpawnWithFriends 开发者模块 会自动将生成的玩家移动到体验中某个朋友附近。此模块也可以配置为在命令下传送玩家,而不是自动传送。
模块使用
安装
要在体验中使用 SpawnWithFriends 模块:
从 Studio 的 窗口 菜单或 首页 选项卡工具栏打开 工具箱,然后选择 创作者商店 选项卡。

确保选择了 模型 排序,然后单击 查看全部 按钮以查看 类别。

找到并单击 包 瓷砖。
找到 与朋友一起生成 模块并单击,或将其拖放到 3D 视图中。

在 资源管理器 窗口中,将整个 SpawnWithFriends 模型移入 ReplicatedStorage。在运行体验时,该模块将开始运行。
限制生成区域
此模块可能会导致玩家在限制区域生成,如 VIP 房间、仅限访问空间等。为防止玩家传送到这些区域:
用不可见的 Anchored 块填充限制区域。确保所有块的 CanCollide、CanTouch 和 CanQuery 都是 禁用 的。

填充整个监狱房间的块以防止玩家在内部生成 使用每个块属性的 标签 部分,应用标签 RestrictedSpawnArea 以便 CollectionService 可以检测到它们。
将以下代码粘贴到 ServerScriptService 中的 Script。
Scriptlocal ReplicatedStorage = game:GetService("ReplicatedStorage")local CollectionService = game:GetService("CollectionService")local SpawnWithFriends = require(ReplicatedStorage.SpawnWithFriends)local function validator(playerToTeleport, destinationPlayer, teleportationPoint)-- 遍历所有标记的零件for _, area in CollectionService:GetTagged("RestrictedSpawnArea") dolocal relativePosition = area.CFrame:PointToObjectSpace(teleportationPoint.Position)local size = area.Sizelocal inXBounds = relativePosition.X < size.X / 2 and relativePosition.X > -size.X / 2local inZBounds = relativePosition.Z < size.Z / 2 and relativePosition.Z > -size.Z / 2if inXBounds and inZBounds thenreturn false -- 生成目的地位于限制区域内;中止传送endendreturn true -- 生成目的地不重叠任何限制区域;继续传送endSpawnWithFriends.setTeleportationValidator(validator)
API 参考
函数
configure
configure(config: table)
通过 config 表中的以下键/值覆盖默认配置选项。此函数只能从 Script 中调用。
| 键 | 描述 | 默认值 |
|---|---|---|
| teleportToFriendOnRespawn | 如果设置为 false,则仅通过 teleportToRandomFriend 手动进行传送。 | true |
| teleportDistance | 玩家之间应相隔的距离,以 studs 为单位。 | 5 |
| maxCharacterVelocity | 移动速度快于此值的角色将不会被选为传送候选,例如正在移动的车辆中的角色。 | 48 |
| bypassFriendshipCheck | 如果设置为 true,所有 玩家都将是传送候选,而不仅仅是朋友。 | false |
| showLogs | 是否在输出中显示日志消息。 | false |
Scriptlocal ReplicatedStorage = game:GetService("ReplicatedStorage")local SpawnWithFriends = require(ReplicatedStorage.SpawnWithFriends)SpawnWithFriends.configure({teleportToFriendOnRespawn = true,teleportDistance = 5,maxCharacterVelocity = 48,bypassFriendshipCheck = false,showLogs = false})
teleportToRandomFriend
teleportToRandomFriend(playerToTeleport: Player): boolean
手动触发玩家传送到他们在体验中的一个朋友。返回一个布尔值,指示传送是否成功;传送失败可能是由于服务器中没有朋友或无法找到无障碍的传送点。此函数只能从 Script 中调用。
setTeleportationValidator
setTeleportationValidator(validator: function)
允许您通过连接验证器回调函数进行自定义的传送前检查。回调接收三个参数:
| 参数 | 描述 |
|---|---|
| playerToTeleport | 需要传送的 Player 的引用。 |
| destinationPlayer | 被传送到的目标 Player 的引用。 |
| teleportationPoint | CFrame,表示 playerToTeleport 要传送到的位置。 |
此函数及其回调只能在 Script 中使用,返回一个布尔值,指示是否应该继续传送。例如,以下验证器函数中的 return 逻辑确保生成玩家和目的地玩家在同一队伍中。
Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SpawnWithFriends = require(ReplicatedStorage.SpawnWithFriends)
-- 仅在玩家在同一队伍时才传送
local function validator(playerToTeleport, destinationPlayer, teleportationPoint)
return playerToTeleport.Team == destinationPlayer.Team
end
SpawnWithFriends.setTeleportationValidator(validator)