How would I make this script work for multiple parts

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    make the script work for all parts in range

  2. What is the issue? Include screenshots / videos if possible!
    the script only works for 1 part within range

  3. What solutions have you tried so far? Did you look for solutions on the Creator Hub?
    ive tried putting them into a table but Im not really good at scripting at the api docs dont really help.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

localscript in startercharacterscripts

local soundStuff = game.Workspace.soundStuff:GetDescendants() -- folder with parts
local localPlayer = game.Players.LocalPlayer
local hum = localPlayer.Character:WaitForChild("HumanoidRootPart")
local debounce = false

while task.wait() do

for i, sound in pairs(soundStuff) do
	if sound.Name == "HearableSound" then	
		local soundpart = sound.Parent
		local mag = (hum.Position - soundpart.Position).Magnitude

		if mag < 20 then -- if in range then, add outline and sign
		if debounce == false then
		local highlight = Instance.new("Highlight")
		highlight.Adornee = soundpart
		highlight.Parent = soundpart
		highlight.FillTransparency = 1
		highlight.OutlineColor = Color3.new(255,0,0)
		highlight.OutlineTransparency = 0
		
		local Gui = game.ReplicatedStorage:WaitForChild("DisplayGui"):Clone()
		Gui.Parent = soundpart
		Gui.Image.ImageTransparency = 0
		debounce = true
		
		end
		else --if out of range
				if soundpart:FindFirstChild("Highlight") then
				repeat
					task.wait(0.1)
					soundpart.Highlight.OutlineTransparency += 0.1
					soundpart.DisplayGui.Image.ImageTransparency += 0.1
				until soundpart.Highlight.OutlineTransparency >= 1
				-- fade then delete
				soundpart.DisplayGui:Destroy()
				soundpart.Highlight:Destroy()
				
					debounce = false

			end
		end
		end
	end
end


Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

EDIT: fixed main problem but now a new issue in post 8

You need to loop through the players, and the parts and the gui’s

local soundStuff = game.Workspace.soundStuff:GetDescendants() 
local localPlayer = game.Players.LocalPlayer
local hum = localPlayer.Character:WaitForChild("HumanoidRootPart")
local debounce = false
local Players = game:GetService("Players")
local plrsonMap = Players:GetChildren()

while true do
	for i, v in pairs(plrsonMap) do
		print(v)
		for n, m in pairs(soundStuff) do
			print(m)
			if m.Name == "HearableSound" then
				print(m)
				local mag = (hum.Position - m.Position).Magnitude
				print(mag)
				if mag < 20 then
					local highlight = Instance.new("Highlight")
					highlight.Adornee = m
					highlight.Parent = m
					highlight.FillTransparency = 1
					highlight.OutlineColor = Color3.new(255,0,0)
					highlight.OutlineTransparency = 0

					local Guis = game.ReplicatedStorage:GetChildren()
					for j, k in pairs(Guis) do					
						if k.Adornee == m and k.Name == "DisplayGui" then
							local cloneGUI = k:Clone()
							cloneGUI.Parent = m
							cloneGUI.Image.ImageTransparency = 0
						end
					end														
				else 
					if m:FindFirstChild("Highlight") then
						repeat
							task.wait(0.1)
							m.Highlight.OutlineTransparency += 0.1
							m.DisplayGui.Image.ImageTransparency += 0.1
						until 
							m.Highlight.OutlineTransparency >= 1						
							m.DisplayGui:Destroy()
							m.Highlight:Destroy()
							debounce = false
						end				
					end	
				end
							
			end								
	end
	task.wait(1)
end

The problem here is the debounce. You are not setting the debounce back to false after the first iteration of the loop,so the highlighting code only ever runs once.

Well you don’t need to iterate through players actually

local soundStuff = game.Workspace.soundStuff:GetDescendants() 
local localPlayer = game.Players.LocalPlayer
local hum = localPlayer.Character:WaitForChild("HumanoidRootPart")


