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

Error on song parent being locked?

Asked by 7 years ago
local Song1 = game.ReplicatedStorage.BackgroundMusic.Song1:Clone()
local Song2 = game.ReplicatedStorage.BackgroundMusic.Song2:Clone()
local Song3 = game.ReplicatedStorage.BackgroundMusic.Song3:Clone()
local Song4 = game.ReplicatedStorage.BackgroundMusic.Song4:Clone()
local Song5 = game.ReplicatedStorage.BackgroundMusic.Song5:Clone()
local Song6 = game.ReplicatedStorage.BackgroundMusic.Song6:Clone()
local Song7 = game.ReplicatedStorage.BackgroundMusic.Song7:Clone()
local Song8 = game.ReplicatedStorage.BackgroundMusic.Song8:Clone()
local Song9 = game.ReplicatedStorage.BackgroundMusic.Song9:Clone()
local Song10 = game.ReplicatedStorage.BackgroundMusic.Song10:Clone()
local Song11 = game.ReplicatedStorage.BackgroundMusic.Song11:Clone()
local Music = script.MusicisPlaying
local SongNumber = script.Number

while true do 
    if Music.Value == false then
        SongNumber.Value = math.random(1,11)
        Music.Value = true
        if SongNumber.Value == 1 then
            local Song = Song1
            Song.Parent = game.Workspace
            Song.Playing = true
            wait(Song.TimeLength)
            Song:Destroy()
        elseif SongNumber.Value == 2 then
            local Song = Song2
            Song.Parent = game.Workspace
            Song.Playing = true
            wait(Song.TimeLength)
            Song:Destroy()
        elseif SongNumber.Value == 3 then
            local Song = Song3
            Song.Parent = game.Workspace
            Song.Playing = true
            wait(Song.TimeLength)
            Song:Destroy()
        elseif SongNumber.Value == 4 then
            local Song = Song4
            Song.Parent = game.Workspace
            Song.Playing = true
            wait(Song.TimeLength)
            Song:Destroy()
        elseif SongNumber.Value == 5 then
            local Song = Song5
            Song.Parent = game.Workspace
            Song.Playing = true
            wait(Song.TimeLength)
            Song:Destroy()
        elseif SongNumber.Value == 6 then
            local Song = Song6
            Song.Parent = game.Workspace
            Song.Playing = true
            wait(Song.TimeLength)
            Song:Destroy()
        elseif SongNumber.Value == 7 then
            local Song = Song7
            Song.Parent = game.Workspace
            Song.Playing = true
            wait(Song.TimeLength)
            Song:Destroy()
        elseif SongNumber.Value == 8 then
            local Song = Song8
            Song.Parent = game.Workspace
            Song.Playing = true
            wait(Song.TimeLength)
            Song:Destroy()
        elseif SongNumber.Value == 9 then
            local Song = Song9
            Song.Parent = game.Workspace
            Song.Playing = true
            wait(Song.TimeLength)
            Song:Destroy()
        elseif SongNumber.Value == 10 then
            local Song = Song10
            Song.Parent = game.Workspace
            Song.Playing = true
            wait(Song.TimeLength)
            Song:Destroy()
        elseif SongNumber.Value == 11 then
            local Song = Song11
            Song.Parent = game.Workspace
            Song.Playing = true
            wait(Song.TimeLength)
            Song:Destroy()
        end
        Music.Value = false
    end
    wait(0.1)
end

The code appears to be the same for every song and the properties are also the same, but whenever it gets to song 10, it has an error. The other songs work fine until then.

0
The error is specifically "The Parent property of Song10 is locked, current parent: NULL, new parent Workspace" btw ZoltofLightning 27 — 7y

1 answer

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

You play the song then you destroy it, why not just stop it?

There is also a tone of duplicate code here and you cannot add new songs easily, this also needs to be looked at.

As you are making a list of songs 1- ect then we should use a table which holds the songs, you also move the songs which is not needed when they are played.

The final code:-

local backgroundMusic = game:GetService('ReplicatedStorage'):WaitForChild('BackgroundMusic')
local numOfSongs = 11
local songList = {}
local Music = script.MusicisPlaying
local SongNumber = script.Number

for i=1, numOfSongs do -- adds our songs to a nice list and adds the song to the workspace
    local song = backgroundMusic:WaitForChild('Song' .. tostring(i)):Clone()
    song.Parent = game.Workspace
    table.insert(songList, song)
end

while true do
    if Music.Value == false then
        Music.Value = true
        local ranSong = math.random(1,#songList)
        SongNumber.Value = ranSong 
        songList [ranSong]:Play() -- plays the song
        songList [ranSong].Ended:wait() -- waits until the ended event fires on the song 
        -- we do not destroy the song 
        Music.Value = false
    end
end

Hope this helps, please commend if you do not understand how or why this code works.

Ad

Answer this question