Touched event linked to single function

Hi! I hope this is not asked before, I have tried to search and did not find it.

I wanted to have one single script that manages the Touched event for every part contained in a folder. So I coded this:

local folder = workspace:WaitForChild("Partes")
local parts = folder:GetChildren()

function cargar(part)
	
	-- whatever the function does
	
end

for _, part in ipairs(parts) do
	part.Touched:Connect(cargar)
end

My question is: Is there any way of knowing which basepart raised the event? Or the only way to do that is placing multiple scripts as children of each part?

Thank you in advance.

Couldn’t you just check for the Part itself, provided for the Touched Event’s parameter?

local function cargar(part)

	if part:IsA("BasePart") and part.Name == "Something" then 
	-- whatever the function does
	
end

You want to get the part that was touched?

local folder = workspace:WaitForChild("Partes")
local parts = folder:GetChildren()

function cargar(partTocuhed, part) -- Now the first argument is the part that touched me, and part is the party that the player/part touched.
	
	-- whatever the function does
	
end

for _, part in ipairs(parts) do
	part.Touched:Connect(function(partTocuhed) cargar(partTocuhed, part) end)
end
1 Like

Exactly, not the part that touched, but the part that is anchored waiting to be touched.

I think you had the answer to this?

My mistake, I miss understood.

I’m not sure I follow, you stated that you wanted to know which “BasePart” raised the Event (Which my assumption is that you’re not wanting to check for the Part that got touched provided by the Touched Event)

I am confusion 100

My first language is not English, let me try again. There are several bricks on the floor (parts in the folder) and the character touches one of them with the left foot. The Touched event has the information of the left foot in the parameter called part, but my question is: can I get which one of the bricks was touched?

Okay

I think I understand, it is possible but the only issue is that you can’t pass additional parameters/arguments, only the ones that are provided by the Events themselves so you’ll have to find some workaround in order to get the Part that got touched inside the Folder itself

local folder = workspace:WaitForChild("Partes")
local parts = folder:GetChildren()

for _, part in ipairs(parts) do
	part.Touched:Connect(function(TouchedPart)
        print("Part that activated the Touched Event: "..TouchedPart)
        print("Part that started the Touched Event: "..part.Name)
    end)
end
1 Like

I guess I cannot mark as solution two posts… But you and @JackscarIitt helped me a lot. Thank you very much!