Quick Touched() Part Question

Hiwi! Quick question. Thank you for reading!

While using Touched event, we get the Parts that are touching the Part that is connected to the event. How do I get the Part that is connected to the event inside the same function?

The main Part were connected by a loop, and I dont have an instance to know which part is… I need to recognize the main Part when the Player is touching it, and use those 2 Parts, the Player, and the Main event Part.

local function Chk(a, b) -- Imaginary b var, its the main part connected to touch event the p var
   	if a.Name == "HumanoidRootPart" then
   		print("The Player", a.Name)
		print("Main Part with the Touched event connected", b.Name)
	end
end
p.Touched:Connect(Chk)

How do I get the main event part?

1 Like

I haven’t tested it so I’m not sure how well this would work, but there are a couple methods you could use.

The first being calling this function: BasePart | Documentation - Roblox Creator Hub
Though that doesn’t guarantee that there aren’t more than just the part you want, and they could have stopped touching it.

The easiest and most reliable is to just call an anonymous function in your Connect() and then call the Chk() from that.

local function Chk(a, b)
  if a.Name == "HumanoidRootPart" then
    print("The Player", a.Name)
    print("Main Part with the Touched event connected", b.Name)
  end
end

p.Touched:Connect(function(hit)
  Chk(hit, p)
end)
1 Like

You can do something like this:

local ObjectsTable --References to objects table that will be looped through.
function ConnectTouchedEvent(Mainpart)
	Mainpart.Touched:Connect(function(TouchedPart)
		print("The Player", TouchedPart.Name)
		print("Main Part with the Touched event connected", Mainpart.Name)
	end)
end

for i, v in pairs(ObjectsTable) do
	if v:IsA("BasePart") then
		ConnectTouchedEvent(v)
	end
end

1 Like

Im always confused when I see the anonymous functions. Thats why Im always using functions with names :v

I felt that was the proper method.
So, I will connect the Touch event while doing the loop with an anonymous function! Oka, thank you so much! I will test it right now

Another thing you could do, unless you plan on using Chk() in multiple places, is just remove the Chk() function and put the code from it inside the function in the Connect();

p.Touched:Connect(function(hit)
  if hit.Name == "HumanoidRootPart" then
    print("The Player", hit.Name)
    print("Main Part with the Touched event connected", p.Name)
  end
end)
1 Like

Yup, I thought about storing parts in a table, but, its more work xD I have enough weird tables everywhere for now. I will try that way if I cant connect it with the anonymous function. Thank you so much!!

Yup, I use that way too, right now, Im just trying to learn how to be more well organizated. So I will try everything, thank you so much @MrLonely1221 !!

Yup, its working great, thank you. Wow im so lazy, why I did not research on how to use those anonymous functions… kinda looks confusing for me write a function like that, Im used to a more normal and readable for me way :v
But yup, works perfect, thank you! :3

1 Like

this is an alternative method, just to keep you in mind.

local function ChkFactory(a)
    return function (b) -- Imaginary b var, its the main part connected to touch event the p var
        if a.Name == "HumanoidRootPart" then
            print("The Player", a.Name)
            print("Main Part with the Touched event connected", b.Name)
        end
    end
end

p.Touched:Connect(ChkFactory(p))
1 Like

Maybe Im doing it wrong someway… cause I tried exactly that, and I got an error. I dont know why then… That was the first thing I tried. Probably I made a mistake in something.

I’m pretty sure it works. Since you are working with characters you might need to do some checking. I can’t think of anything else.

1 Like

Im sure I did something wrong the first time I tried it. Im sure it works too. But I always fail a lot, so, Im gonna try it again, cause seems more compact and useful!