RBXScriptSignal repeats twice [Button1Up]

I’m making some code right now for a selection system and it isn’t working.

The events print and duplicate twice, despite me adding a debounce. This immediately gets rid of the highlight and the debounce also breaks for some reason.

local debouncev = false

local function debounce()
	task.wait(0.2)
	debounce = false
	print("Debounced")
end


mouse.Button1Up:Connect(function()
	local target = mouse.Target
	if target and debouncev == false then
		debouncev = true
		local ctrlDown = game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.LeftControl)
		local altDown = game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.LeftAlt)
		if altDown then
			print("rr")
			debounce()
		else
			if target:FindFirstChild("CreateHighlight") then
				target:FindFirstChild("CreateHighlight"):Destroy()
				debounce()
			else
				if #selectedParts > 0 then
					selectedParts = {}
				end
				table.insert(selectedParts, target:GetFullName())
				local highlightClone = highlightPath:Clone()
				highlightClone.Parent = target
				print(highlightClone:GetFullName())
				print(selectedParts)
				debounce()
			end
		end
	end
end)

I’ve tried adding a debounce and such, but it didn’t work.

I’m not sure about the signal firing twice, but there is a flaw in your debounce.

In

local function debounce()
	task.wait(0.2)
	debounce = false
	print("Debounced")
end

I believe you meant to do

debouncev = false

This is because of this function:

local function debounce()
	task.wait(0.2)
	debounce = false
	print("Debounced")
end

According to this logic, you are setting the function itself to a boolean which is causing the bug. (As I suspect.)

A better way to write this will be:

local function debounce()
	task.delay(0.2,function()
		debounce = false
	end)
end

I added a task.delay so that, there won’t be any delay after you run the function.

Hi, while the function doesn’t repeat twice now, after the debounce I can’t click at all.

local function debounce()
	task.delay(0.2,function()
		debounce = false
	end)
end


mouse.Button1Up:Connect(function()
	local target = mouse.Target
	if target and debouncev == false then
		debouncev = true
		local ctrlDown = game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.LeftControl)
		local altDown = game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.LeftAlt)
		if altDown then
			print("rr")
			debounce()
		else
			if target:FindFirstChild("CreateHighlight") then
				target:FindFirstChild("CreateHighlight"):Destroy()
				debounce()
			else
				if #selectedParts > 0 then
					selectedParts = {}
				end
				table.insert(selectedParts, target:GetFullName())
				local highlightClone = highlightPath:Clone()
				highlightClone.Parent = target
				print(highlightClone:GetFullName())
				print(selectedParts)
				debounce()
			end
		end
	end
end)

Oh my, I made a mistake! Apologies!!

local function debounce()
	task.delay(0.2,function()
		debouncev = false
	end)
end
1 Like

Ok, so it sort of half works and half doesnt [and sorry about not realising about debouncev].

This is attached to a gear in a PlayerGui, and I have to retoggle the gear to be able to use the function again, because despite the debounce it will still report itself true [even after changing debouncev].

image

Can you show the entire script? I am having issues understand the problem here.

[Handler is the script]
image

local player = game.Players.LocalPlayer
local highlightPath = player.Character:FindFirstChild("Create"):FindFirstChild("CreateHighlight")
local mouse = player:GetMouse()
local selectedParts = {}

local function updatehighlight()
	for _, part in pairs(selectedParts) do
		if part and part:FindFirstChild("CreateHighlight") then
			local beam = part:FindFirstChild("CreateHighlight")
			if script.Parent.CurrentTool.Value == 0 then
				beam.OutlineColor = Color3.new(1, 1, 1)
			elseif script.Parent.CurrentTool.Value == 1 then
				beam.OutlineColor = Color3.new(1, 1, 0)
			elseif script.Parent.CurrentTool.Value == 2 then
				beam.OutlineColor = Color3.new(0.576471, 1, 0.12549)
			elseif script.Parent.CurrentTool.Value == 3 then
				beam.OutlineColor = Color3.new(0.0431373, 1, 0.937255)
			elseif script.Parent.CurrentTool.Value == 4 then
				beam.OutlineColor = Color3.new(1, 0, 0)
			elseif script.Parent.CurrentTool.Value == 5 then
				beam.OutlineColor = Color3.new(0.635294, 0.0470588, 1)
			elseif script.Parent.CurrentTool.Value == 6 then
				beam.OutlineColor = Color3.new(0.6, 0.6, 0.6)
			elseif script.Parent.CurrentTool.Value == 7 then
				beam.OutlineColor = Color3.new(0, 0, 0)
			elseif script.Parent.CurrentTool.Value == 8 then
				beam.OutlineColor = Color3.new(1, 0.584314, 0)
			elseif script.Parent.CurrentTool.Value == 9 then
				beam.OutlineColor = Color3.new(1, 1, 1)
			end
		end
	end
