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

how do i stop a function from running?

script.Parent.BladePart.Touched:connect(function(hit)
				print(hit.Parent.Name)
				
			end)

it works fine but how do i break a function or make it run again until something happens or a period of time

2 comments

1

you can use "break" to stop it

0

k, ill try it

1 answer

✓ Accepted answer

I only learned about an interesting method that solves your problem around half a month ago; the disconnect() method! It does exactly what you'd expect: it disconnects the connection from an event. In order to do so, you must give your event a variable name. Here, I'll show you how yours should look:

local thisEvent = script.Parent.BladePart.Touched:connect(function(hit)
	print(hit.Parent.Name)
end)

Now say you want to disconnect that event named "thisEvent". Simply use this line of code:

thisEvent:disconnect()

This will effectively severe the event listener.

Note: you can also do this like so:

function hitFunc()
	print(hit.Parent.Name)
end

local thisEvent = script.Parent.BladePart.Touched:connect(hitFunc)

--Somewhere else:
thisEvent:disconnect()

Hoped this helped! If you need any other help, just ask! :)

Here is the wiki page for Connections/disconnect(): RBXScriptSignal

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