中级教程

添加语音聊天

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

语音聊天是一种基于接近的聊天功能,通过调整玩家在3D空间中彼此靠近或远离时的音量,模拟真实的沟通。通过让玩家使用麦克风进行交流,他们能够在全球范围内实时社交和协作,以完成您游戏的目标。

使用 Gingerbread Island - Voice Chat .rbxl 文件作为参考,本教程向您展示如何将不同形式的语音聊天融入到您的游戏中,包括以下指导:

  • 将玩家分组到只能与队友使用语音聊天的团队中。
  • 允许玩家在按下设备上的特定按钮时暂时激活他们的麦克风。
  • 配置语音聊天启用或禁用的时间段。

在您查看以下部分时,可以根据示例调整每个代码示例,以更好地满足您自己的语音聊天需求。

配置设置

为了使团队聊天、推送聊天或基于时间的聊天正常工作,您必须 配置您的语音聊天设置,以启用语音聊天并创建在3D环境中接收和发出音频所需的适当音频对象。

当玩家加入您的游戏时,您的语音聊天设置现在:

以下部分详细介绍了使用这些设置的三种独特语音聊天配置。当您跟随示例 Gingerbread Island - Voice Chat 放置文件时,可以在 Explorer 窗口中启用相应的禁用脚本以测试其行为。为了确保每个脚本正常工作,在继续下一个配置之前,请确保再次禁用该脚本

Explorer 窗口中的禁用脚本

添加团队聊天

团队聊天是一种语音聊天配置,其中只有同一团队的玩家可以在游戏中相互交谈或听到彼此。将团队聊天集成到您的游戏中非常有用,当您希望玩家协作并共同制定策略以解决游戏中的问题时,例如协调敌方团队的位置、资源和任务。

要在示例 Gingerbread Island - Voice Chat 放置文件中重建团队语音聊天:

  1. Explorer 窗口中,将 Script 插入 ServerScriptService

  2. 将脚本重命名为 TeamChat,然后将以下代码粘贴到脚本中:

    local Teams = game:GetService("Teams")
    local Players = game:GetService("Players")
    local redTeam = Instance.new("Team", Teams)
    redTeam.TeamColor = BrickColor.new("Bright red")
    redTeam.AutoAssignable = true
    redTeam.Name = "Red Team"
    local blueTeam = Instance.new("Team", Teams)
    blueTeam.TeamColor = BrickColor.new("Bright blue")
    blueTeam.AutoAssignable = true
    blueTeam.Name = "Blue Team"
    local function getUserIds(team : Team) : {number}
    local userIds = {}
    for _, player : Player in team:GetPlayers() do
    table.insert(userIds, player.UserId)
    end
    return userIds
    end
    local function getDevices(team : Team) : {AudioDeviceInput}
    local devices = {}
    for _, player : Player in team:GetPlayers() do
    local device : AudioDeviceInput = player:FindFirstChild("AudioDeviceInput")
    if not device then
    continue
    end
    table.insert(devices, device)
    end
    return devices
    end
    local function updateTeam(team : Team)
    local users = getUserIds(team)
    for _, device in getDevices(team) do
    device.AccessType = Enum.AccessModifierType.Allow
    device:SetUserIdAccessList(users)
    end
    end
    local function onDeviceAdded(device : AudioDeviceInput)
    local player : Player = device.Parent
    if player.Team then
    updateTeam(player.Team)
    end
    end
    local function onPlayerAdded(player : Player)
    local device = player:FindFirstChild("AudioDeviceInput")
    if device then
    onDeviceAdded(device)
    end
    player.ChildAdded:Connect(function(child)
    if child.Name == "AudioDeviceInput" then
    onDeviceAdded(child)
    end
    end)
    end
    updateTeam(blueTeam)
    updateTeam(redTeam)
    for _, player in Players:GetPlayers() do
    onPlayerAdded(player)
    end
    Players.PlayerAdded:Connect(onPlayerAdded)
    blueTeam.PlayerAdded:Connect(function() updateTeam(blueTeam) end)
    blueTeam.PlayerRemoved:Connect(function() updateTeam(blueTeam) end)
    redTeam.PlayerAdded:Connect(function() updateTeam(redTeam) end)
    redTeam.PlayerRemoved:Connect(function() updateTeam(redTeam) end)

    该脚本首先获取 TeamsPlayers 服务,以便可以利用它们的现成功能在玩家加入游戏时将其分组到团队中。例如,在没有任何额外脚本工作的情况下,Teams 服务处理诸如:

    • 将玩家均匀地排序和平衡到每个团队中。
    • 在排行榜上将玩家分组到他们的团队下。
    • 在3D空间中将玩家名称着色为对应的团队颜色。

    使用此服务,脚本创建两个不同的 Team 对象,具有不同的 Color 属性值,以表示每个团队:一个团队为 亮红色,另一个团队为 亮蓝色

    然后,脚本定义了三个函数,其中大部分工作发生在设置团队语音聊天配置中:

    • getUserIds - 返回团队中所有玩家的用户ID数组。
    • getDevices - 返回团队中所有玩家的 AudioDeviceInput 对象数组。每个 Class.AudioDeviceInput 对象代表玩家在现实世界中的 物理麦克风
    • updateTeam - 从 getUserIds 获取所有用户ID,遍历 getDevices 中的 AudioDeviceInput 对象,将它们的 Enum.AccessModifierType 属性设置为 Allow,以便只有每个团队中的用户ID被允许听到其队友的麦克风,然后使用团队中的用户ID更新 SetUserIdAccessList

    脚本的其余部分控制这些函数和事件监听器如何协同工作:

    • 当新玩家加入游戏时,脚本验证该玩家是否有麦克风并更新其团队。
    • 当脚本检测到新的 AudioDeviceInput 对象时,它会调用 updateTeam 以更新其相应的红色或蓝色团队。
    • 随着玩家加入或离开游戏,脚本连接到 PlayerAddedPlayerRemoved 事件以更新每个团队的设置。
  3. 与几个朋友一起进行游戏测试,以验证每个团队的队友只能通过语音聊天听到彼此。

