单击对象时会播放声音



单击该部分时无法播放声音。

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

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)

您可以尝试此代码

local function onMouseClick(Player)
if not Player.PlayerGui:findFirstChild("ClickAudio") then 
local ls = Instance.new("Sound")
local length = 1 -- How long the sound plays before removed.
ls.SoundId = "http://www.roblox.com/asset/?id=" -- Audio id here.
ls.Name = "ClickAudio"
ls.Volume = 0.5 -- You can set the volume (Suggestion: keep it below 1)
ls.Pitch = 1 -- This is the pitch of the sound
ls.Parent = Player.PlayerGui
ls:Play()
game:GetService("Debris"):AddItem(ls, length)
end
end
script.Parent.MouseClick:connect(onMouseClick)

您将不得不更改SoundId="0"并粘贴在你想播放的声音的链接中,希望这能回答你的问题!

我知道你是一个RLua开发人员。你能把声音放在当地吗?

顺便说一句,客户端脚本看起来是这样的:

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

好吧,你可以制作一个遥控器,当点击按钮时就会启动,使其成为服务器端(每个人都能听到(。制作一个远程,在任何地方添加脚本(非本地(,代码应该是这样的:

local Sound = workspace.Sound
local RemoteEvent = game.ReplicatedStorage.PlaySound
RemoteEvent.OnServerEvent:Connect(function()
Sound:Play() -- Yes, it's all LOL
end)

我忘了告诉你,你所有的代码都是客户端的,其他玩家不会看到灯变了,但是。。。如果你的游戏有太多可定制的遥控器(比如你为管理员制作了一个可以开火杀人的遥控器,那么它可能会被黑客滥用(,所以这不是最好的——我不会撒谎。一个游戏不可能有一次破解。

最新更新