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

How do I make the music stop playing once the player has stepped out of the music playing brick?

Asked by 8 years ago

I have the region brick setup like this: SoundRegion (CanCollide off) Sound Script

The code of the script is like this:

local brick = script.Parent
local sound = brick.Sound
debounce = false

brick.Touched:connect(function(hit)
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    if plr and not debounce then
        debounce = true
        local play = sound:Clone()
        play.Parent = plr.PlayerGui
        play:Play()
        wait(5)
        debounce = false
    end
end)

I've tried so many things (I've lost my progress a lot because Studio crashes a lot for some reason), but I can't make the music stop.

Please help me!

1 answer

Log in to vote
0
Answered by 8 years ago

I have made an incredibly complicated way to make it work, but it works, so here it goes;

1: Create the part that you want the music to be played in, and name it 'SoundPlayer' 2: Insert Sound and name it 'Sound'. Insert Script and name it 'Play'. Finally, insert BoolValue and name it 'Debounce'

3: After all of that, put this code in the Script:

brick = script.Parent -- Rename the object you are stepping on 'Part'
sound = brick.Sound
debounce = script.Parent:findFirstChild("Debounce")

brick.Touched:connect(function(hit)
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    if plr and debounce.Value == false then
        debounce.Value = true
        local play = sound:Clone()
        play.Parent = plr.PlayerGui
        play:Play()
        wait()
    end
end)

4: Create another part, and name it 'SoundRemover'

5: Insert a Script into this part, and name it 'Remove'. Then put in this code:

area = script.Parent -- Rename the object you are stepping on 'Part'
debounce = game.Workspace.Soundplayer:findFirstChild("Debounce")

area.Touched:connect(function(hit)
        local play = game.Players:GetPlayerFromCharacter(hit.Parent)
        local music = play.PlayerGui:findFirstChild("Sound")
    if debounce.Value == true and music.IsPlaying == true then
        music:remove()
        debounce.Value = false  
    else
    end
end)

Finally, duplicate 'SoundRemover' if necessary, and place it all around 'SoundPlayer' to make sure if the player leaves from any angle, the script stops the music.

If you have any questions about it, PM me or reply to this answer :)

Ad

Answer this question