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

How do I make a sound stop playing when it's inside of PlayerScripts?

Asked by 5 years ago

I'm using two different models, one plays background music while the other blows things up, the problem I'm having is that they both play music. The one that blows up plays music when it is activated, and the other one always plays music. I'm trying to make the one that plays music stop playing when the one that blows up is activated. After some research, I found that the music is inside of a local script. The local script's parent is PlayerScripts, I'm not good at using "GetChildren()", so sorry if I used it incorrectly it, here is a snippet of my script:

for i,v in pairs(game.Players:GetChildren()) do -- Did I use this wrong?
            local Alarm = AssetsF:WaitForChild("Alarm") -- This stuff is what the bomb plays
            local Alarmb = AssetsF:WaitForChild("Alarm1")
            local Music = PlayerScripts:GetChildren()
            if not Alarm.IsPlaying then
                Alarm:Play()
                Alarmb:Play()
                Music.LocalBackgroundMusic:Destroy() -- This is the script that has the sound in it
                end
            end
        end

A big thanks if you can help me!

0
have you tried :Stop() ? ieatandisbaconhair 77 — 5y
0
I can't find out how to get the sound, so that wouldn't do anything. CaptainAlien132 225 — 5y

1 answer

Log in to vote
0
Answered by
poke7667 142
5 years ago
Edited 5 years ago

The script automatically runs as soon as a player joins your game. Meaning IsPlaying is only checked as soon as the script runs. What you should do is Parent the localscript under StarterPlayerScripts, that way it'll be saved under PlayerScripts for every player that joins. Then, you should use the event called Changed. Changed will fire every time a property is changed (hence changed). Eliminate the for loop and used the Changed event instead like this:

Alarm.Changed:Connect(function()
    -- Code here
end)

I'm not very good at explaining things, but I hope this was sufficient enough.

0
I'll try it out. CaptainAlien132 225 — 5y
Ad

Answer this question