Is it possible to do multiple things, depending on the touched part's name, from one function with multiple connections?

I have a function, with multiple connections.
Because I’d like this script to be softcoded, and not require changing the script (the properties of the mechanism are specified in a table), I’d like to know if it’s possible to know which part was touched or clicked, so that depending on where that part is specified in the table, different things can happen.

function InputFunction(Hit)
--print(touched/clicked part)
end

for i,v in pairs(Input:GetDescendants()) do
     if v:IsA("ClickDetector") then
            v.MouseClick:Connect(InputFunction)
     elseif v:IsA("Part") and v.Parent:IsA("Folder") then
            v.Touched:Connect(InputFunction)
     end
end
2 Likes

Im not sure if this is what you mean by that I probably misread what you said.

if Hit.Name == "NAME" then
    --print("Touched/Clicked "..Hit.Name)
elseif Hit.Name == "NAME" then
    --print("Touched/Clicked "..Hit.Name)
end

I mean the part that the player actually clicks, or touches. Not the one who touched it.

Could always try something like:

function InputFunction(Hit)
--print(touched/clicked part)
end

for i,v in pairs(Input:GetDescendants()) do
     if v:IsA("ClickDetector") then
            v.MouseClick:Connect(function() InputFunction(v.Parent) end)
     elseif v:IsA("Part") and v.Parent:IsA("Folder") then
            v.Touched:Connect(function(hit) InputFunction(hit) end)
     end
end

Yes, that did the trick.
On line 9, I removed the first “hit” and replaced the 2nd one with “v”, because otherwise it just outputs the person or part that touched.
Thank you.

1 Like