Need help with my code /simple

How can i connect this script to all parts/children of a folder called “Music”?
(I wanna make it work on multiple parts with the same name)
Here is the script:

local music = game.Workspace.Music.MusicBlock

script.Parent.MouseButton1Click:Connect(function()
music.Sound.SoundId = “rbxassetid://17721474407”
music.Sound.Playing = true
end)

Here’s some code that should work! All it does is that it loops through the “Music” folder, then checks if it’s a part, and it has a certain name. Then, you can do whatever you want after that!

local MusicFolder = game.Workspace:WaitForChild("Music") -- change this to the location of your "Music" folder

for i, Part in pairs(MusicFolder:GetChildren()) do
	if Part:IsA("Part") and Part.Name == "Part" then -- Change "Part" to the name of the parts
		local music = game.Workspace.Music.MusicBlock
		
		script.Parent.MouseButton1Click:Connect(function()
			music.Sound.SoundId = "rbxassetid://17721474407"
			music.Sound.Playing = true
		end)
	end
end
1 Like

Wait it only works for 3 of my parts/musicblocks

Do they all have different names, or do all of the parts/musicblocks have the same name?

Same everything, Same name yes

I just realized that it was still only 1 that worked but a different one so i thought it was all of them

if this helps

Try this instead. What I changed is that it uses every individual MusicPart, because it seemed that you were only playing it on one part:

local MusicFolder = game.Workspace:WaitForChild("Music")

for i, Part in pairs(MusicFolder:GetChildren()) do
	if Part:IsA("Part") and Part.Name == "MusicBlock" then
		script.Parent.MouseButton1Click:Connect(function()
			Part.Sound.SoundId = "rbxassetid://17721474407"
			Part.Sound.Playing = true
		end)
	end
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.