添加推送聊天

推送聊天是一种语音聊天配置,其中玩家只能在游戏中按住设备上的特定按钮时激活他们的麦克风。将推送聊天集成到您的游戏中非常有用,当您希望玩家在想要被其他玩家和环境噪音听到时拥有更多隐私和控制权。

要在示例 Gingerbread Island - Voice Chat 放置文件中重建推送语音聊天:

  1. Explorer 窗口中,将 Script 插入 ReplicatedStorage

  2. Properties 窗口中,将 RunContext 设置为 Client,以便脚本仅控制本地玩家的麦克风。

  3. 将脚本重命名为 PushToChat,然后将以下代码粘贴到脚本中:

    local Players = game:GetService("Players")
    local UserInputService = game:GetService("UserInputService")
    local audioIn: AudioDeviceInput = Players.LocalPlayer:WaitForChild("AudioDeviceInput")
    local pushToTalkKey = Enum.KeyCode.V
    audioIn.Muted = true
    UserInputService.InputBegan:Connect(function(input: InputObject)
    if input.KeyCode == pushToTalkKey then
    audioIn.Muted = false
    end
    end)
    UserInputService.InputEnded:Connect(function(input: InputObject)
    if input.KeyCode == pushToTalkKey then
    audioIn.Muted = true
    end
    end)

    该脚本首先获取:

    • Players 服务,以便可以引用游戏中的所有玩家。
    • UserInputService 服务,以便可以检查玩家何时按下设备上的按钮。
    • 本地玩家的 AudioDeviceInput 对象,或其设备上的物理麦克风。

    然后,脚本:

    • V 键设置为玩家需要按住的按钮,以便通过麦克风说话。
    • 将玩家的麦克风静音。

    脚本的其余部分为 UserInputService 设置了两个事件监听器,分别用于 InputBeganInputEnded。当他们按下 V 键时,InputBegan 事件会取消静音玩家的麦克风,而当他们释放 V 键时,InputEnded 事件会再次将其麦克风静音。

  4. 进行游戏测试,以验证玩家只有在按下 V 键时才能通过语音聊天听到彼此。

添加基于时间的聊天

基于时间的聊天是一种语音聊天配置,其中玩家只能在设定的时间段内相互交谈和听到彼此。将基于时间的聊天集成到您的游戏中非常有用,当您希望玩家在特定阶段仔细计划如何与彼此沟通时,例如在回合讨论或过场动画后。

要在示例 Gingerbread Island - Voice Chat 放置文件中重建基于时间的语音聊天:

  1. Explorer 窗口中,将 Script 插入 ServerScriptService

  2. 将脚本重命名为 TimeBasedChat,然后将以下代码粘贴到脚本中:

    local Players = game:GetService("Players")
    local muteAll = false
    local function toggleMuteAll()
    muteAll = not muteAll
    for _, player in Players:GetPlayers() do
    local device : AudioDeviceInput = player:FindFirstChild("AudioDeviceInput")
    if not device then
    continue
    end
    device.Muted = muteAll
    end
    end
    while true do
    task.wait(15) -- 每15秒
    toggleMuteAll() -- 允许人们说话,或阻止他们说话
    end

    该脚本首先获取 Players 服务,以便可以引用其功能来管理游戏中的所有玩家。然后,它将标志 muteAll 设置为 false,后续的 toggleMuteAll 函数使用该标志来切换语音聊天的开启和关闭。

    让我们回顾一下 toggleMuteAll 函数:

    • 它首先使用 Players.GetPlayers 循环遍历游戏中的所有玩家。
    • 对于每个玩家,它检查是否有子 AudioDeviceInput 对象,或其设备上的物理麦克风。
    • 如果有,函数将 AudioDeviceInput 对象的 Muted 属性设置为 muteAll 的值。例如,如果 muteAlltrue,则玩家的麦克风被禁用;如果 muteAllfalse,则玩家的麦克风被启用。

    然后,脚本进入一个无限循环,每15秒等待一次以静音和取消静音所有 AudioDeviceInput 对象。

  3. 进行游戏测试,以验证玩家只能在15秒的时间段内通过语音聊天听到彼此。

©2026 Roblox Corporation、Roblox、Roblox 标志及 Powering Imagination 是我们在美国及其他国家或地区的注册与未注册商标。