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

Sound Glitches On Death?

Asked by
yoshi8080 445 Moderation Voter
8 years ago

Making a little sound stage thing for a friend, however it's a bit glitchy after a player dies. No errors though. This is a local script placed in StarterGui

Problem: After player once or more, the sound stops and won't play.

Sounds = script:WaitForChild'SongFolder'

Player = game.Players.LocalPlayer
PlayerGui = Player.PlayerGui
Leaderstats = Player:WaitForChild'leaderstats'
Stage = Leaderstats:WaitForChild'Stage'

Song1 = Sounds:WaitForChild'Sound1'
Song2 = Sounds:WaitForChild'Sound2'
Song3 = Sounds:WaitForChild'Sound3'
Song4 = Sounds:WaitForChild'Sound4'


if Stage.Value == 1 and Song1.IsPlaying ~= true then
Song1:Play()
end


Stage.Changed:connect(function(NewValue)
if NewValue == 2 and Song2.IsPlaying ~= true then
Song2:Play()
Song1:Stop()

end
end)

Stage.Changed:connect(function(NewValue)
if NewValue == 3 and Song3.IsPlaying ~= true then
Song3:Play() 
Song2:Stop()

end
end)

Stage.Changed:connect(function(NewValue)
if NewValue == 4 and Song4.IsPlaying ~= true then
Song4:Play() 
Song3:Stop()

end
end)

2 answers

Log in to vote
1
Answered by
XAXA 1569 Moderation Voter
8 years ago

Problem

Every time the player spawns, if Stage.Value is > 1, the appropriate song will not play.

Suggestions

Given how you name your sounds, it would be best if these were stored in a table so you can iterate through it and index them neatly. As a plus, you can get rid of those ugly redundant if-statements.

Solution

Sounds = script:WaitForChild'SongFolder'

local Songs = {
Sounds:WaitForChild'Sound1',
Sounds:WaitForChild'Sound2',
Sounds:WaitForChild'Sound3',
Sounds:WaitForChild'Sound4'
}

Player = game.Players.LocalPlayer
PlayerGui = Player.PlayerGui
Leaderstats = Player:WaitForChild'leaderstats'
Stage = Leaderstats:WaitForChild'Stage'

function PlaySound(id) -- Stops all the songs that are currently playing, then plays the sound.
    for _, v in pairs(Songs) do
        v:Stop()
    end
    Songs[id]:Play()
end

PlaySound(Stage.Value) -- play the appropriate song every time the player spawns
Stage.Changed:connect(function(NewValue) -- whenever the stage changes, play the appropriate song.
    PlaySound(NewValue)
end)
Ad
Log in to vote
0
Answered by 8 years ago
Sounds = script:WaitForChild'SongFolder'

Player = game.Players.LocalPlayer
PlayerGui = Player.PlayerGui
Leaderstats = Player:WaitForChild'leaderstats'
Stage = Leaderstats:WaitForChild'Stage'

Song1 = Sounds:WaitForChild'Sound1'
Song2 = Sounds:WaitForChild'Sound2'
Song3 = Sounds:WaitForChild'Sound3'
Song4 = Sounds:WaitForChild'Sound4'

if Stage.Value == 1 and Song1.IsPlaying ~= true then
Song1:Play()
end
 Stage.Changed:connect(function(NewValue)
if NewValue == 2 and Song2.IsPlaying ~= true then
Song2:Play()
Song1:Stop()
 end
end)
Stage.Changed:connect(function(NewValue)
if NewValue == 3 and Song3.IsPlaying ~= true then
Song3:Play()
Song2:Stop() 
end
end)

Stage.Changed:connect(function(NewValue)
if NewValue == 4 and Song4.IsPlaying ~= true then
Song4:Play()
Song3:Stop()
end
end)
if
game.Players:FindFirstChilden():LoadCharacter() == true and song1 or song2 or song3 or song4.IsPlaying == true then
for i,v = 1,4 
song[i,v]:stop()
end
end


Tell me if it works, didn't test it in studio.

0
I think line 37 is wrong, it's seems incomplete yoshi8080 445 — 8y
0
oops, for i,v = 1,4 do See if that works? I'm not good with i,v. Dominus113 0 — 8y
0
I see yoshi8080 445 — 8y

Answer this question