TouchZone v1.4 - Simple touch event wrapper

WARNING: This module is outdated. V2 of the module is planned and I further elaborate on it in the following reply, thanks.

---------------------------------------------------------------------------

Minimal, efficient and simple

TouchZone is a simple touch event wrapper for wide area player detection.
Delivers unnoticable performance delays while making your code much cleaner.
Handles all of the background work used to detect valid characters, and gives you easy-to-use events and methods to take advantage of.

Wide compability and open source

This module will work on any character that has a Humanoid under it, and it can run both on the server and the client, having minimal difference between the two.
Do bare in mind theres no security checks involved, so you might want to check the magnitude of newly added parts if you’re using the module server-side.
You’re free to edit and do anything with the module following the MIT license.

Saves your time and upgrades your touched events

My main goal with creating this module was to make setting up BasePart.Touched and BasePart.TouchEnded easier by offering a specific integration for characters, and making it extremely easy to detect character entering into your zones.
You can achieve the same behavior by listing all body parts that enter and leave, which fires the event when the first body part enters and last one leaves, this module just does that for you with some neat features.

Effortlessy create precise zones, with user handled filtering

This is not meant to be a replacement to BasePart.Touched for anything other than character detection in a large area, you’re discouraged from using it for any projectiles or rapidly moving areas due to innacuracy, though this is the case with the Touched event itself too.

Plug-and-play code sample for most users:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TouchZone = require(ReplicatedStorage:WaitForChild("TouchZone")) -- Require module
local MyPart = script.Parent -- Your part of choice

local ZoneFilter = function(character,part) -- Callback function for Touched (not required)
	if part:FindFirstAncestorWhichIsA("Accessory") then -- Ancestor is an accessory
		return false -- This part will be ignored
	end
	return true -- or this part will not be ignored
end

local MyZone = TouchZone.NewZone(MyPart,ZoneFilter) -- Attach a zone to the part with the filter

MyZone.OnEnter:Connect(function(character) -- Character starts touching the zone
	print("Character entered zone!")
end)
MyZone.OnLeave:Connect(function(character) -- Character stops touching the zone
	print("Character left zone!")
end)

In this sample I’m not allowing any accessories to be processed by the module, which is what most people would want to do, however you can filter it in any way you choose.
Read the API following the Github link for more information on everything the module can do.

You can add it to your game or look at the source code

Feel free to comment any suggestions or feedback, I’ll be actively checking this thread and updating the module with any reasonable requests and answering questions, thank you!

10 Likes

This looks great! It’s basically ZonePlus, but a .Touched alternative instead of a Region3 alternative. This will help a lot on my project. :+1:

1 Like

Hi, I’m glad that the module helped you :slight_smile:
And yes this appears similar to ZonePlus however I was aiming for something more lightweight and a general utility rather than a whole seperate system to what Roblox already offers, thats where I believe we differ.

TouchZone Update v1.4

  • Using :Enable() now doesn’t fire the OnEnter event for characters already present inside the zone.
  • Fixed type checking for callback function.
  • Improved local variable naming and code clarity.
  • Improvements to run order for active state check

Public update no2 - 08/12/2022

I m very interested! by any chance, do you have benchmark samples showing how it compares to ZonePlus?

Also, ZonePlus no longer uses region3.

Thank you! code looks awesome!

Hello, thank you for the compliments, it means a lot!
I’m going to do a benchmark promptly, but first let me explain our differences.

ZonePlus is supposed to cover basically all scenarios to track any BasePart, model, folder or a table containing such, afaik, as well as they have a much wider API. I can’t go much further into it because the code looks pretty complex for me.

TouchZone is made to cover only Models with a Humanoid present, tracking all body parts that the user whitelists with the filter function. The API is therefore very slim and besides the simple tracking you’re the one that filters out specific instances out of the character and then uses the 2 events to listen when the whitelisted parts of a valid character model have entered/left.
That is my current goal, I do have plans to extend the functionality, but it will mostly stay as Character detection as its primary goal, since going into details is just what ZonePlus does.

Basically, if you just want to detect when any Model that has a Humanoid enters or leaves a BasePart with minimal other features, TouchZone would be more prefferable due to sub 200 lines and the UX focusing on that just that. My planned features will not stray far from this idea.

I checked out the ZonePlus source code, and they do use Touched as well, without TouchEnded.
Due to our difference in methods(and honestly reading through all of their code is pretty complex for me), I’ll be benchmarking by comparing the time since a model teleports into the Zone, and the time the corresponding event fires. Expect a response in the next 2 hours.

I was a bit busy so it took me longer to create the benchmark code and test.
Either way, with 500 samples moving a Model with a HumanoidRootPart and a Head(ZonePlus required it present for some reason, but it has all collision disabled) in and out of the Zone, these are my results:

TouchZone v1.4

OnEnter
Average: 0.01677s
Minimum: 0.00483s
Maximum: 0.0191s

OnLeave
Average: 0.01672s
Minimum: 0.01184s
Maximum: 0.01948s

ZonePlus v3.2.0

itemEntered
Average: 0.04471s
Minimum: 0.00004s
Maximum: 0.1013s

itemExited
Average: 0.0453s
Minimum: 0.00005s
Maximum: 0.10276s

Bare in mind the testing environment is not completely objective since there is the teleport of the part and possiblity of server stuttering etc. however the average should tell you the general performance.

To simplify the results, talking about the average time between contact and event fire, TouchZone v1.4 is 63% faster than ZonePlus v3.2.0

If you believe my benchmark code is at fault here is the roblox place, repeating the test takes roughly 17 minutes. I believe the code captures the times with good accuracy but do tell me if I am wrong.
ZoneBenchmark.rbxl (73.1 KB)

1 Like

Hello there!
Its been 2 months without the V2 update. I wanted to clarify I was very busy with school and an ongoing project of mine, which all stopped me from development. I can make a promise to deliver an update to this page, the code as well as the github page sometime after the 16th of this month.

I’m sorry for keeping you waiting if you saw the message or were otherwise interested in the project.
I decided to make this message as I received several inquiries about the V2 release.
The main focus of the V2 aka the rework is overall better documentation and explaination of the module. Thanks for reading :slight_smile:

2 Likes

Another update!
Sadly during my break I couldn’t really fit in this module as I had a lot of work for my project as well as school preperation.
I’ve already updated the github code to the latest commit of V2 edits… just lacks some final tweaks im unable to do due to school and stuff, plus the redesign of this page and documentation.
So yeah I won’t post any more updates until I get the time to fully finish an update and do everything, didn’t wanna leave anyone waiting for a response thus this reply, thanks for reading.

1 Like