How can I destroy a specific object when its parent has the same name as the other things?

I made that when a door is opened it destroys a block within it, but since the parent of that block has the same name as the other models, then the block is destroyed for all the models with the same name, what do I do?

code here:


					local filtro = script.Parent:FindFirstChild("Blastdoorfilter")
					if filtro then
						for _, destroy in pairs(script.Parent:GetChildren()) do
							if destroy.Name == "Blastdoorfilter" then
								script.Parent.Blastdoorfilter:Destroy(1)
							end
						end
					end
					
					
					
					evento:FireClient(player,"correcto")
					evento:FireClient(player)
					center.granted:Play()
					wait(.7)
					center.open:Play()
					open1:Play()
					open2:Play()
					open1.Completed:Connect(function()
						wait()
						deb = false
						open.Value = true
					end)

image

1 Like

Instead of iteration could you just call Destroy on a single reference? You’ve searched for filtro, if it exists could you not call filtro:Destroy()?

Alternatively, in your iteration, you’re iterating children with destroy. Yet, when you find destroy.Name == "Blastdoorfilter" to be true, you call :Destroy on script.Parent.Blastdoorfilter, it should be:

if destroy.Name == "Blastdoorfilter" then
    destroy:Destroy()
end
1 Like

the same thing keeps happening, destroy all the blocks of all the models with the same name

I’m not seeing a reference issue in your snippet of code that might be bringing the iterations’ scope outside of just the one door in your explorer heirarchy. What is triggering the code? It might be an issue somewhere else.
Is every script is listening for this “evento” event, and they’re all performing this operation instead of just the intended door?

1 Like

my code only opens and closes the door the player is closest to.
and yes all the doors are listening the event but just it activate the most closer to the player

Can’t you rename it? Then change the script?

1 Like

Okay, try doing this instead of iteration:

local filtro = script.Parent:FindFirstChild("Blastdoorfilter")

if filtro then
    print("Deleting a part located at " .. tostring(filtro.CFrame.Position))--This should only print once
	filtro:Destroy()
end

Since it is a reference to only one instance, this should only delete one part.
If it doesn’t, something outside of your snippet isn’t operating as intended and multiple doors are executing that part of the code.

1 Like

i tried with magnitude to the most closest to the player and it works

1 Like