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)
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)
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
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)
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.
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)
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)