Why is this event firing twice?

Hello! so basically I had this same error yesterday, and adding a debounce fixed the code only the first time a player tried to sell, then the second time the debounce stopped working and the event .Completed would fire twice again
Here’s the code:

    local debounce = false
    barTween:Play()
	barTween.Completed:Connect(function()
			if debounce then
				debounce = false
				task.wait(3)
				return
			end
			debounce = true
			if sellingBar.Size == UDim2.new(1, 0, 1, 0) then
				holdEnd()
				print("Sold for "..player.Multiplier.Value.."X value")
				print("Revenue: "..math.ceil(player.backpack.backpackStorage.Value * player.Multiplier.Value))
				sellRemote:FireServer()
				sellingBarOuter.Visible = false
				barTween:Cancel()
				sellingBar.Size = UDim2.new(0.05, 0, 1, 0)
			end
		end)
1 Like

Can you share the whole code? Because usually if it fires twice is because you put events in an event function.

Tool.Equipped:Connect(function()
      Tool.Activated:Connect() -- will fire twice
end)

You can still put events in events I think, but you need to do something with connections. But for me, I put them seperately to avoid this problem.

1 Like

Sure, The code is a mess though, so I apologize for that x)

local players = game.Players
local player = players.LocalPlayer
local userInput = game:GetService("UserInputService")
local repStorage = game.ReplicatedStorage
local remotes = repStorage.Remotes
local bindables = repStorage.Bindables
local sellRemote = remotes:FindFirstChild("Sell")
local holdDown = remotes:FindFirstChild("SellHoldDown")
local holdDownEnd = remotes:FindFirstChild("SellHoldDownEnd")

local character = player.Character or player.CharacterAdded:Wait()
player.character:WaitForChild("HumanoidRootPart")

local tweenService = game:GetService("TweenService")

local nothingToSell = script.Parent.MainGUI.SellingText
local sellingBar = script.Parent.MainGUI.SellingBar.SellingBarOutline.SellingBarInner
local sellingBarOuter = sellingBar.Parent.Parent

local character = player.Character or player.CharacterAdded:Wait()

local goalSize = {}
goalSize.Size = UDim2.new(1, 0, 1, 0)
local barTween = tweenService:Create(
	sellingBar,
	TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut),
	goalSize
)

local function hold()

	if player.backpack.backpackStorage.Value >= 1 then
		holdDown:FireServer()
	else
		nothingToSell.Visible = true
	end
end

local function holdEnd()
	holdDownEnd:FireServer()
end

player.sellOnDistance:GetPropertyChangedSignal("Value"):Connect(function()
	if player.sellOnDistance.Value == false then
		holdEnd()
		nothingToSell.Visible = false
	end
end)

player.selling:GetPropertyChangedSignal("Value"):Connect(function() --checks if the player is pressing down or not
	if player.selling.Value == true then --if player is holding down then
		print("player is selling")
		sellingBarOuter.Visible = true
		local debounce = false
		barTween:Play()
		barTween.Completed:Connect(function()
			if debounce then
				debounce = false
				task.wait(3)
				return
			end
			debounce = true
			if sellingBar.Size == UDim2.new(1, 0, 1, 0) then
				holdEnd()
				print("Sold for "..player.Multiplier.Value.."X value")
				print("Revenue: "..math.ceil(player.backpack.backpackStorage.Value * player.Multiplier.Value))
				sellRemote:FireServer()
				sellingBarOuter.Visible = false
				barTween:Cancel()
				sellingBar.Size = UDim2.new(0.05, 0, 1, 0)
			end
		end)
		player.Character:WaitForChild("Humanoid").Died:Connect(function()
			holdEnd()
		end)
	else -- if player is no longer holding down then
		print("player is no longer selling")
		sellingBarOuter.Visible = false
		barTween:Cancel()
		sellingBar.Size = UDim2.new(0.05, 0, 1, 0)
	end
end)

userInput.InputBegan:Connect(function(input) --pressing
	local inputType = input.UserInputType
	if inputType == Enum.UserInputType.MouseButton1 and player.sellOnDistance.Value == true then
		hold()
	end
end)

userInput.InputEnded:Connect(function(input) --no longer pressing
	local inputType = input.UserInputType
	if inputType == Enum.UserInputType.MouseButton1 and player.sellOnDistance.Value == true then
		holdEnd()
	end
end)

And here’s the remotes:

sell.OnServerEvent:Connect(function(Player)
	Player["Coins"].Value += math.ceil(Player.backpack.backpackStorage.Value * Player.Multiplier.Value)
	Player.backpack.backpackStorage.Value = 0
	Player.leaderstats["Potions🧪"].Value += 1
end)

