在 Roblox 上本地播放音乐

我在我的 Roblox 游戏中遇到了本地音乐播放方面的一些问题。

我的游戏中有一个脚本,将 五个音频文件 插入到 玩家的 GUI 中。当建立服务器时,这些声音确实会出现在玩家的 GUI 中。

为了播放这些声音,我已经设置了一些部件来检测玩家的碰撞。当探测到玩家时,它们会在 玩家的 GUI 中播放一个 五个音频文件 中的一个。

以下是其中一个部件中的代码:

script.Parent.Touched:connect(function(hit)

if hit.Parent:FindFirstChild('Humanoid') then
    if game.Players[hit.Parent.Name].PlayerGui.Sound2.TimePosition < 1 then
        game.Players[hit.Parent.Name].PlayerGui.Sound2.Volume = 1
        game.Players[hit.Parent.Name].PlayerGui.Sound2:Play()
        game.Players[hit.Parent.Name].PlayerGui.Sound1:Stop()
        game.Players[hit.Parent.Name].PlayerGui.Sound4:Stop()
        game.Players[hit.Parent.Name].PlayerGui.Sound3:Stop()
        game.Players[hit.Parent.Name].PlayerGui.Sound5:Stop()
    end
end

end)

我已经测试过这个脚本能够正常地检测到玩家。该系统在 Roblox Studio 测试区域中确实有效,但是在设置了服务器后,没有任何声音被播放。

事实上,服务器确实将这些声音设置为正在播放,并且在客户端从服务器端看起来正在播放,但客户端看不到它们正在播放,也听不到它们的声音。

我已经开启了数据库过滤功能,但这不应该影响它...

点赞
用户10109881
用户10109881

我相信这是某个十分简单的东西。我觉得这是 SoundService 的一个函数。如果我错了,请纠正我,但我相信它是这样的:

soundobj = game.Players[hit.Parent.Name].PlayerGui.Sound2

game:GetService('SoundService'):PlayLocalSound(soundobj)

请参阅 https://wiki.roblox.com/index.php?title=API:Class/SoundService/PlayLocalSound 以获取更多信息。

2018-07-20 09:01:13
用户88888888
用户88888888

一种方法是将音频的Parent设置为PlayerGui(如果您需要ScreenGui,可以考虑修改),然后使用:Play()播放音频。

2018-07-24 04:41:42
用户6506067
用户6506067

FilteringEnabled 是这段代码无法工作的原因。

为了解决这个问题,你可以使用游戏中的 ReplicatedStorage 中的 RemoteEvent

声音应该被放置在 StarterGUI 中,并且应该有一个本地脚本,脚本如下:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("REMOTE EVENT NAME")
script.Parent.Sound2.Playing = true

local function onNewPlayerFired(sound)
    script.Parent.Sound1.Playing = false
    script.Parent.Sound2.Playing = false
    script.Parent.Sound3.Playing = false
    script.Parent.Sound4.Playing = false
    script.Parent.Sound5.Playing = false

    script.Parent[sound].Playing = true

end

event.OnClientEvent:Connect(onNewPlayerFired)

在每个声音触发部分,应该加入以下代码:

local debounce = false

script.Parent.Touched:connect(function(hit)
    if debounce == true then return end
    debounce = true
    if hit.Parent:FindFirstChild('Humanoid') then
        local plr = game.Players:FindFirstChild(hit.Parent.Name)
        game.ReplicatedStorage.REMOTE EVENT NAME:FireClient(plr,"SOUND NAME")

    end
wait(2)
    debounce = false

end)
2018-07-27 17:03:24
用户10307547
用户10307547

我最近在制作音乐播放器时遇到了这个问题。最后发现问题很傻,和前端有关。我的播放器能够在Test Studio中工作,但在客户机端却给了我一长串错误代码,我只需要将脚本更改为本地脚本就可以解决问题。重点是,在工作室中的所有相同代码都能在客户机端正常工作。我花了整整一天的时间在头痛和维基搜索中才找到我的错误。起初,FE对我来说是一场噩梦,但我已经开始喜欢它为游戏提供的安全性了。 :)

2018-09-02 19:16:52