5.7.1 Touched Event

One of the most common uses of events is to detect when a part has been touched.

If we look at the API for (base)parts, there is a Touched event that passes the other touching part to the listener. Using that information, we can find the character that touched some part ingame.

First, add a part into the workspace if you do not already have one there. This will also require a play session since we need physics simulation.

Lua
local part = game.Workspace.Part

part.Touched:Connect(function(otherPart)
	local character = otherPart.Parent
	
	if character.ClassName == "Model" then
		print("Detected:", character, otherPart)
	end
end)

Now start the session and go walk all over that part. And we get….


Well, we get kind of a mess actually.

Lua
--Detected: {Character_Name_Here, LeftFoot}
--Detected: {Character_Name_Here, LeftLowerLeg}
--Detected: {Character_Name_Here, RightLowerLeg}
--Detected: {Character_Name_Here, LeftUpperLeg}
--Detected: {Character_Name_Here, RightUpperLeg}
--Detected: {Character_Name_Here, LeftLowerLeg}
--Detected: {Character_Name_Here, LeftFoot}
--Detected: {Character_Name_Here, HumanoidRootPart}
--Detected: {Character_Name_Here, RightFoot}
--Detected: {Character_Name_Here, LeftFoot}

The message gets printed out a whole bunch of times. But our character only touched the block one time. It was just one time, right?