while true do
		for n, m in pairs(soundStuff) do
			print(m)
			if m.Name == "HearableSound" then
				print(m)
				local mag = (hum.Position - m.Position).Magnitude
				print(mag)
				if mag < 20 then
					local highlight = Instance.new("Highlight")
					highlight.Adornee = m
					highlight.Parent = m
					highlight.FillTransparency = 1
					highlight.OutlineColor = Color3.new(255,0,0)
					highlight.OutlineTransparency = 0

					local Guis = game.ReplicatedStorage:GetChildren()
					for j, k in pairs(Guis) do					
						if k.Adornee == m and k.Name == "DisplayGui" then
							local cloneGUI = k:Clone()
							cloneGUI.Parent = m
							cloneGUI.Image.ImageTransparency = 0
						end
					end														
				else 
					if m:FindFirstChild("Highlight") then
						repeat
							task.wait(0.1)
							m.Highlight.OutlineTransparency += 0.1
							m.DisplayGui.Image.ImageTransparency += 0.1
						until 
						m.Highlight.OutlineTransparency >= 1						
						m.DisplayGui:Destroy()
						m.Highlight:Destroy()
					end				
				end	
			end

		end								
	task.wait(1)
end

Like what @loltrol2121 said You set denounce to true in your first if statement. So it will never run if statement, after your done all your logic in the first if statement add in the denounce to make become false by doing

Debounce = false
1 Like

local soundStuff = game.Workspace.soundStuff:GetDescendants() -- folder with parts
local localPlayer = game.Players.LocalPlayer
local hum = localPlayer.Character:WaitForChild("HumanoidRootPart")
local debounce = false

local function loop(sound)
	local isFading = false
	while task.wait() do
		if sound.Name == "HearableSound" then	
			local soundpart = sound.Parent
			local mag = (hum.Position - soundpart.Position).Magnitude

			if mag < 20 then -- if in range then, add outline and sign
				isFading = false
				local highlight = soundpart:FindFirstChild("Highlight")
				local Gui = soundpart:FindFirstChild("DisplayGui")
				
				if not highlight then
					highlight = Instance.new("Highlight")
					highlight.Adornee = soundpart
					highlight.Parent = soundpart
					highlight.FillTransparency = 1
					highlight.OutlineColor = Color3.new(255,0,0)
				end
				
				highlight.OutlineTransparency = 0
				
				if not Gui then
					Gui = game.ReplicatedStorage:WaitForChild("DisplayGui"):Clone()
					Gui.Parent = soundpart
				end
				
				Gui.Image.ImageTransparency = 0
				
			else --if out of range
				if soundpart:FindFirstChild("Highlight") then
					if not isFading then
						isFading = true
						task.spawn(function()
							repeat
								task.wait(0.1)
								if not isFading then return end
								soundpart.Highlight.OutlineTransparency += 0.1
								soundpart.DisplayGui.Image.ImageTransparency += 0.1
							until soundpart.Highlight.OutlineTransparency >= 1 
							-- fade then delete
							soundpart.DisplayGui:Destroy()
							soundpart.Highlight:Destroy()
							isFading = false
						end)
					end
				end
			end
		end
	end
end

for _,sound in soundStuff do
	task.spawn(loop,sound)
end

EDIT: Just edited the code because I made a mistake, if you tried it before and it didn’t work, try it again

Hope this Help :happy4: !

local soundStuff = game.Workspace.soundStuff:GetDescendants() -- folder with parts
local localPlayer = game.Players.LocalPlayer
local hum = localPlayer.Character:WaitForChild("HumanoidRootPart")

-- you can name your function whatever you want
function NameFunction(sound)
	local debounce = false
	
	while task.wait() do
		local soundpart = sound
		local mag = (hum.Position - soundpart.Position).Magnitude

		if mag < 20 then -- if in range then, add outline and sign
			if debounce == false then
				local highlight = Instance.new("Highlight")
				highlight.Adornee = soundpart
				highlight.Parent = soundpart
				highlight.FillTransparency = 1
				highlight.OutlineColor = Color3.new(255,0,0)
				highlight.OutlineTransparency = 0

				local Gui = game.ReplicatedStorage:WaitForChild("DisplayGui"):Clone()
				Gui.Parent = soundpart
				Gui.Image.ImageTransparency = 0
				debounce = true

			end
		else --if out of range
			if soundpart:FindFirstChild("Highlight") then
				repeat
					task.wait(0.1)
					soundpart.Highlight.OutlineTransparency += 0.1
					soundpart.DisplayGui.Image.ImageTransparency += 0.1
				until soundpart.Highlight.OutlineTransparency >= 1
				-- fade then delete
				soundpart.DisplayGui:Destroy()
				soundpart.Highlight:Destroy()

				debounce = false

			end
		end
	end
