点击物体时播放声音

当我点击物体时无法播放声音。

我该如何将声音代码集成到以下代码中?

local ClickDetector = script.Parent.ClickDetector

ClickDetector.MouseClick:Connect(function()
    script.Parent.SurfaceLight1.Enabled = not script.Parent.SurfaceLight1.Enabled
end)

ClickDetector.MouseClick:Connect(function()
    script.Parent.SurfaceLight2.Enabled = not script.Parent.SurfaceLight2.Enabled
end)

ClickDetector.MouseClick:Connect(function()
    script.Parent.SurfaceLight3.Enabled = not script.Parent.SurfaceLight3.Enabled
end)

ClickDetector.MouseClick:Connect(function()
    script.Parent.SurfaceLight4.Enabled = not script.Parent.SurfaceLight4.Enabled
end)
点赞
用户13137824
用户13137824

我发现你是一个 RLua 开发者。你能制作一下声音的本地副本吗?

顺便说一下,客户端脚本应该是这样的:

local Sound = workspace.Sound
ClickDetector.MouseClick:Connect(function()
    Sound:Play()
end)

那么,你可以制作一个远程脚本,当按钮被点击时触发,使其成为服务端脚本(所有人都能听到声音)。制作一个远程脚本,在任何地方添加一个_script_(不是本地)代码应该是这样的:

local Sound = workspace.Sound
local RemoteEvent = game.ReplicatedStorage.PlaySound
RemoteEvent.OnServerEvent:Connect(function()
Sound:Play() -- 是的,就这么简单
end)

我忘了告诉你,所有的代码都是客户端的,其他玩家不会看到灯光的变化,但是......如果你的游戏有太多可定制的远程脚本(比如你制作一个远程脚本,可以为管理员提供杀死玩家的功能,那么它就可能会被黑客滥用),所以这不是最好的选择-我不会撒谎。一个游戏不能只有一个远程脚本。

2020-11-08 07:40:31
用户14539093
用户14539093

你可以尝试以下代码

local function onMouseClick(Player)
    if not Player.PlayerGui:findFirstChild("ClickAudio") then
        local ls = Instance.new("Sound")
        local length = 1 -- 音频播放时间

        ls.SoundId = "http://www.roblox.com/asset/?id=" -- 在这里输入音频id
        ls.Name = "ClickAudio"
        ls.Volume = 0.5 -- 设置音量(建议低于1)
        ls.Pitch = 1 -- 设置音调
        ls.Parent = Player.PlayerGui
        ls:Play()

        game:GetService("Debris"):AddItem(ls, length)
    end
end
script.Parent.MouseClick:connect(onMouseClick)

你需要更改 SoundId = "" 并粘贴你想要播放的音频链接,我希望这个回答解决了你的问题!

2020-11-10 17:33:15