I come in here seeking the help of programmers.
So, I have the following script to randomise and play audio:
local SoundService = game:GetService("SoundService")
local soundGroup = Instance.new("SoundGroup", SoundService)
soundGroup.Name = "Music"
local SONG_IDS = {
1837879082,
1837879143,
1837879303,
1837879370
}
local songFolder = workspace:FindFirstChild("Songs")
if not songsFolder then
songsFolder = Instance.new("Folder")
songsFolder.Name = "Songs"
songsFolder.Parent = workspace
end
local songs = nil do
songs = {}
for _, songId in SONG_IDS do -- this would create an instance for each SONG_ID
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://" .. songId
sound.Name = "Song_" .. songId
sound.Parent = songsFolder
sound.SoundGroup = soundGroup
end
for _, child in songsFolder:GetChildren() do
if not child:IsA("Sound") then
continue
end
table.insert(songs, {sound = child})
end
end
local function shuffle(tbl)
for i = #tbl, 2, -1 do
local j = math.random(i)
tbl[i], tbl[j] = tbl[j], tbl[i]
end
return tbl
end
local NUM_SONGS = #songs
while true do
local order = {}
for i = 1, NUM_SONGS do
order[i] = i
end
shuffle(order)
for i = 1, NUM_SONGS do
local song = songs[i]
song.sound:Play()
song.sound.Ended:Wait()
end
end
Basically, it creates a Sound
Instance for each SoundID present, then it should add them to the SoundGroup
I created.
I’ve created a Play/Pause button in order to be able to control the sounds:
local TweenService = game:GetService("TweenService")
local SoundService = game:GetService("SoundService")
local audio = game.Workspace:FindFirstChildOfClass("Sound")
local audioVolumeControl = game.SoundService:FindFirstChildOfClass("SoundGroup")
local playPauseButton = script.Parent
local cooldown = false
playPauseButton.Image = "rbxassetid://166377450"
-- TweenInfos
local buttonTweenInfo = TweenInfo.new(0.15, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false)
local audioTweenInfo = TweenInfo.new(0.8, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false)
-- Button Tweens
local buttonPopOut = TweenService:Create(script.Parent, buttonTweenInfo, {Size = UDim2.new(0, 50, 0, 50), Rotation = 10})
local buttonPopIn = TweenService:Create(script.Parent, buttonTweenInfo, {Size = UDim2.new(0, 45, 0, 45), Rotation = 0})
-- Audio Tweens
local pausingSound = TweenService:Create(audioVolumeControl, audioTweenInfo, {Volume = 0})
local resumingSound = TweenService:Create(audioVolumeControl, audioTweenInfo, {Volume = 0.5})
local function onRelease()
print("Button released")
buttonPopOut:Play()
if cooldown == true then return end
if (audio) then
if audio.IsPlaying == true then
print("Audio paused")
script.Parent.Image = "rbxassetid://11104029889"
cooldown = true
pausingSound:Play()
task.delay(0.8, function()
audio:Pause()
end)
cooldown = false
elseif audio.IsPaused == true then
print("Audio resumed")
script.Parent.Image = "rbxassetid://11104029772"
cooldown = true
audio:Resume()
resumingSound:Play()
task.wait(0.8)
cooldown = false
end
end
end
playPauseButton.Activated:Connect(onRelease)
When the button is pressed, it should pause or resume the audio. However, the onRelease
function does not respond past the print
statement.
The console log after I press the button:
Additionally, the Image
on said button has a Transparency
of 1
, but does not happen when the script is disabled (It is the same script above. Assuming there is something that sets the transparency to 1 until activated). I’ve tried another script for the sound before, however the Image
would only appear after I actually activate the button.
(this is where the button is supposed to be)
I’ve never really asked for help here so I’m not sure if I’ve provided enough info, but thanks anyway.