Script changes all parts in folder instead of just the one that was hit

Hello, I’m trying to make a section of my acid rain script change the part it hits to green and get destroyed after some time, but currently it’s selecting all destroyable parts instead of just the one that was hit by the acid rain and destroying them all.

I’ve tried playing around with in pairs but I’m not too familiar with it so I’m having a bit of trouble. Here is the section of code.

-- Part Acid Turns Green, Damages Player, then Melts (not yet)
	
	local Parts = workspace.Parts
	
	Parts:FindFirstChild("Part").Touched:Connect(function(hitAPartToDestroy)
		local parts = workspace.Parts:GetDescendants()
		for _, descendant in pairs(parts) do
			if descendant:IsA("Part") then
				descendant.Color = Color3.fromRGB(85, 255, 0)
				descendant.Anchored = false
			end
		end
	end)
	
	Parts:FindFirstChild("Part").Touched:Connect(function(touchedAPartToDestroy)
		local humanoid = touchedAPartToDestroy.Parent:FindFirstChild("Humanoid")
		if humanoid then
			if not Parts:GetAttribute("Touched") then
				Parts:SetAttribute("Touched", true)
			humanoid.Health = humanoid.Health - 10
			task.wait(1)
				Parts:SetAttribute("Touched", false)
				Debris:AddItem(Parts,1)
			end
		end
	end)

Thank you for your time :smile:

1 Like

Hi, you are using Parts in Debris:AddItem(), I am assuming you have all your destroyable parts and parts hit by the acid rain inside workspace.Parts (in other words, the parent). This will naturally destroy the parent of all those parts, along with all children inside (the parts).

If I am wrong, please clarify more about your question and what you are trying to do in the code. It seems you are using only one acid rain part?

1 Like

ur not selecting the part that the acid rain hit, but instead ur selecting every every destroyable part. try changing the parts variable in the first event to this:

local parts = hitAPartToDestroy:FindFirstAncestorWhichIsA(“Model”)

Hello, sorry for the late reply I was trying to fix my script using your suggestion. I was trying to solve the problem in another place file which probably caused more confusion.

I’m working on a game which has a round system, when the round starts, a map is chosen, and acid rain falls over the map. I’m trying to make it so when the rain hits a part in the map, the function executes.

Here is the updated script

	-- Part Acid Turns Green, Damages Player, then Melts (not yet)

	local Parts = workspace.Parts
	local Destroyables = game.ReplicatedStorage.Maps:GetDescendants()
	

	Destroyables.Touched:Connect(function(hitAPartToDestroy)
		local Destroyables = game.ReplicatedStorage.Maps:GetDescendants()
		for _, descendant in pairs(Destroyables) do
			if descendant:IsA("Part") or descendant:IsA("Model") then
				descendant.Color = Color3.fromRGB(85, 255, 0)
				descendant.Anchored = false

				descendant.Touched:Connect(function(touchedAPartToDestroy)
					local humanoid = touchedAPartToDestroy.Parent:FindFirstChild("Humanoid")
					if humanoid then
						if not Parts:GetAttribute("Touched") then
							Parts:SetAttribute("Touched", true)
							humanoid.Health = humanoid.Health - 10
							task.wait(1)
							Parts:SetAttribute("Touched", false)
							Debris:AddItem(descendant,1)
						end
					end
				end)	
			end
		end
	end)

Now it’s giving me an error saying
image

and the function isn’t running.

it gives me this error

image

This returns a table with all of the descendants of the folder

Try using for i, v in pairs(Destroyables), then you can connect it with the touched method.

Before you use the touched function, you need to check if the objects are a part

if v:IsA("Part") then  
-- touched event here
end