How to disconnect a function with another function?

Well, by this I mean that I want to disconnect a Touched function because it only executes when you are moving and I want it to execute whenever you are touching the object and for that I put a loop, but to deactivate it I do not know why I want it to be turn off when you stop touching the object but I really don’t know how to do it

local Enabled = false

local function Lumberjack()
while Enabled == false and wait(0.1) do
print("Activated")
end

end

local function Finish()
Enabled = true
wait(0.5)
Enabled = false
end

script.Parent.Touched:Connect(Lumberjack)
script.Parent.TouchEnded:Connect(Finish)

Maybe an elseif function about a moving player could do, I am not on my pc right now so I wont be able to make a script, but this is just a slight idea.

Also you can go the extra mile and connect a remote with a timed reaction.

1 Like
local Enabled = false

local conn

local function Lumberjack()
	while Enabled == false and wait(0.1) do
		print("Activated")
	end
end

local function Finish()
	conn:Disconnect()
	Enabled = true
	wait(0.5)
	Enabled = false
end

conn = script.Parent.Touched:Connect(Lumberjack)
script.Parent.TouchEnded:Connect(Finish)

I tried it and it doesn’t work, but thanks

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

Something like this?

local touching = false
script.Parent.Touched:Connect(function()
	touching = true
end)
script.Parent.TouchEnded:Connect(function()
	touching = false
end)

while true do
	if touching then
		print("Touching")
	end
	
	wait()
end