What do you want to achieve? Keep it simple and clear!
Currently, I’m trying to make an audio player play background music with the song title in the bottom right hand side of the screen. I am able to get the GUI part of the script to work properly, along with the audio player itself as of now.
2. What is the issue? Include screenshots / videos if possible!
If I wish to add future songs, I would need to repeat the current block of code each time, which can leave a script very messy.
local Sound1 = Instance.new("Sound", game.Workspace)
Sound1.Volume = 0.5
Sound1.Looped = false
Sound1.SoundId = "http://www.roblox.com/asset?id=2153548494"
Sound1.PlaybackSpeed = 3
Sound1:Play()
script.Parent.Text = 'National Anthem of Anglia - LouisLohenzollern'
fadeIn()
Sound1.Ended:Wait()
Sound1:Destroy()
fadeOut()
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I’ve tried using local values to try and simplify the process, but I cannot get it right. I simply cannot grasp the idea of setting a value as an asset ID.
I would use a library with dictionaries inside (see below) and a module script.
-- local script
local soundModule = require(moduleScript)
local sound = {
S1 = {
Instance = Instance.new("Sound", parent)
Txt = "Song name"
volume = 0.5,
looped = false,
id = "xyz"
speed = 3
}
-- etc.
}
while True do -- Can replace with heartbeat
soundModule.Play(sound.S1)
-- etc
end
-- Module script
local sound = {}
function sound.Play(soundNo)
soundNo.Instance.Volume = soundNo.volume
-- Do the same for other variables
soundNo.Instance:Play()
GUI.Text = soundNo.Txt
end
return sound
I am on mobile thus my code is not tested and may have Syntax errors and be formatted badly
Let’s begin. Create a table with properties, function that can benefit the creation of each sound instance, setting all functions together and finally running the loop.
local sounds = {}
local soundIds = {
-- your sound IDs here
}
local function getSoundSettings()
-- Generates default sound settings.
return {Volume = 0.5, Looped = false, PlaybackSpeed = 3} -- woah prepared settings!
end
local function new(instance, properties)
-- Useful function for creating instances with properties smoothly using a table.
local obj = Instance.new(instance)
for property, value in next, properties do
if obj[property] then
obj[property] = value
end
end
return obj
end
local function combineDictionary(dict, otherDict)
-- Combines two dictionaries into one.
for key, value in next, otherDict do
dict[key] = value
end
return dict
end
-- Initialize
for index, id in next, soundIds do
sounds[index] = new("Sound", combineDictionary(getSoundSettings(), {SoundId = id})
sounds[index] = workspace
end
-- Loop
while true do
for _, sound in next, sounds do
-- fade in
sound:Play()
sound.Ended:Wait()
-- sound:Destroy() -- big woops
-- fade out
end
end
Don’t forget to handle potential Parent keys in the properties table of any instancer intended to extend the functionality of vanilla Instance.new. I wrote a script that does this, though it’s designed to work as a ModuleScript. The code is salvageable however.