Hello scripting helpers.
I just recently made a script what plays random music.
Here is the script:-
function lose() -- Creates function local loseservice = game.ServerStorage.LoseMusic:GetChildren() -- Gets the music local loserandommusic = loseservice[math.random(1, #loseservice)] -- Gets random music loserandommusic:Clone().Parent = game.Workspace -- Clones the random music into workspace loserandommusic:Play() -- Plays the random music wait(7) loserandommusic:Stop() -- Stops the random music loserandommusic:Destroy() -- Destroys the random music end
The script successfully clones the random music into the workspace to play, however the music doesn't play, stop or destroy. There is also no errors in the console.
Does anyone know how to fix this?
Thanks, Nathan.
The only problem was that you didn't assign a variable to the cloned sound. Try this:
function lose() -- Creates function local loseservice = game.ServerStorage.LoseMusic:GetChildren() -- Gets the music local loserandommusic = loseservice[math.random(1, #loseservice)] -- Gets random music loserandommusic=loserandommusic:Clone() --Re-assigned the variable "loserandommusic" to the clone. loserrandimmusic.Parent = game.Workspace loserandommusic:Play() -- Plays the random music wait(7) loserandommusic:Stop() -- Stops the random music loserandommusic:Destroy() -- Destroys the random music end
I think that this should probably work. If it doesn't let me know in the comments. Hope I helped :P Sorry, I realized the problem in my first post. I updated it, and it should work now.
Assign a variable to reference the clone, or else the merciless garbage collector will eat your music!
local function lose() local music = game.ServerStorage.LoseMusic:GetChildren() --I changed some variable names so that the code looks cleaner local randommusic = music[math.random(1,#music)] local clone = randommusic:Clone() --Make sure that if you're using an independent function, your variables are local, or your code might result in error in certain situations. clone.Parent = workspace clone:Play() wait(7) clone:Stop() clone:Destroy() end
Are you sure the music has a soundid?