Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I disconnect an event/function that is connected using :connect(function)?

script.Parent.Activated:Connect(function()
	local animation = script.Parent.Animation
	local animationtrack = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(animation)
	animationtrack:Play()
		script.Parent.Handle.Touched:Connect(function(Hit)
				if animationtrack.IsPlaying then
			Hit.Parent:FindFirstChild("Humanoid"):TakeDamage(5)
				else
				script.Parent.Handle.Touched:Disconnect() -- problem
		end	
	end)
end)

I am trying to disconnect the event, but it just gives me an error instead.

3 comments

0

ahem...

0

Your answer doesn't even work. I am not accepting something that is broken.

0

first of all add a local with the name you want, like local event = script.Parent.Activated:Connect(function() then after that do event:Disconnect() outside but not inside th ecode

3 answers

✓ Accepted answer

I feel bad for the amount of crap answer you have got. So this is A way to do it. Its what I use. We just create a bool variable and check it when the function is ran

disconnected = false

script.Parent.Touched:Connect(function(touch)
	if disconnected then return end --We check to see if the function has been "Disconnected"
	--Code here
end)

If you need to actually disconnect the event, do something like this:

-- "PartTouched" now represents what "connect()" returns, which is the event signal for this individual event.
local PartTouched = Part.Touched:connect(function()
	print("Part touched")
end)
	 
-- Once you have a reference to that signal, you can then disconnect it by calling the "disconnect()" method on it whenever you want.
PartTouched:disconnect()

You can set the disconnected to true or false at your descretion/as needed. I hope I was able to help. If you have any questions, feel free to comment to my post.

2 comments

0

How is this answer better than mine? He is obviously trying to disconnect handle.Touched connection, after main part (scrip.Parent) has been touched. Those are 2 different connections. You have just provided him with some stock code, while I gave him a working solution.

0

Because you literally said: "like this". Give some context to your answers. Its great to now how to do it, but you dont benefit if you dont understand what your actually doing

since you have connected the function on the same line, it will not work

local Initiate = function()
	print("hi")

end

local connection = script.Parent.Touched:Connect(Initiate)

wait(1)

connection:Disconnect()

So you would do something like

local function TouchEvent(hit)
	
	if animationtrack.IsPlaying then
		hit.Parent:FindFirstChildOfClass("Humanoid"):TakeDamage(5)
	else
		connection:Disconnect()
	end
end

local connection = script.Parent.Handle.Touched:Connect(TouchEvent) --connects the event to the function

Like this:

script.Parent.Activated:Connect(function()
	local animation = script.Parent.Animation
	local animationtrack = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(animation)
	animationtrack:Play()
	local connection = script.Parent.Handle.Touched:Connect(function(Hit)
		if animationtrack.IsPlaying then
			local humanoid = Hit.Parent:FindFirstChild("Humanoid")
			if humanoid then -- added extra check here, otherwise your script may error when touched by a non humanoid
				humanoid:TakeDamage(5)
			end
		else
			connection:Disconnect()
		end 
	end)
end)

Edit:Consider adding debounce.

This discussion is preserved as it was. New answers, comments, and votes are closed.