end

for i, sound in pairs(soundStuff) do
	if sound.Name == "HearableSound" then	
		coroutine.wrap(NameFunction)(sound.Parent)
	end
end```

Make sure to use task.spawn to prevent blockages

okay, i think i need to clear things up.

the way the script should work is whenever there is a part that contains a sound - which is called “HearableSound” - and if that part is within the range of the player (the magnitude thing) then it gets highlighted and the caution billboardgui appears above it.

  • the debounce is there because otherwise the script constantly spawns in new highlights and guis. (very laggy obv)

  • I cant use functions because i need the script to run constantly,

also Ive tried all the scripts provided and none of them seem to work. although the scripts by @ys3741 and @TamalessGod seem to work but only for one part.

I did make the script work by checking if it already contains highlight

if not (soundpart:FindFirstChild("Highlight")) then

however now, not all parts in the folder get highlighted, i think this is because the parts in the folder get called as soon as the script starts.

  • i need to find a way to pause the script untill all the parts load

Add this to the bottom of the script that I gave:

workspace.soundStuff.DescendantAdded:Connect(loop)

the loop messes some stuff up i think because this prints now during the fade away stuff.
it isnt a problem but it kinda annoys me by filling up the output
only works for one part again

That’s not the script I posted here …

i edited my original and added the loop

Just copy the entire script straight from my post, I think it should work

Here is a video of it working and how i have it set up in the workspace !

okay your original script fully works now,
thanks :slight_smile:

any idea how I could script a function to toggle it on/off? I have a remoteEvent in rep storage that fires once the game starts

Add boolean flag to the while task.wait() and set it to true or false based on when you want it to be used.

local soundStuff = game.Workspace.soundStuff:GetDescendants() -- folder with parts
local localPlayer = game.Players.LocalPlayer
local hum = localPlayer.Character:WaitForChild("HumanoidRootPart")
local debounce = false

local function loop(sound:Instance)
	local isFading = false
	while task.wait() do
		if sound.Name == "HearableSound" and sound:GetAttribute("Toggle") then	
			local soundpart = sound.Parent
			local mag = (hum.Position - soundpart.Position).Magnitude

			if mag < 20 then -- if in range then, add outline and sign
				isFading = false
				local highlight = soundpart:FindFirstChild("Highlight")
				local Gui = soundpart:FindFirstChild("DisplayGui")

				if not highlight then
					highlight = Instance.new("Highlight")
					highlight.Adornee = soundpart
					highlight.Parent = soundpart
					highlight.FillTransparency = 1
					highlight.OutlineColor = Color3.new(255,0,0)
				end

				highlight.OutlineTransparency = 0

				if not Gui then
					Gui = game.ReplicatedStorage:WaitForChild("DisplayGui"):Clone()
					Gui.Parent = soundpart
				end

				Gui.Image.ImageTransparency = 0

			else --if out of range
				if soundpart:FindFirstChild("Highlight") then
					if not isFading then
						isFading = true
						task.spawn(function()
							repeat
								task.wait(0.1)
								if not isFading then return end
								soundpart.Highlight.OutlineTransparency += 0.1
								soundpart.DisplayGui.Image.ImageTransparency += 0.1
							until soundpart.Highlight.OutlineTransparency >= 1 
							-- fade then delete
							soundpart.DisplayGui:Destroy()
							soundpart.Highlight:Destroy()
							isFading = false
						end)
					end
				end
			end
		end
	end
end

for _,sound in soundStuff do
	task.spawn(loop,sound)
end

workspace.soundStuff.DescendantAdded:Connect(function(descendant)
	table.insert(soundStuff,descendant)
	loop(descendant)
end)

local function start(toggle)
	for _,sound in soundStuff do
		if sound.Name == "HearableSound" then
			sound:SetAttribute("Toggle",toggle)
		end
	end
end

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