Empty
Empty
Empty
SoundService 是 Roblox 中处理游戏内声音播放的服务。它允许您管理全局声音设置和背景音乐、公告及其他声音的播放,这些声音应在游戏世界中的所有玩家都能听到。
循环播放背景音乐
Roblox Studio
从脚本播放声音
Empty
Empty
Empty
- 服务器脚本: 如果服务器端脚本播放一个附加到 Workspace 中部分的声音,通常情况下,靠近该部分的所有玩家都能听到。
- 本地脚本: 如果客户端脚本播放声音,只有运行该脚本的玩家能够听到。
Empty
Empty
Empty
-- 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)
Empty
Empty
Empty
-- 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)