Touched function is going through multiple times even with a debounce

I am trying to get the touched function to only go through once and a debounce stop it from going again. Even with the debounce in place, I can’t figure out why it goes through multiple times. I have tried moving the debounce before some of the parts and even increasing the length of the debounce as much as possible.

hitbox.Touched:Connect(function(hit)
	local folder = hit:FindFirstAncestorOfClass("Folder")
	if folder and folder.Name == "Cars" then
		local body = hit:FindFirstAncestor("Body")
		if body then
			local seat = body.Parent:FindFirstChild("DriveSeat")
			if seat then
				local plr = game.Players:GetPlayerFromCharacter(seat.Occupant.Parent)
				if not debounces.Group2[plr.UserId] and currentlights.Group2 == "Red" then
					debounces.Group2[plr.UserId] = true
					coroutine.resume(coroutine.create(function()
						task.wait((redt + greent) - (tick() - redtimelight.Group2))
						debounces.Group2[plr.UserId] = nil
					end))

					plr.RedLightFolder.RedLights.Value += 1

					local TS = game:GetService("TweenService")
					local flash = plr.PlayerGui.FlashGUI.Flash
					local ints = TS:Create(flash, TweenInfo.new(0.25), {["Transparency"] = 0})
					local outts = TS:Create(flash, TweenInfo.new(0.25), {["Transparency"] = 1})

					coroutine.resume(coroutine.create(function()
						ints:Play()
						ints.Completed:Wait()
						outts:Play()
					end))
				end
			end
		end
	end
end)
1 Like

Is debounces.Group2 a folder or a table?

Debounces.Group2 is a table outside of the function.

Maybe your task.wait is incorrect and is actually 0 seconds?

no, it would atleast end up at 13 seconds and even if it was 0 seconds, the function wouldn’t go into place as

this would return false.
Here’s another part of the script that would help explain a bit:

local currentlights = {
	["Group1"] = "Green",
	["Group2"] = "Red"
}

local redtimelight = {
	["Group1"] = 0,
	["Group2"] = tick()
}

local greent = 13
local yellowt = 2
local redt = 13

function changecolor(color, light)
	local TrafficLight = light
	local RedLight = TrafficLight.RedLight
	local YellowLight = TrafficLight.YellowLight
	local GreenLight = TrafficLight.GreenLight
	local RedPointLight = RedLight.PointLight
	local YellowPointLight = YellowLight.PointLight
	local GreenPointLight = GreenLight.PointLight
	
	local group
	
	if table.find(group1, light.Parent) then
		group = "Group1"
	elseif table.find(group2, light.Parent) then
		group = "Group2"
	end
	
	if color == "Green" then
		currentlights[group] = color
		RedLight.Transparency = 0.8
		RedPointLight.Enabled = false
		GreenLight.Transparency = 0
		GreenPointLight.Enabled = true
	elseif color == "Yellow" then
		currentlights[group] = color
		GreenLight.Transparency  = 0.8
		GreenPointLight.Enabled = false
		YellowLight.Transparency = 0
		YellowPointLight.Enabled = true
	elseif color == "Red" then
		currentlights[group] = color
		redtimelight[group] = tick()
		YellowLight.Transparency = 0.8
		YellowPointLight.Enabled = false
		RedLight.Transparency = 0
		RedPointLight.Enabled = true
	end
end

Isn’t the debounce itself supposed to be set as just true or false?
Doesn’t setting debounces.Group2[plr.UserId] = nil go against that?

Also, why would you have the debounce reference more than 1 player? If a player is in the VehicleSeat couldn’t you just turn the CanTouch Property off while they’re in the seat to get rid of the second player touch affecting the lights?

The debounce sets it to nil as to not waste memory, and I would have it reference more than 1 player because multiple people could be doing it at or near the same time. If it set the CanTouch property to false, the second player touching it wouldn’t be affected making it quite useless.

To people trying to answer this, read this first.

1: This touch function is regarding Traffic Lights

When a user dashes a traffic lights while red, a flash UI will appear. However, the UI will appear multiple times, sometimes 5 - 6 times. @Kaid3n22 is trying to make it to only appear 1 time and not more whenever 1 user dashes the red light.

2: The debounce time can defer.

After much communication, Kaid3n22 has said that debounce will be around 0seconds - 13seconds.

3: What do we want?

We want the traffic lights to work so that, when a player dashes the red light, the UI only appears once, not twice or more.