holdDown.OnServerEvent:Connect(function(Player)
	if Player.Character:FindFirstChild("Humanoid").Health >= 1 then
		Player.selling.Value = true
		if not Player.Character.UpperTorso:FindFirstChild("Beam") then
			local attachmentStart = Instance.new("Attachment")
			attachmentStart.Name = "attachmentStart"
			attachmentStart.Orientation = Vector3.new(0, -90, 180)
			attachmentStart.Position = Vector3.new(0, 0, 0)
			attachmentStart.Parent = Player.Character.UpperTorso
			local attachmentEnd = Instance.new("Attachment")
			attachmentEnd.Name = "attachmentEnd"
			attachmentEnd.Orientation = Vector3.new(-25, 180, 180)
			attachmentEnd.Position = Vector3.new(0, 0, 0)
			attachmentEnd.Parent = workspace.Cauldron.Attachments
			local Beam = Instance.new("Beam")
			Beam.FaceCamera = true
			Beam.CurveSize1 = -6
			Beam.CurveSize0 = 4
			Beam.Width1 = 1.5
			Beam.Width0 = 1.5
			Beam.Transparency = NumberSequence.new(0, 0)
			Beam.Texture = "rbxassetid://0"
			Beam.TextureLength = 1.5
			Beam.Attachment0 = attachmentStart
			Beam.Attachment1 = attachmentEnd
			Beam.Parent = Player.Character.UpperTorso
		else
			Player.Character.UpperTorso.Beam.Enabled = true
		end
	end
end)

holdDownEnd.OnServerEvent:Connect(function(Player)
	if Player.Character.UpperTorso:FindFirstChild("Beam") then
		Player.selling.Value = false
		Player.Character.UpperTorso.Beam.Enabled = false
	end
end)
1 Like

and

Put them outside of the getpropertychangedsignal event separately. Since you only want to fire when the player is holding down then you can put these if statements at the very top.

if player.selling.Value == true then -- put this on top of the events above

if player.selling.Value == false then return end --  alternative if statement you can try 

I don’t think I understood what you said here

Put them out of the event listener above

So it will be like this

player.selling:GetPropertyChangedSignal("Value"):Connect(function() --checks if the player is pressing down or not
	if player.selling.Value == true then --if player is holding down then
		print("player is selling")
		sellingBarOuter.Visible = true
		local debounce = false
		barTween:Play()
	else -- if player is no longer holding down then
		print("player is no longer selling")
		sellingBarOuter.Visible = false
		barTween:Cancel()
		sellingBar.Size = UDim2.new(0.05, 0, 1, 0)
	end
end)

barTween.Completed:Connect(function()
    if player.selling.Value == false then return end
	if debounce then
		debounce = false
		task.wait(3)
		return
	end
	debounce = true
	if sellingBar.Size == UDim2.new(1, 0, 1, 0) then
	    holdEnd()
	    print("Sold for "..player.Multiplier.Value.."X value")
	    print("Revenue: "..math.ceil(player.backpack.backpackStorage.Value * player.Multiplier.Value))
		sellRemote:FireServer()
		sellingBarOuter.Visible = false
		barTween:Cancel()
		sellingBar.Size = UDim2.new(0.05, 0, 1, 0)
	end
end)

player.Character:WaitForChild("Humanoid").Died:Connect(function()
    if player.selling.Value == false then return end
	holdEnd()
end)

I got that part, but outside the listener separately?
and why would I put those if statements at the very top? I’m confused sorry :frowning:

1 Like

Those if statements are there because you put the .Died and .Completed event inside the
“if player.selling.Value == true then” code block.

So basically what it means is that you want the .Died and .Completed event to fire only if the player is holding down. If you don’t put the if statements then I think it would still be fine. Up to you.

1 Like

but shouldn’t I put the if statements inside the functions?
like

barTween.Completed:Connect(function()
	if player.selling.Value == true then
		if sellingBar.Size == UDim2.new(1, 0, 1, 0) then
			holdEnd()
			print("Sold for "..player.Multiplier.Value.."X value")
			print("Revenue: "..math.ceil(player.backpack.backpackStorage.Value * player.Multiplier.Value))
			sellRemote:FireServer()
			sellingBarOuter.Visible = false
			barTween:Cancel()
			sellingBar.Size = UDim2.new(0.05, 0, 1, 0)
		end
	end
end)
---------------------------------
player.Character:WaitForChild("Humanoid").Died:Connect(function()
	if player.selling.Value == true then
		holdEnd()
	end
end)
1 Like

Yes this is exactly how I meant it should be.

Oh wait, did you think I meant at the top of the script earlier? Hahaha sorry

2 Likes

yeah that’s why I got confused haha '^^,
Also this way it works, but still fires the .completed twice

nevermind! I put the debounce inside like this

barTween.Completed:Connect(function()
local debounce = false
	if debounce then
		debounce = false
		task.wait(3)
		return
	end
	if player.selling.Value == true then
		debounce = true
		if sellingBar.Size == UDim2.new(1, 0, 1, 0) then
			holdEnd()
			print("Sold for "..player.Multiplier.Value.."X value")
			print("Revenue: "..math.ceil(player.backpack.backpackStorage.Value * player.Multiplier.Value))
			sellRemote:FireServer()
			sellingBarOuter.Visible = false
			barTween:Cancel()
			sellingBar.Size = UDim2.new(0.05, 0, 1, 0)
		end
	end
end)

putting it outside like this worked!

local debounce = false
barTween.Completed:Connect(function()
	if debounce then
		debounce = false
		task.wait(3)
		return
	end
	if player.selling.Value == true then
		debounce = true
		if sellingBar.Size == UDim2.new(1, 0, 1, 0) then
			holdEnd()
			print("Sold for "..player.Multiplier.Value.."X value")
			print("Revenue: "..math.ceil(player.backpack.backpackStorage.Value * player.Multiplier.Value))
			sellRemote:FireServer()
			sellingBarOuter.Visible = false
			barTween:Cancel()
			sellingBar.Size = UDim2.new(0.05, 0, 1, 0)
		end
	end
end)
2 Likes