end

script.Parent.CurrentTool.Changed:Connect(function()
	updatehighlight()
end)

local function resetTransparency()
	local tools = {
		"MoveTool", "RotateTool", "AddTool", "AnchorTool",
		"EffectTool", "MaterialTool", "PaintTool", "PartSettingsTool", "ScaleTool"
	}
	for _, toolName in ipairs(tools) do
		local tool = script.Parent.BackGround:FindFirstChild(toolName)
		if tool then
			tool.ImageTransparency = 0
		end
	end
end

script.Parent.BackGround.MoveTool.MouseButton1Click:Connect(function()
	script.Parent.CurrentTool.Value = 1
	resetTransparency()
	updatehighlight()
	script.Parent.BackGround.MoveTool.ImageTransparency = 0.5
end)

script.Parent.BackGround.RotateTool.MouseButton1Click:Connect(function()
	script.Parent.CurrentTool.Value = 2
	resetTransparency()
	updatehighlight()
	script.Parent.BackGround.RotateTool.ImageTransparency = 0.5
end)

script.Parent.BackGround.AddTool.MouseButton1Click:Connect(function()
	script.Parent.CurrentTool.Value = 9
	resetTransparency()
	updatehighlight()
	script.Parent.BackGround.AddTool.ImageTransparency = 0.5
end)

script.Parent.BackGround.AnchorTool.MouseButton1Click:Connect(function()
	script.Parent.CurrentTool.Value = 7
	resetTransparency()
	updatehighlight()
	script.Parent.BackGround.AnchorTool.ImageTransparency = 0.5
end)

script.Parent.BackGround.EffectTool.MouseButton1Click:Connect(function()
	script.Parent.CurrentTool.Value = 8
	resetTransparency()
	updatehighlight()
	script.Parent.BackGround.EffectTool.ImageTransparency = 0.5
end)

script.Parent.BackGround.MaterialTool.MouseButton1Click:Connect(function()
	script.Parent.CurrentTool.Value = 5
	resetTransparency()
	updatehighlight()
	script.Parent.BackGround.MaterialTool.ImageTransparency = 0.5
end)

script.Parent.BackGround.PaintTool.MouseButton1Click:Connect(function()
	script.Parent.CurrentTool.Value = 4
	resetTransparency()
	updatehighlight()
	script.Parent.BackGround.PaintTool.ImageTransparency = 0.5
end)

script.Parent.BackGround.PartSettingsTool.MouseButton1Click:Connect(function()
	script.Parent.CurrentTool.Value = 6
	resetTransparency()
	updatehighlight()
	script.Parent.BackGround.PartSettingsTool.ImageTransparency = 0.5
end)

script.Parent.BackGround.ScaleTool.MouseButton1Click:Connect(function()
	script.Parent.CurrentTool.Value = 3
	resetTransparency()
	updatehighlight()
	script.Parent.BackGround.ScaleTool.ImageTransparency = 0.5
end)

local debouncev = false

local function debounce()
	local function debounce()
		task.delay(0.2,function()
			debouncev = false
		end)
	end
end


mouse.Button1Up:Connect(function()
	print(debouncev)
	local target = mouse.Target
	if target and debouncev == false then
		debouncev = true
		local ctrlDown = game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.LeftControl)
		local altDown = game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.LeftAlt)
		if altDown then
			print("rr")
			debounce()
		else
			if target:FindFirstChild("CreateHighlight") then
				target:FindFirstChild("CreateHighlight"):Destroy()
				debounce()
			else
				if #selectedParts > 0 then
					selectedParts = {}
				end
				table.insert(selectedParts, target:GetFullName())
				local highlightClone = highlightPath:Clone()
				highlightClone.Parent = target
				print(highlightClone:GetFullName())
				print(selectedParts)
				debounce()
			end
		end
	end
end)

Okayyyyyy, sooo:

mouse.Button1Up:Connect(function()
	print(debouncev)
	local target = mouse.Target
	if target and debouncev == false then
		debouncev = true
		local ctrlDown = game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.LeftControl)
		local altDown = game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.LeftAlt)
		if altDown then
			print("rr")
		else
			if target:FindFirstChild("CreateHighlight") then
				target:FindFirstChild("CreateHighlight"):Destroy()
			else
				if #selectedParts > 0 then
					selectedParts = {}
				end
				table.insert(selectedParts, target:GetFullName())
				local highlightClone = highlightPath:Clone()
				highlightClone.Parent = target
				print(highlightClone:GetFullName())
				print(selectedParts)
			end
		end
		debounce()
	end
end)

Only change I made is to make sure debounce ends.

Hi, I fixed the issue by changing it to a physical BoolValue instead of specifying it into my script, and by adding sanity checks.

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