SoundService (声音服务)

SoundService 是 Roblox 中处理游戏内声音播放的服务。它允许您管理全局声音设置和背景音乐、公告及其他声音的播放,这些声音应在游戏世界中的所有玩家都能听到。

循环播放背景音乐

Image 1
Roblox Studio

从脚本播放声音

  • 服务器脚本: 如果服务器端脚本播放一个附加到 Workspace 中部分的声音,通常情况下,靠近该部分的所有玩家都能听到。
  • 本地脚本: 如果客户端脚本播放声音,只有运行该脚本的玩家能够听到。
-- Workspace > SoundPart > PlayStepSoundScript

local soundPart = script.Parent
local stepSound = soundPart:WaitForChild("StepSound")


local function onTouched(otherPart)
    local character = otherPart.Parent
    local humanoid = character:FindFirstChildOfClass("Humanoid")
    if humanoid then
        stepSound:Play()
    end
end

soundPart.Touched:Connect(onTouched)
-- StarterPlayerScripts > LocalStepSoundScript

local player = game.Players.LocalPlayer
local soundPart = workspace:WaitForChild("SoundPart")
local stepSound = soundPart:WaitForChild("StepSound")

local function onTouched(otherPart)
    local character = otherPart.Parent
    if character == player.Character then
        local humanoid = character:FindFirstChildOfClass("Humanoid")
        if humanoid then
            stepSound:Play()
        end
    end
end

soundPart.Touched:Connect(onTouched)

如果您觉得本教程对您有帮助,并且愿意支持我的工作,请考虑请我喝杯咖啡。

非常感谢您的支持!

请我喝咖啡