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.
01 | Sounds = script:WaitForChild 'SongFolder' |
02 |
03 | Player = game.Players.LocalPlayer |
04 | PlayerGui = Player.PlayerGui |
05 | Leaderstats = Player:WaitForChild 'leaderstats' |
06 | Stage = Leaderstats:WaitForChild 'Stage' |
07 |
08 | Song 1 = Sounds:WaitForChild 'Sound1' |
09 | Song 2 = Sounds:WaitForChild 'Sound2' |
10 | Song 3 = Sounds:WaitForChild 'Sound3' |
11 | Song 4 = Sounds:WaitForChild 'Sound4' |
12 |
13 |
14 | if Stage.Value = = 1 and Song 1. IsPlaying ~ = true then |
15 | Song 1 :Play() |
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
01 | Sounds = script:WaitForChild 'SongFolder' |
02 |
03 | local Songs = { |
04 | Sounds:WaitForChild 'Sound1' , |
05 | Sounds:WaitForChild 'Sound2' , |
06 | Sounds:WaitForChild 'Sound3' , |
07 | Sounds:WaitForChild 'Sound4' |
08 | } |
09 |
10 | Player = game.Players.LocalPlayer |
11 | PlayerGui = Player.PlayerGui |
12 | Leaderstats = Player:WaitForChild 'leaderstats' |
13 | Stage = Leaderstats:WaitForChild 'Stage' |
14 |
15 | function PlaySound(id) -- Stops all the songs that are currently playing, then plays the sound. |
01 | Sounds = script:WaitForChild 'SongFolder' |
02 |
03 | Player = game.Players.LocalPlayer |
04 | PlayerGui = Player.PlayerGui |
05 | Leaderstats = Player:WaitForChild 'leaderstats' |
06 | Stage = Leaderstats:WaitForChild 'Stage' |
07 |
08 | Song 1 = Sounds:WaitForChild 'Sound1' |
09 | Song 2 = Sounds:WaitForChild 'Sound2' |
10 | Song 3 = Sounds:WaitForChild 'Sound3' |
11 | Song 4 = Sounds:WaitForChild 'Sound4' |
12 |
13 | if Stage.Value = = 1 and Song 1. IsPlaying ~ = true then |
14 | Song 1 :Play() |
15 | end |
Tell me if it works, didn't test it in studio.