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

How can I disconnect a function?

How can I disconnect a function? For example;

function oc(ack)
find=ack.Parent:FindFirstChild("Humanoid")
if find then
find.Health=50
end end

script.Parent.Touched:connect(oc)
script.Parent.TouchEnded:disconnect(oc) --Would this work? If not, how can I disconnect the function?

3 answers

Connections are actually instances of RBXScriptSignal. Creating one (with connect) returns a instance of RBXScriptConnection. And there's a method that allows you to disconnect them.

So

connection = script.Parent.Touched:connect(function(part)
	print(part.Name)
	connection:disconnect()
end)

1 comment

0

Wow, I never really knew that until now. Thanks man! +1

game.Workspace.Yellow.Touched:connect(function(ack)
find=ack.Parent:FindFirstChild("Humanoid")
if(find)then
	find.Health=50
end
end)

Not too sure what you meant by disconnect but I guessed you meant make it only run once and if so no code will run twice without a loop inside of it. The code above is a tad more efficient that your own and will only run once due to the non-existence of a loop.

1 comment

0

You are wrong. That code can run more than once because it is being called every time the event fires, i.e when you touch the brick.

There's only two methods I can think of doing this, (although you'd might want to look up breakpoints)

Method 1

Call a function that doesn't exist, it's messy, but it does the job

part = Workspace.Part

part.Touched:connect(function(hit)

	--code code code...

	callaFuntionThatDoesnExist()
end)

Method 2

Use a debounce

part = Workspace.Part

active = true

part.Touched:connect(function(hit) if not active return end

	--code code code...

	active = false
end)

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