Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Random music not working? [ANSWERED]

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.

3 answers

✓ Accepted answer

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.

4 comments

0

Thanks for trying to help, however it still doesn't work. On the line loserandommusic=loserandommusic:Clone().Parent = game.Workspace , it errors and tells me that there is a unexpected symbol near '='. This is because there is two ='s on the same line.

0

Yea, I know, I updated it. It should work now. Thx for letting me know though.

1

Thanks! Works perfectly. Liked and answered.

0

Thanks, glad I could help! :P

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?

2 comments

0

Yes, I am sure the sound has a id.

0

Did you call the function somewhere in the script? cause it looks like you just created it.

This discussion is preserved as it was. New answers, comments, and votes are closed.