Skip code but don't break loop

Hello, I have this code that its supposed to check if a sound its playing and if so clone and send a part to its position, however not only does it not iterate thru the entire table at the same time I also don’t know how to make it not stop the loop

while task.wait() do
			for _, sound in soundList do
				for _, part in Folder:GetChildren() do
					if part.Name == sound.Name then
						return
					elseif part.Name ~= sound.Name then
						part:Destroy()
					end
				end
				
				local clone = soundIndicator:Clone()
				clone.Parent = Folder
				clone.Name = sound.Name
				clone.Position = sound.Parent.Position
				
			end
		end

Here’s the entire code for context:

--// GameServices
local CollectionService = game:GetService("CollectionService")
local Players = game:GetService("Players")
local Debris = game:GetService("Debris")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//
	
local soundScanner = task.spawn(function()
	--// Paths
	local Mechanics = ReplicatedStorage:WaitForChild('Mechanics')
	local soundIndicator = Mechanics:WaitForChild('SoundBubble')
	local Folder = workspace:WaitForChild('SoundIndicators')
	--//
	
	--// Values
	local soundList = {}
	
	--//

	task.spawn(function()
		while task.wait() do
			for _, sound in workspace:GetDescendants() do
				if sound:IsA('Sound') then
					if sound.Playing == true then
						if not table.find(soundList, sound) then
							table.insert(soundList, sound)
						end
					end
				end
			end
		end
	end)

	task.spawn(function()
		while task.wait() do
			for _, sound in workspace:GetDescendants() do
				if sound:IsA('Sound') then
					if sound.Playing == false then
						if table.find(soundList, sound) then
							table.remove(soundList, table.find(soundList, sound))
						end
					end
				end
			end
		end
	end)
	
	task.spawn(function()
		while task.wait() do
			for _, sound in soundList do
				for _, part in Folder:GetChildren() do
					if part.Name == sound.Name then
						return
					elseif part.Name ~= sound.Name then
						part:Destroy()
					end
				end
				
				local clone = soundIndicator:Clone()
				clone.Parent = Folder
				clone.Name = sound.Name
				clone.Position = sound.Parent.Position
				
			end
		end
	end)
	
	task.spawn(function()
		while task.wait(1) do
			print(soundList)
		end
	end)


end)

--//

Regarding this little bit:

if part.Name == sound.Name then
						return
					elseif part.Name ~= sound.Name then
						part:Destroy()
					end

instead of using

return

try using

break

Depending on what you want to achieve with the code, you could also just remove the if statement and use part:Destroy() on all parts in the folder

3 Likes

I was able to use ChatGPT to fix my code, and then change it to my own needs.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
--//

--// Paths
local Mechanics = ReplicatedStorage:WaitForChild('Mechanics')
local soundIndicator = Mechanics:WaitForChild('SoundBubble')
local Folder = workspace:WaitForChild('SoundIndicators')
--//


-- Function to check if a part with the same name already exists in the same position
local function partExistsInPosition(partName, position)
	for _, existingPart in ipairs(workspace:GetChildren()) do
		if existingPart.Name == partName and existingPart:IsA("BasePart") and existingPart.Position == position then
			return true
		end
	end
	return false
end

-- Function to create or modify a part for a given sound if it's playing with a volume higher than 0
local function createPartForSound(sound)
	if sound.Volume > 0 then
		local partName = sound.Name
		local soundPosition = sound.Parent.Position
		local existingPart = Folder:FindFirstChild(partName)
		
		if existingPart then
			existingPart.Position = soundPosition
		else
			local clone = soundIndicator:Clone()
			clone.Name = partName
			clone.Position = soundPosition
			clone.Parent = Folder
		end
	end
end

-- Loop to constantly check if there are sounds playing in the workspace and create parts for them
while task.wait() do
	for _, child in ipairs(workspace:GetDescendants()) do
		if child:IsA("Sound") then
			if child.Playing then
				createPartForSound(child)
			else
				local part = Folder:FindFirstChild(child.Name)
				if part and part:IsA("BasePart") then
					part:Destroy()
				end
			end
		end
	end
end

here’s what I ended with.

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