Best way to debounce?

U can have a counter variable and increase it by 1 each upgrader touched
Here is some code considering u have the upgraders in an arrangement of e.g “Upgrader1”,“Upgrader2” and so on

local DropItem = script.Parent

local MainString = "Upgrader"
local counter = 1

DropItem.Touched:Connect(function(hit)
	if hit.Name == MainString..counter then
		counter +=1
		print("Touched")
	end
end)

this script can give debounce for each part i guess
(put this script in detector)

local hits = {}
local detector = script.Parent
detector.Touched:Connect(function(hit)
if hit.Parent.Name == “dropped item” and not hits[hit.Parent] then
hits[hit.Parent] = true
—your script here
end
end)

im sorry if its not what you want

Hi Everyone, Sorry I have been extremely busy (just replying now so the topic does not auto close). However I have almost completed the script and would love to hear some critiques once I post it!

Hi again, I have finally created the debounce. This model I have linked below will work for normal scripts whilst I still have to make edits to finish the OOP version/module script.


Demo of the debounce - Red, purple, and blue bricks are Part1, 2, and 3 respectively.

Applications

This debounce will work in situations where there is multiple parts passing through many touch detectors/upgraders. It also works fine with single parts and single detectors.

Setup

To setup the debounce for your own follow these steps:

  1. Substitute my variables for your own.
    a) Decide on how many touch detectors you need (I have 4 in my demonstration) but you can delete or add however you see fit.
    b) Change the touch detector variables’ name to that of your upgraders.
    c) Specify the touch detectors directory (optional: use waitforchild())
    d) Decide on how many parts you want to be detected and substitute their names into the code.

  2. Specify db_Time or the debounce cooldown time in seconds. The time between deboucing always stays the same so make sure one debounce interval works for all detectors.

  3. On the part(s) you want detect create 1 or 2 attributes depending on your use:
    a) A “Debounce” boolean set to false.
    b) FOR TYCOONS: A “Worth” number containing the original value of the drop (if you are changing something different instead of worth simply change this second attribute to whatever it is you desire to be debounced).

  4. Enter the directory of the parts you want to be detected inside the filterObjects table.

  5. If you want to view the debounce in action, uncomment the print statements.

Code

local TouchDetector = workspace:FindFirstChild("Test").Detectors.TouchDetector
local TouchDetector2 = workspace:FindFirstChild("Test").Detectors.TouchDetector2
local TouchDetector3 = workspace:FindFirstChild("Test").Detectors.TouchDetector3
local TouchDetector4 = workspace:FindFirstChild("Test").Detectors.TouchDetector4


local db_Time = 0.5

local filterObjects = {workspace.Test.Part2, workspace.Test.Part1, workspace.Test.Part3}

local params = OverlapParams.new()
params.FilterType = Enum.RaycastFilterType.Whitelist -- WHITELIST MAKES OVERLAP PARAMS IGNORE ALL OBJECTS EXCEPT THOSE IN THE FILTER OBJECTS (BLACKLIST DOES THE OPPOSITE)
params.FilterDescendantsInstances = filterObjects


local TouchDetectors = {}


while TouchDetectors do task.wait()
	local TouchDetectors = {
		touchingList = workspace:GetPartsInPart(TouchDetector, params),
		touchingList2 = workspace:GetPartsInPart(TouchDetector2, params),
		touchingList3 = workspace:GetPartsInPart(TouchDetector3, params),
		touchingList4 = workspace:GetPartsInPart(TouchDetector4, params)
	}

	for i,v in pairs(TouchDetectors) do
		for i,v in pairs(v) do
			local debounce = v:GetAttribute("Debounce")
			if not debounce then
				--print(i, v)
				local worth = v:GetAttribute("Worth")
				v:SetAttribute("Worth", worth * 2)
				--print(v:GetAttribute("Worth"))
				v:SetAttribute("Debounce", true)

			else
				--print("Debounce off fired")
				task.wait(db_Time)
				v:SetAttribute("Debounce", false)

			end
			
		end
	end
end

View the full demonstration at:

I hope this helps everyone as there is suprisingly little knowledge in such an important aspect of development. Also please credit me if you use this, thanks :smile:

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