How to stop a function in the same script

How would I do this? for example:

script.Parent.ProximityPrompt.Triggered:Connect(function()
    --code
end)
--If cancel is == true then, stop the function above if it is mid-execution.

heartbeatrunservice:Connect(function()
   if cancel == true then
       proxPromptFunction:Stop()
   end
end)
1 Like
script.Parent.ProximityPrompt.Triggered:Connect(function()
    --code
end)
--If cancel is == true then, stop the function above if it is mid-execution.

heartbeatrunservice:Connect(function()
   if cancel == true then
       proxPromptFunction:Disconnect()
   end
end)

What is this even supposed to do?

It would disconnect the given function.

How do you get the specific function

Say you have a function named “Example”

You would call

Example:Disconnect()
function example()
	
end

example():Disconnect()

So like this?

1 Like

Remove the brackets on “example”
In your case, it’d be:

heartbeatrunservice:Connect(function()
   if cancel == true then
       proxPromptFunction:Disconnect()
   end
end)

Unless I’ve misunderstood or something…

Yeah, it’s correct. What it does is breaks the code or disconnect it you can say. But remove the brackets from example.

Like this

function example()
	
end

example:Disconnect()

The code I sent was an example, I was looking to know how to do something like this:

script.Parent.Triggered:Connect(function()
       --code
end)

If cancel == true then, stop the function.

Wouldn’t it be:

local cancel = false

local MyFunction = script.Parent.Triggered:Connect(function()
     warn("hi")
end

heartbeatrunservice:Connect(function()
   if cancel == true then
       MyFunction:Disconnect()
   end
end)
1 Like

That’s not correct…You can only call Disconnect() on RBXScriptConnections.

local event = TextButton.MouseButton1Click:Connect(function()
   -...
end)

--...
event:Disconnect() -- Disconnect the event
2 Likes

Ok, thanks for recorrecting me. I don’t have quite a good knowledge on disconnecting functions. But thanks !

1 Like

Yes, this is the problem we all had… There wasn’t any reference to the event that was being disconnected.

1 Like

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