TouchEnded Issue

Hello! Apparently, This is My Second Post Here.

In my Game Recently, I’ve Been Trying to Make Region Notification Which a Ui Notifies the Player Once
They’ve Entered a Specific Region Throughout the Map. I’ve Been Using Touch and TouchEnded for Detecting If the Character Has Entered a Certain Region. I Find it More Efficient because Using Loop with Region3 Would Cause Performance Issues for The Client.

My Issue is TouchEnded Fired When The Player’s Character Jumps but In Studio It Didn’t Fire (Which is What I Wanted) The same goes with The Mobile Version of Roblox but Somehow The Website Version of Roblox Does This. I Don’t Know If This is an Engine Bug Though or It Could be My Code.

Here’s a Gif

https://i.gyazo.com/a432eb488c09b6163c1ef5941c3a26e5.mp4

As You Can See from The Gif Above, When I Entered That Region It Notifies Me. But Once I Jumped
It Notifies Me Again (Not What I Wanted).

Another Gif Example (Appstore Version)

https://i.gyazo.com/6fd6ede8d5f1f940f5c9be6905c3d04f.mp4

Here’s My Code

local Regions = workspace:WaitForChild("Regions"):GetChildren()
spawn(function()
	for i , SelectedRegions in pairs(Regions) do 
		local RegionName = SelectedRegions:WaitForChild("Name")
		SelectedRegions.Touched:Connect(function(thething) -- When An Object Touched / Entered The Part / Make Contact
			if thething == Character.PrimaryPart and EnteringRegion ~= true then -- If The Part That Entered Is The Character
				if CurrentRegion ~= RegionName.Value then -- If the region is not the current region
					UponEnteringRegion(RegionName.Value) -- Has Entered Region
					print("Player In Region")
					if DoingVisuals ~= true then
						spawn(function()
							Visuals(CurrentRegion)
						end)
					end
				end
			end
		end)
		
		SelectedRegions.TouchEnded:Connect(function(thething) -- When An Object No Longer Touching / No longer inside part's Region
			if thething == Character.PrimaryPart and EnteringRegion == true then -- When They Already In a Region
				if CurrentRegion == RegionName.Value then -- If the region is the current region so when character can enter other regions without being in the same one
					UponLeavingRegion(RegionName.Value) -- Has Left Region
					print("Player Left Region")
				end
			end
		end)		
	end
end)

I Would Appreciate Any Advice or Tips :smile:.

I had to do some region3 related code, and to be honest; do not rely on Roblox’s Region3 and rather use a custom module for it.

Zone is probably the best one out there, you can simply reference when a player has entered/exited a zone with:

zone.playerEntered:Connect(function(player)
    print(("%s entered the zone!"):format(player.Name))
end)

zone.playerExited:Connect(function(player)
    print(("%s exited the zone!"):format(player.Name))
end)

TouchEnded is very unreliable and should be avoided as much as possible!

1 Like

Thanks For The Suggestion!

I Will Look This Up and See If This’ll Solve My Issue :grinning:.

here try this

-- Rami_XD1

local runService = game:GetService("RunService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local Regions = workspace:WaitForChild("Regions")
local collisionPoint
local collisionPoints

local CurrentRegion 

local function UponEnteringRegion(RegionName)
	print(RegionName)
end

local function UponLeavingRegion(RegionName)
	print(RegionName)
end

local function CheckRegions()

	collisionPoint = character.PrimaryPart.Touched:Connect(function() end)
	collisionPoints = character.PrimaryPart:GetTouchingParts()

	local isin = false

	for i = 1, #collisionPoints do
		local RegionName = collisionPoints[i]

		if collisionPoints[i]:IsDescendantOf(Regions)then
			isin = true
			if RegionName ~= CurrentRegion then	
				UponEnteringRegion(RegionName.Value)
				print("Player In Region "..RegionName.Name)
				CurrentRegion = RegionName
			end		
			break
		end
	end

	if not isin then
		if CurrentRegion then
			UponLeavingRegion(CurrentRegion.Value)
			print("Player Left Region "..CurrentRegion.Name)			
			CurrentRegion = nil
		end
	end

	collisionPoint:Disconnect()
end


runService:BindToRenderStep("CheckRegions", Enum.RenderPriority.Input.Value, CheckRegions)
1 Like

I’ve Tried Your Methods But Sadly, The Local Script’s Activity Rate Went Above 3% (In My Opinion Already Causing Performance Issue).

Thank You for Your Methods!

1 Like

After Testing This For Myself, This Was The Better Option In The End.

I’m Very Grateful. Thanks For The Suggestion :smile:

2 Likes