This has been solved by greenboo5 and CreditsMix.
I’m trying to make a regions system for my game that will not have much lag. I have tried multiple ways and want to be able to design it directly how I want (For example my desert is not an easy shape to make with things like region3 or however that works (I have never really used region3 and don’t quite understand how to use it atm)
I have tried multiple ways such as making a huge part around the area (for a square/rectangle area) and using touched connect and ended, this in return caused major lag making fps go from 60 to 50 in any area away from it due to all the parts inside of it. (Some move) So instead of going with that I decided I would still use touched connect this time though I would make 8 walls. 4 covering the area from the outside and 4 covering the area from the inside. On touch of the outside walls it will disable that region and detect that you have left it. On touch of the inside it should be activating and detecting that you have entered that region. This is where the bug comes in.
Code below, some parts of it were left out because they were unimportant and things like the functions already work its just touch event wont.
local Inside = false
local RegionName = "Forest"
local Region1 = game.Workspace.Regions:FindFirstChild(RegionName)
for i,v in pairs(Region1.Inside:GetChildren()) do
v.Touched:Connect(function(Part)
if Inside == false then
if v.Parent then
if v.Parent:FindFirstChild("ExtraData") and v.Parent:FindFirstChild("Humanoid") then
Entered(RegionName)
end
end
end
end)
end
Screen shot of the way the walls thing works below. Yes I know im such a great drawer, no need to complement me.
You don’t have a debounce which is probably why you’re experiencing lag. Without the debounce, the Touched event is fired a million times each second.
The reason why you claim the Touched event isn’t working is because you’re trying to check for “ExtraData” and “Humanoid” on v.Parent. Let me remind you that v is an object and not the hit part. You should call the Part instead.
Use an alternative plan for this.
I’ve adjusted the code that you made in effort to fix the bug. Please note that this code is a very hacky method and I don’t suggest you use this long-term. However, I would look for different methods in accomplishing your task.
local debounces = {}
local Inside = false
local RegionName = "Forest"
local Region1 = game.Workspace.Regions:FindFirstChild(RegionName)
for i,v in pairs(Region1.Inside:GetChildren()) do
v.Touched:Connect(function(hit)
local tagPlayer = game.Players:GetPlayerFromCharacter(hit.Parent)
if tagPlayer then
if Inside == false and hit.Parent:FindFirstChild("ExtraData") then
if not debounces[tagPlayer.Name] then
debounces[tagPlayer.Name] = true
Entered(RegionName)
wait(5) --change to whatever debounce
debounces[tagPlayer.Name] = false
end
end
end
end)
end
I would use a point in polygon algorithm to determine if they are in a specific region.
I usually use a module script I keep in my packages for this use case.
This lets you create a polygon surrounding an area, and when you check it will return if a part is in a polygon. It’s much, much less resource intensive than region3, doesn’t require you to create lots of events, and you can build odd shapes quite easily.
I believe you need to switch v with Part in the following lines of code:
v is your boundary part that fires the event, part is the object / character part that hit the boundary. I beleive the TouchedEvent was always running, it just would not get past your ExtraData & Humanoid check. I would say for the future use prints to debug where your code actually runs.
I think you are right about this thanks for pointing that out to me. I’ll get back to you on if fixing that works.
EDIT: this has fixed it the events all fire now and I still remain at a solid 60fps.
I’ll test credits way still.