Make a part only be touchable once

  1. What do you want to achieve? Make a part only be touchable once.

  2. What is the issue? Include screenshots / videos if possible!
    My part has a weird mesh that always touches the Collector 2 times even tho i delete it right away after the touch.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried working with Attributes

Here is my onTouched function in the Collector:

function Collector:OnTouched(hitPart)
	local worth = hitPart:GetAttribute("Worth")
	if worth then
		print(worth)
		hitPart:Destroy()
	end
end

This will fire 2 times if the part touches it.

Try this

local Collected = false

function Collector:OnTouched(hitPart)
if Collected == false then
	local worth = hitPart:GetAttribute("Worth")
	if worth then
Collected = true
		print(worth)
		hitPart:Destroy()
	end
end
end

This works, but I want it so there can be another part after this that gets collected

local Debounce = false

function Collector:OnTouched(hitPart)
if Debounce == false then
	local worth = hitPart:GetAttribute("Worth")
	if worth then
Debounce = true
		print(worth)
		hitPart:Destroy()
task.spawn(function()
task.wait(0.1)
Debounce = false
end)
	end
end
end

Try something like this

Thank you that works, but why do you use task.spawn?

It continues doing whatever without doing anything to stop the script (as in task.wait() will only happen inside of the task), however I do not recommend using multiple, one will do fine.

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