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

Error on song parent being locked?

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.

1 comment

0

The error is specifically "The Parent property of Song10 is locked, current parent: NULL, new parent Workspace" btw

1 answer

✓ Accepted answer

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.

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