Sound Region System Issue with Multiple Parts

Hey developers!

  1. What do you want to achieve? So I’m making a sound and lighting region system which works (but with 1 part and that’s it). I want to be able to make it work with multiple parts of the same name.

  2. What is the issue? My problem is that I need to use multiple parts to make complex shaped regions and when I use more than one part with the same name it doesn’t work (effects from the the corresponding sounds and lighting folder will not apply).

  3. What solutions have you tried so far? I’ve looked on the forum and Developer Hub, I even tried renaming the parts so they have separate folders with the effects (even though they will be the same effects) and it didn’t work.

So I have my sound and lighting organized in folders in the workspace
image
And the script is located under StarterPlayerScripts

The red parts in the video are both named the same (“Refresh”) and the sounds and lighting doesn’t apply when your in the part. But, in the part named “Cave” (which there is only one of) works – sound and lighting applies.


local ZoneRegions = game.Workspace:WaitForChild("LightingAndSoundChanging"):WaitForChild("Zones")

local LightingData = game.Workspace:WaitForChild("LightingAndSoundChanging"):WaitForChild("Lighting")
local SoundData = game.Workspace:WaitForChild("LightingAndSoundChanging"):WaitForChild("Sound")

local Found = false

while task.wait(1) do

	for i, v in pairs (ZoneRegions:GetChildren()) do
		Found = false
		
		local region = Region3.new(v.Position - (v.Size/2),v.Position + (v.Size/2))
		local parts = game.Workspace:FindPartsInRegion3WithWhiteList(region, game.Players.LocalPlayer.Character:GetDescendants())
		
		for _, part in pairs(parts) do
			if part:FindFirstAncestor(game.Players.LocalPlayer.Name) then
				Found = true
				break
			else
				Found = false
			end
		end

		if Found then
			if LightingData:FindFirstChild(v.Name) then
				for i,lighting in pairs(LightingData:FindFirstChild(v.Name):GetChildren()) do
					if lighting:IsA("StringValue") or lighting:IsA("Color3Value") or lighting:IsA("BoolValue") then
						game.Lighting[lighting.Name] = lighting.Value
					elseif lighting:IsA("ColorCorrectionEffect") then
						game.Lighting.ColorCorrection.Saturation = LightingData[v.Name].ColorCorrection.Saturation
						game.Lighting.ColorCorrection.Contrast = LightingData[v.Name].ColorCorrection.Contrast
					elseif lighting:IsA("Atmosphere") then
						game.Lighting.Atmosphere.Density = LightingData[v.Name].Atmosphere.Density
						game.Lighting.Atmosphere.Color = LightingData[v.Name].Atmosphere.Color
						game.Lighting.Atmosphere.Decay = LightingData[v.Name].Atmosphere.Decay
						game.Lighting.Atmosphere.Glare = LightingData[v.Name].Atmosphere.Glare
						game.Lighting.Atmosphere.Haze = LightingData[v.Name].Atmosphere.Haze
					end
				end
			end
			if SoundData:FindFirstChild(v.Name) then
				for i, sound in pairs(SoundData:FindFirstChild(v.Name):GetChildren()) do
					game:GetService("TweenService"):Create(sound,TweenInfo.new(1),{Volume = sound.OriginalVolume.Value}):Play()
				end
			end
		else
			if SoundData:FindFirstChild(v.Name) then
				for i, sound in pairs(SoundData:FindFirstChild(v.Name):GetChildren()) do
					game:GetService("TweenService"):Create(sound,TweenInfo.new(1),{Volume = 0}):Play()
				end
			end
		end
	end
end

I think its something wrong with this part but that’s just an assumption (there could be something else causing it?):

        else
			if SoundData:FindFirstChild(v.Name) then
				for i, sound in pairs(SoundData:FindFirstChild(v.Name):GetChildren()) do
					game:GetService("TweenService"):Create(sound,TweenInfo.new(1),{Volume = 0}):Play()
				end
			end
		end

If you know how to fix this to work with multiple parts of the same name, please inform me.
Thanks in advance!

You would have to store the parts by using a unique property such as Position, rather than the name. Alternatively you could access the LightingData Object through a new table instead of the name.