Problems with Touch and TouchEnd

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Detect when Humanroot part enters and leaves the part
  2. What is the issue? Include screenshots / videos if possible!
    it’s pretty much spamming the two prints consecutively
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Mixing up the denounces but I’m not sure what order things should be
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local mattouch = true -- debounce thing
AvgMat.Touched:Connect(function(hit)  -- if hit
	if not mattouch then return end -- is mat can be touch make false
	mattouch = false
	if not hit.Name == "HumanoidRootPart" then return end  -- if hit is a root part
	print("Touched by a person")
end)

AvgMat.TouchEnded:Connect(function(hit) -- Ended touch
	if not hit.Name == "HumanoidRootPart" then return end -- if the part is a root part
	print("Humanoid left")
	mattouch = true -- allow touching
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like
local inRegion = {}

AvgMat.Touched:Connect(function(hit)
	if hit.Name ~= "HumanoidRootPart" or table.find(inRegion, hit) then return end 
	table.insert(inRegion, hit)
	print("Touched by a person") 
end)

AvgMat.TouchEnded:Connect(function(hit)
	if hit.Name ~= "HumanoidRootPart" then return end
	local index = table.find(inRegion, hit) 
	if not index then return end 
	table.remove(inRegion, index)
	print("Humanoid left")
end)
2 Likes

I would mix @NyrionDev’s solution with using GetPartsInPart so you can avoid using the awful touched events.

1 Like