Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Random music not working? [ANSWERED]

Asked by 9 years ago

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

Log in to vote
1
Answered by
dyler3 1510 Moderation Voter
9 years ago

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.

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. WelpNathan 307 — 9y
0
Yea, I know, I updated it. It should work now. Thx for letting me know though. dyler3 1510 — 9y
1
Thanks! Works perfectly. Liked and answered. WelpNathan 307 — 9y
0
Thanks, glad I could help! :P dyler3 1510 — 9y
Ad
Log in to vote
1
Answered by 9 years ago

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
Log in to vote
-1
Answered by 9 years ago

Are you sure the music has a soundid?

0
Yes, I am sure the sound has a id. WelpNathan 307 — 9y
0
Did you call the function somewhere in the script? cause it looks like you just created it. matt1020304050 0 — 9y

Answer this question