Zone Trigger Script

I want to have a region (inner and outer), defined by the bounds of two parts, that registers when a player has entered or left the region.

Here’s my code. It works fine when I walk in and out of the zone, but for some reason when I jump, it triggers my character leaving and reentering the zone, twice, even though my character never gets close to crossing the top of the inner boundary.

local Players = game:GetService("Players")

local model = script.Parent
local startTrigger = model:WaitForChild("StartTrigger")
local endTrigger = model:WaitForChild("EndTrigger")

local function findPlayerFromRoot(part)
	if not part then return end
	local char = part:FindFirstAncestorOfClass("Model")
	if not char or char.PrimaryPart ~= part then return end
	return Players:GetPlayerFromCharacter(char)
end

startTrigger.Touched:Connect(function(hitPart)
	
	local ply = findPlayerFromRoot(hitPart)
	if not ply then return end
	
	print("entering")
end)

endTrigger.TouchEnded:Connect(function(hitPart)
	
	local ply = findPlayerFromRoot(hitPart)
	if not ply then return end
	
	print("leaving")
end)

Zone for reference.

maybe try adding a debounce? the touchevent may be running every time the char’s position changes

2 Likes

maybe try ZonePlus by 1ForeverHD

2 Likes

this is just because of the Touched function being kind of bad, and so like @daulric said, you could do ZonePlus, or (i believe) make a Region3 thing instead of using the Touched function

1 Like

This is normal behavior by the physics engine. There’s even a bug report for it here:

The behavior can’t be accurately explained but jumping is the only way this occurs.

ZonePlus, Quadtrees, Magnitude calculations, using :GetTouchingParts() are more effective ways to getting around this. A debounce would not help because Touch and TouchEnded fires while in the part and you can have an edge case of jumping out of the part around when that happens.

However, the player will always activate TouchEnded last when leaving, even when jumping. You could exploit this fact if you still want to use Touched/TouchEnded.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.