How do i reset a touched function?

I want to reset a touched function for a passing a bomb function

Touched function doesn’t reset when the instance in a variable change

I’ve tried looking for a solution but can’t find any if you do have one or found a solution please give it to me

4 Likes

If you wanna reset (or disconnect if that’s what you mean) the :Touched function, then
:Disconnect() should do the trick.

local touchedEvent
touchedEvent = instance.Touched:Connect(function()
    -- your code here
end)

touchedEvent:Disconnect() -- call this anywhere, will disconnect the event
4 Likes

You know how when you change a variable for example the variable is a yellow part then it changes to a purple part the touch function detects the yellow part not the new and shiny purple part. well i want it to reset so it changes to the purple part instead of staying with the yellow one! [hopefully this isn’t confusing :grimacing:]

4 Likes

Yeah, use :Disconnect()!
I’ll show an example:

local part = workspace.YellowPart
local touchedEvent
touchedEvent = part.Touched:Connect(function() -- declare the event in a variable
    -- code here
end)

-- ...

touchedEvent:Disconnect() -- removes connection from the yellow part

part = workspace.PurplePart
touchedEvent = part.Touched:Connect(function() -- create a new event for the new part
    -- code here
end)
5 Likes

but i want it to change in the function if that’s possible

4 Likes

you want a function that undoes any changes it’s made?

3 Likes

Do you mean, a different function when touched? different than the previous?,

3 Likes

I really don’t get what you’re trying to achieve here,
by what you mean is resetting it inside the function?

Like this?

touchedEvent = part.Touched:Connect(function()
   -- code here
   touchedEvent:Disconnect()
end)

or the better alternative:

part.Touched:Once(function() -- disconnects immediately after event execution
   -- code here
end)
2 Likes

I will also make an attempt to understand. Otherwise it’s best to show an image example or code of what you are trying to achieve.

Anyhow, this is a Touched function that will check in what state it is. You can sort of ‘manually’ update the requirements for which function will play.

Script location.

image

local part = script.Parent

local debounce = false

part.Touched:Connect(function()
	if debounce == false then
		if part.BrickColor.Name == "Lime green" then   --//original state of the part
			debounce = true
                 --//[any function to run]
			part.BrickColor = BrickColor.new("Bright purple")   --//'updating' the :Touched function
			print("Part turned purple")
		elseif
			part.BrickColor.Name == "Bright purple" then   --//'updated' state of the part
			debounce = true
                 --//[any function to run]
			part.BrickColor = BrickColor.new("Lime green")   --//'resetting' the :Touched function
			print("Part turned green")
		else 
			return warn("part.BrickColor.Name is unable to be found.")
		end
		task.wait(3)
		debounce = false
		print("Debounce set to ", debounce)
	end
end)
1 Like

If you want to take a whole new function, make a MoudleScript and add a function inside of it, then on the main script, take that function from the ModuleScript using require(). please tell me if that is youre case, else if you want to repeat the function:

local throwed = false

script.Parent.Touched:Connect(function(hit)
       if not throwed then
          throwed = true
          
         -- you're code here

        wait(1) -- change if needed
        throwed = false
end)

I did not try this code, but im still sure it will work.

1 Like

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