Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. import { Workspace } from "@rbxts/services"
  2. import { LoadSongs, SongList } from "./InitSongs";
  3.  
  4.  
  5. let SongPlaying = new Instance("StringValue")
  6. SongPlaying.Name = "SongPlaying";
  7. SongPlaying.Value = "Nothing";
  8. SongPlaying.Parent = Workspace
  9. let AudioObject = new Instance("Sound");
  10. AudioObject.Name = "MusicSystem"
  11. AudioObject.Parent = Workspace
  12.  
  13. let RequestedMusic = new Array<number>();
  14.  
  15. let IsRunning = false;
  16.  
  17.  
  18. function ToSoundId(AudioId : Number) : string
  19. {
  20. return `rbxassetid://${AudioId}`;
  21. }
  22. let CurrentSong : number;
  23. function PickNewSong()
  24. {
  25. print("Stop Detected!");
  26. CurrentSong = math.random(0, SongList.size()-1)
  27. if(RequestedMusic.size() > 0)
  28. {
  29. AudioObject.SoundId = ToSoundId(RequestedMusic[0])
  30. RequestedMusic.pop();
  31. }
  32. else
  33. {
  34. AudioObject.SoundId = ToSoundId(SongList[CurrentSong].AudioId);
  35. }
  36. print("Playing new song!")
  37. AudioObject.Play();
  38. }
  39.  
  40. function RunMusic()
  41. {
  42. if(IsRunning) return;
  43. IsRunning = true;
  44. LoadSongs();
  45. CurrentSong = math.random(0, SongList.size()-1);
  46. AudioObject.Played.Connect(()=>{
  47. SongPlaying.Value = SongList[CurrentSong].SongName;
  48. let Counter = 0;
  49. while(AudioObject.TimeLength === 0 || Counter < 100) { wait(); print("Waiting for TimeLength not to = 0"); Counter++; }
  50. wait(AudioObject.TimeLength)
  51. AudioObject.Stop();
  52. PickNewSong();
  53. })
  54.  
  55. AudioObject.SoundId = ToSoundId(SongList[CurrentSong].AudioId);
  56. AudioObject.Play();
  57. AudioObject.Parent = Workspace;
  58.  
  59. }
  60.  
  61. let IsRequestInProgress = false;
  62. function RequestMusic(plr : Player, MusicId : number)
  63. {
  64. if(IsRequestInProgress) { return false }
  65. let DataFolder = plr.FindFirstChild<Folder>("DataFolder");
  66. if(DataFolder !== undefined)
  67. {
  68. let MusicCredits = DataFolder.FindFirstChild<NumberValue>("MusicCredits");
  69. if(MusicCredits !== undefined)
  70. {
  71. if(MusicCredits.Value > 0)
  72. {
  73. MusicCredits.Value = MusicCredits.Value - 1;
  74. RequestedMusic.push(MusicId);
  75. }
  76. }
  77. }
  78. }
  79.  
  80. export {RunMusic}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement