与朋友一起生成

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

在体验中找到朋友可能是一个挑战。SpawnWithFriends 开发者模块 会自动将生成的玩家移动到体验中某个朋友附近。此模块也可以配置为在命令下传送玩家,而不是自动传送。

模块使用

安装

要在体验中使用 SpawnWithFriends 模块:

  1. 从 Studio 的 窗口 菜单或 首页 选项卡工具栏打开 工具箱,然后选择 创作者商店 选项卡。

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

  3. 找到并单击 瓷砖。

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

  5. 资源管理器 窗口中,将整个 SpawnWithFriends 模型移入 ReplicatedStorage。在运行体验时,该模块将开始运行。

限制生成区域

此模块可能会导致玩家在限制区域生成,如 VIP 房间、仅限访问空间等。为防止玩家传送到这些区域:

  1. 用不可见的 Anchored 块填充限制区域。确保所有块的 CanCollideCanTouchCanQuery 都是 禁用 的。

    填充整个监狱房间的块以防止玩家在内部生成
  2. 使用每个块属性的 标签 部分,应用标签 RestrictedSpawnArea 以便 CollectionService 可以检测到它们。

  3. 将以下代码粘贴到 ServerScriptService 中的 Script

    Script

    local 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") do
    local relativePosition = area.CFrame:PointToObjectSpace(teleportationPoint.Position)
    local size = area.Size
    local inXBounds = relativePosition.X < size.X / 2 and relativePosition.X > -size.X / 2
    local inZBounds = relativePosition.Z < size.Z / 2 and relativePosition.Z > -size.Z / 2
    if inXBounds and inZBounds then
    return false -- 生成目的地位于限制区域内;中止传送
    end
    end
    return true -- 生成目的地不重叠任何限制区域;继续传送
    end
    SpawnWithFriends.setTeleportationValidator(validator)

API 参考

函数

configure

configure(config: table)

通过 config 表中的以下键/值覆盖默认配置选项。此函数只能从 Script 中调用。

描述默认值
teleportToFriendOnRespawn如果设置为 false,则仅通过 teleportToRandomFriend 手动进行传送。true
teleportDistance玩家之间应相隔的距离,以 studs 为单位。5
maxCharacterVelocity移动速度快于此值的角色将不会被选为传送候选,例如正在移动的车辆中的角色。48
bypassFriendshipCheck如果设置为 true,所有 玩家都将是传送候选,而不仅仅是朋友。false
showLogs是否在输出中显示日志消息。false
Script

local 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 的引用。
teleportationPointCFrame,表示 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)
©2026 Roblox Corporation、Roblox、Roblox 标志及 Powering Imagination 是我们在美国及其他国家或地区的注册与未注册商标。