How to disconnect a function with another function?

Touched is inconsistent. Even if you get fancy with it, you still will have problems where TouchEnded fires when it shouldn’t.

Fancy way that accumulates time, still doesn't work perfectly.
local touching = false

script.Parent.Touched:Connect(function() print("touched") touching = true end)
script.Parent.TouchEnded:Connect(function() print("touchended") touching = false end)

local touchingTime = 0 -- will use this to keep track of how long
-- we've been touching

local WAIT = 0.1 -- how long between activations

local function Activate()
	print("Activated!")
end

game:GetService("RunService").Heartbeat:Connect(function(step)
	if touching then
		touchingTime = touchingTime + step
	end

	-- while loop just in case a frame takes a really long time or
	-- something
	while touchingTime >= WAIT do
		Activate()
		touchingTime = touchingTime - WAIT
	end
end)

If this is for checking if a Humanoid is on the ground, you can look at the Humanoid | Roblox Creator Documentation instead.

If this is checking if a person is within a region, check for a Region3 | Roblox Creator Documentation every frame.

If this is really a Part-to-Part interaction, and you can’t just Raycast every frame to fake it, and a slightly-larger-than-the-part Region3 won’t work, I guess you could use this Rotated Region 3 Module instead and rotate a Region3 to match up with the part.

1 Like