【问题标题】:Playing Music on Roblox locally在本地播放 Roblox 上的音乐
【发布时间】:2018-12-28 00:17:40
【问题描述】:

我有点沉迷于在我的 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 测试区域中确实可以工作,但是当使用它启动服务器时,不会播放任何声音。

服务器确实将声音设置为正在播放,并且它们从服务器端显示为在客户端播放,但是客户端不认为它们正在播放并且它们不播放。

我确实启用了过滤,但这不应该影响它...

【问题讨论】:

    标签: audio lua roblox


    【解决方案1】:

    我相信这很简单。我认为这是 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了解更多信息

    【讨论】:

    • 这是播放声音的方式,但播放 SoundService 声音只能在本地工作,不能通过服务器,所以在这种情况下它不会工作
    【解决方案2】:

    一种方法是把声音传给 PlayerGui(我认为你需要一个 ScreenGui),然后 :Play() 他们。

    【讨论】:

    • 是的,它们只能在 PlayerGui 中播放,尽管客户端上有本地脚本,所以 ScreenGui 将是这样做的方式,但是我想使用服务器来启动歌曲
    【解决方案3】:

    启用过滤是这不起作用的原因。

    要解决此问题,您可以使用位于游戏 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)
    

    【讨论】:

      【解决方案4】:

      我最近在制作音乐播放器时遇到了这个问题。它最终成为最愚蠢的事情,并且与 FE 有关。我的播放器在 Test Studio 中工作,但给了我一整套错误代码,我只需将脚本更改为本地脚本即可修复这些错误代码。从字面上看,所有相同的代码都像在工作室中一样工作,但在客户端。我花了一整天的时间挠头和 wiki 搜索来找出我的错误。 FE 最初对我来说是一场噩梦,但我开始喜欢它为我的游戏提供的安全性。 :)

      【讨论】:

      • 是的,FE 确实有点烂。这对系统来说是一个巨大的变化,但我更了解它,并且我对在 FE 中进行开发比现在更有信心。
      猜你喜欢
      • 1970-01-01
      • 2021-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-02
      • 2021-12-05
      • 1970-01-01
      相关资源
      最近更新 更多