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

How do I make my touched function stop?

Hello everyone! I'm not a professional at scripting so I have bumped into a problem that I've been having a hard time fixing. I've been trying to script a 'punch' ability in the form of a tool. So, it went pretty well until I stumbled into a problem. Whenever it activates, it will continue dealing damage forever. I will be happy if someone comes and corrects my mistake or gives me some form of help!

	script.Parent.Activated:Connect(function()
		local Debounce = false
		print("active")
		script.Parent.Handle.Touched:Connect(function(hit)
			if Debounce == false then
				Debounce = true
				print("something touched me!")
				wait(2)
				Debounce = false
			end
		end)

2 comments

0

Why are you connecting functions inside of functions

0

Is that bad? I'm trying to make it so the touched function only works while the tool has been activated.

1 answer

You can store the connection in a variable, and disconnect it once finished.

local Debounce = false -- I recommend leaving Debounce outside.
script.Parent.Activated:Connect(function()
    if Debounce then return end -- We don't want it to activate if debounce is on.
    print("active")
    -- Here, we store our connection in a variable. This will allow us to disconnect is later, and it will stop tracking touches.
    local connection 
    connection = script.Parent.Handle.Touched:Connect(function(hit)
        if Debounce == false then
            Debounce = true
            print("something touched me!")
            wait(2)
            Debounce = false
            connection:Disconnect() -- Disconnect after we're done.
        end
    end)

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