Answered by
5 years ago Edited 5 years ago
Hey bud, I was helping you out with your other post on this. All you need to do to fix this is add one line of code and change the order around a little. Here is your updated code:
01 | local Event = game.ReplicatedStorage.ApoxyzMusicChange |
05 | game.Players.PlayerAdded:Connect( function (player) |
07 | if Activate = = false then |
15 | local randomKey = keys [ math.random(#keys) ] |
19 | musicName = musicList [ randomKey ] |
20 | Event:FireAllClients(musicName) |
24 | script.Parent.Ended:Wait() |
28 | Event:FireClient(player, musicName) |
Also, I remember your other post on this, so don't change Activate back to false. You wanted the while loop to run only once as I remember. I hope this helps out! If it doesn't, please keep this post opened and add a comment under my post. I check my answers for feedback regularly.
I also noticed that you structured your code differently than what I did to prevent the Studio bug. Unless they fixed this (it's been around for a long time, so doubtful) you will definitely experience the bug as your game grows. It's ultimately your choice, but I promise it will save you some headache down the line.
[EDIT] The second problem probably should've been in another post since it was new problems based off of your changes, but I fixed it for you anyway. All I did was move some things around a little bit, you were definitely on the right track.
Here is the updated script:
01 | local Sound = Instance.new( "Sound" , game.Workspace) |
02 | Sound.Name = "SongAcreol2" |
04 | [ "356718047" ] = "N'to Trauma" , |
05 | [ "372492410" ] = "Tell Me Why" , |
06 | [ "2468961271" ] = "Mine The Diamond" |
08 | local Event = game.ReplicatedStorage.ApoxyzMusicChange |
12 | local Change = game.ReplicatedStorage.ChangeSong |
13 | local Skip = game.ReplicatedStorage.SkipSong |
15 | for key, _ in pairs (musicList) do |
16 | table.insert(keys, key) |
22 | local randomKey = keys [ math.random(#keys) ] |
25 | musicName = musicList [ randomKey ] |
26 | Event:FireAllClients(musicName) |
30 | game.Players.PlayerAdded:Connect( function (player) |
31 | if Activate = = false then |
35 | Event:FireClient(player, musicName) |
49 | Skip.OnServerEvent:Connect( function () |
52 | local randomKey = keys [ math.random(#keys) ] |
54 | musicName = musicList [ randomKey ] |
55 | Event:FireAllClients(musicName) |
A good rule of thumb is don't embed event-driven functions within one another. If you need several functions to use the same variables, then increase the scope of the variables to the highest scope. Be careful when doing this though, you'll need to account for the different values of the variable in the different functions.
I tested it as well in Studio for single and multiple players with the skip button, without the skip button, letting the song finish, and combinations of the three.