Simulator Sell Part Is Selling 2 times at a row

Well, I Don’t Really Know How To Explain it but i will try my best.
so i am trying to make a simulator game and while working on the sell part
and testing it, i realized that it sells 2 times in a row even though there is no error in the output.

  1. What do you want to achieve? Making A Simulator Sell Part

  2. What is the issue? Sell Part Sells 2 times in a row.

local Part = script.Parent
Part.Touched:Connect(function(HIT)
	local f = HIT.Parent:FindFirstChild("Humanoid")
	if f then
		local player = game.Players:GetPlayerFromCharacter(HIT.Parent)
		if player then
			local leaderstats = player:WaitForChild("leaderstats")
			local currency = leaderstats.Coins
			local selling = leaderstats.Swings
			if selling.Value > 0 then
				currency.Value = currency.Value + selling.Value * 1
				selling.Value = 0
			end
		end
	end
end)

Your touched event is activated more than once, so you will have to implement a debounce to stop it from doing anything after it has been touched for a certain period of time.

May require Debounce technique

local Part = script.Parent
local debounce = false
Part.Touched:Connect(function(HIT)
    if not debounce then
        debounce = true

        local f = HIT.Parent and HIT.Parent:FindFirstChild("Humanoid")
        if f then
            local player = game.Players:GetPlayerFromCharacter(HIT.Parent)
            if player then
                local leaderstats = player:WaitForChild("leaderstats")
                local currency = leaderstats.Coins
                local selling = leaderstats.Swings
                if selling.Value > 0 then
                    currency.Value = currency.Value + selling.Value * 1
                    selling.Value = 0
                end
            end
        end

        debounce = false
    end
end)
1 Like

its not because of the debounce it just sells 2 times in a row
well, this is what it looks like.
Click Here

I recommend adding a yield to the debounce. You should ask the script to pause with a wait() before re-enabling the sell part.

wait(1)
debounce = false
1 Like

I’ve Tried That Before But It Still Doesn’t Work

Try to put a print after if selling.Value… and check how much it prints

This is because of your debounce. It doesn’t trigger the touched event once, but multiple times when you walk over it. If you do not want this, end the debounce whenever the player steps off the sell button.-

Didn’t I Already Tell You? It’s Not Because Of The Debounce, I Tried Using Debounce A Lot Of Time But It Just Doesn’t Work.

local Part = script.Parent

local HitPlayers = {}
local TimeBetweenSells = 0.1

Part.Touched:Connect(function(HIT)
	local humanoid = HIT.Parent:FindFirstChild("Humanoid")
	if humanoid then
		local player = game.Players:GetPlayerFromCharacter(HIT.Parent)
		
		if player then
			-- check the time
			-- make sure they cant sell too quickly
			if HitPlayers[player] and os.clock() - HitPlayers[player] < TimeBetweenSells then
				return
			end
			
			HitPlayers[player] = os.clock()
			
			local leaderstats = player:WaitForChild("leaderstats")
			local currency = leaderstats.Coins
			local selling = leaderstats.Swings
			
			if selling.Value > 0 then
				currency.Value += selling.Value 
				selling.Value = 0
			end
		end
	end
end)

This uses a different type of denouncing. Let me know if this works for you

Really Sorry. It Was Because Of The Gamepass Which Gives 2 times currency