Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Workspace } from "@rbxts/services"
- import { LoadSongs, SongList } from "./InitSongs";
- let SongPlaying = new Instance("StringValue")
- SongPlaying.Name = "SongPlaying";
- SongPlaying.Value = "Nothing";
- SongPlaying.Parent = Workspace
- let AudioObject = new Instance("Sound");
- AudioObject.Name = "MusicSystem"
- AudioObject.Parent = Workspace
- let RequestedMusic = new Array<number>();
- let IsRunning = false;
- function ToSoundId(AudioId : Number) : string
- {
- return `rbxassetid://${AudioId}`;
- }
- let CurrentSong : number;
- function PickNewSong()
- {
- print("Stop Detected!");
- CurrentSong = math.random(0, SongList.size()-1)
- if(RequestedMusic.size() > 0)
- {
- AudioObject.SoundId = ToSoundId(RequestedMusic[0])
- RequestedMusic.pop();
- }
- else
- {
- AudioObject.SoundId = ToSoundId(SongList[CurrentSong].AudioId);
- }
- print("Playing new song!")
- AudioObject.Play();
- }
- function RunMusic()
- {
- if(IsRunning) return;
- IsRunning = true;
- LoadSongs();
- CurrentSong = math.random(0, SongList.size()-1);
- AudioObject.Played.Connect(()=>{
- SongPlaying.Value = SongList[CurrentSong].SongName;
- let Counter = 0;
- while(AudioObject.TimeLength === 0 || Counter < 100) { wait(); print("Waiting for TimeLength not to = 0"); Counter++; }
- wait(AudioObject.TimeLength)
- AudioObject.Stop();
- PickNewSong();
- })
- AudioObject.SoundId = ToSoundId(SongList[CurrentSong].AudioId);
- AudioObject.Play();
- AudioObject.Parent = Workspace;
- }
- let IsRequestInProgress = false;
- function RequestMusic(plr : Player, MusicId : number)
- {
- if(IsRequestInProgress) { return false }
- let DataFolder = plr.FindFirstChild<Folder>("DataFolder");
- if(DataFolder !== undefined)
- {
- let MusicCredits = DataFolder.FindFirstChild<NumberValue>("MusicCredits");
- if(MusicCredits !== undefined)
- {
- if(MusicCredits.Value > 0)
- {
- MusicCredits.Value = MusicCredits.Value - 1;
- RequestedMusic.push(MusicId);
- }
- }
- }
- }
- export {RunMusic}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement