So I wanted to create a "sound list" system and I came up with having a ModuleScript return a table with all the sounds the player has entered, BUT the player can add or remove sounds from the list at any time. But then this question hit me: Can I actually do this with a ModuleScript? Can I actually change the ModuleScript's returning value once the return command is executed?
Changing what a ModuleScript returns?
1 answer
It's not very complicated or sophisticated to do this.
I assume you would want a song request module, for the client only, not the whole server.
To do this, you would create a GUI with a TextBox and 2 TextButtons, a LocalScript inside of each button, and a ModuleScript in the GUI.
I'd setup the ModuleScript like this.
local mainModule = {}
mainModule.SoundTable = {} --// This is where the sounds will be added and removed.
return mainModule
The mainModule.SoundTable is basically the root of how all this will be controlled.
You can shorten this by using a function inside of the module, but either way works. I'll be doing it by the way of adding and removing it through the LocalScripts.
Inside of the AddFunction LocalScript I'd write this.
local soundModule = require(script.Parent.Parent:WaitForChild("SoundModule"))
script.Parent.MouseButton1Click:Connect(function()
local songName = script.Parent.Parent.SongNameBox
if table.find(soundModule.SoundTable, songName.Text) then --// Check if song is already in the list
warn(songName.Text.." Is Already In The Queue!")
songName.Text = ""
else
table.insert(soundModule.SoundTable,songName.Text) --// Adds the song to the list
print("Successfully Added "..songName.Text.." To The Queue!")
songName.Text = ""
end
end)
This will check if the song is already in the list, if it's not then it will add the song into the queue.
Inside of the DeleteFunction LocalScript I'd write this.
local soundModule = require(script.Parent.Parent:WaitForChild("SoundModule"))
script.Parent.MouseButton1Click:Connect(function()
if table.find(soundModule.SoundTable, script.Parent.Parent.SongNameBox.Text) then
local songNamePos = table.find(soundModule.SoundTable, script.Parent.Parent.SongNameBox.Text)
wait()
table.remove(soundModule.SoundTable,songNamePos)
print("Successfully Removed "..script.Parent.Parent.SongNameBox.Text.." From The Queue!")
script.Parent.Parent.SongNameBox.Text = ""
else
warn(script.Parent.Parent.SongNameBox.Text.." | That Song Does Not Exist In The Queue!")
script.Parent.Parent.SongNameBox.Text = ""
end
end)
This will check if the song is in the list, if it's not then it will warn them and tell them the song was not removed as it doesn't exist in the list.
Hope this helped! You can also go farther with this by using a LocalScript and add a sound into the player gui everytime one song ends, remove it from the list and add another song and start playing that one.
But, if this helped don't forget to select this as an answer!
2 comments
can other scripts get the changed table when it requires that modulescript?
Yes, it will only change for the client, as that is located inside of their player gui. So, you can play client sided custom music.
This discussion is preserved as it was. New answers, comments, and votes are closed.

3 comments
A module script has is returned value cached after which the cache is retuned. You should not have an issue if you return a table. The table will then be shared.
So if I change something inside a table, that changed table will be returned?
Yes, it will.