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)