Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why won't music located in "StarterGui" play?

Asked by 7 years ago

I am making a game where (once a button is pressed) a series of lanterns are released into the air.

Part of this is taking "LIGHT" which is an audio snipit inside "StarterGui" and making it play as the lanterns are floating up.

I think it has to do with how my script is NOT local, but the music needs to be played locally.

Here is my code:

**local light = game.StarterGui.LIGHT**

script.Parent.ClickDetector.MouseClick:connect(function()
    script.Parent.Transparency = 1
    local la = game.Workspace.Lanterns
    local lag = la:GetChildren()
    for _,v in pairs (lag) do
        spawn(function()
            if v.Name == "LanternAtt" then
                local attA = v:FindFirstChild("AttachmentA")
                if attA then
                    local at = attA:FindFirstChild("Attachment")
                    if at then
                        **light:Play()**
                        at:Destroy()
                    end
                end
            end
        end)
    end
    wait(100)
    for _,v in pairs (lag) do
        v:Destroy()
    end
end)

2 answers

Log in to vote
1
Answered by
DanzLua 2879 Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

The reason this isn't working is because you are attempting to play a sound instance which is inside startergui, startergui is used to replicated its contents to the PlayerGui of the player. to fix this we need to navigate to the sound instance inside the player's PlayerGui. We can also use the argument that the mouseclick event gives to us to get to the sound.

script.Parent.ClickDetector.MouseClick:connect(function(plr)--MouseClick event gives us the player
    script.Parent.Transparency = 1
    local la = game.Workspace.Lanterns
    local lag = la:GetChildren()
    for _,v in pairs (lag) do
        spawn(function()
            if v.Name == "LanternAtt" then
                local attA = v:FindFirstChild("AttachmentA")
                if attA then
                    local at = attA:FindFirstChild("Attachment")
                    if at then
                        plr.PlayerGui.LIGHT:Play()--look at how I navigated to the player's PlayerGui
                        at:Destroy()
                    end
                end
            end
        end)
    end
    wait(100)
    for _,v in pairs (lag) do
        v:Destroy()
    end
end)
0
Looks like you were a few seconds faster haha Asceylos 562 — 7y
0
@Asceylos ;p i type fast DanzLua 2879 — 7y
1
Thank you @DanzLua and @Asceylos, both answers make great sense! AidanThDev 9 — 7y
Ad
Log in to vote
0
Answered by
Asceylos 562 Moderation Voter
7 years ago

It is because you don't make the music inside the player play, but inside StarterGui.

script.Parent.ClickDetector.MouseClick:connect(function(player)
    local light = player.PlayerGui.LIGHT
    script.Parent.Transparency = 1
    local la = game.Workspace.Lanterns
    local lag = la:GetChildren()
    for _,v in pairs (lag) do
        spawn(function()
            if v.Name == "LanternAtt" then
                local attA = v:FindFirstChild("AttachmentA")
                if attA then
                    local at = attA:FindFirstChild("Attachment")
                    if at then
                        light:Play()
                        at:Destroy()
                    end
                end
            end
        end)
    end
    wait(100)
    for _,v in pairs (lag) do
        v:Destroy()
    end
end)

Feel free to accept this answer if it helped you.

Answer this question