Debounce getting overwritten when it shouldn't

I’m trying to make an Upgrade that can be bought when a player clicks 1 and has 25 or more coins

The problem is the debounce for some reason gets overwritten when it shouldn’t

				local hasBeenPurchased = false

I’ve tried to fix it for the better part of an hour now.

Code segment:

			--Coin Upgrades
			if input.KeyCode == Enum.KeyCode.One then
				local hasBeenPurchased = false
				local cost = 25
				print(hasBeenPurchased)
				if hasBeenPurchased == false then
					if coins >= cost then
						print("Upgraded (1)")
						
						hasBeenPurchased = true
						print(hasBeenPurchased)
						script.Purchase.Playing = true
						coins -= cost
						
						COOLDOWN -= 0.25
					else
						script.NotEnough.Playing = true
					end
				end

Full code:

-- Services
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")


-- Variables
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local COOLDOWN = 0.5
local GIVE_AMOUNT = 1
local sprint = 36
local walk = 24
local coins = 0


-- Coins
mouse.Button1Down:Connect(function()
	local isPressed = false
	if isPressed == false then
		isPressed = true
		local luckyNumber = math.random(1,50)

		local function Lucky()
			coins += 10
		end
		if luckyNumber == 1 then
			Lucky()
			print("You got lucky!")
		end

		coins += GIVE_AMOUNT
		script.ClickSound.Playing = true
		print(coins)

		wait(COOLDOWN)
		isPressed = false
	end
end)


-- Controls
UserInputService.InputBegan:Connect(function(input, gameProssesedEvent)
	if not gameProssesedEvent then
		if UserInputService.KeyboardEnabled and UserInputService.MouseEnabled then
			-- Sprint
			if input.KeyCode == Enum.KeyCode.LeftShift then
				local character = player.Character
				character:FindFirstChild("Humanoid").WalkSpeed = sprint
				UserInputService.InputEnded:Connect(function(input)
					if input.KeyCode == Enum.KeyCode.LeftShift then
						character:FindFirstChild("Humanoid").WalkSpeed = walk
					end
				end)
			end
			-- Spawn fading color changing parts
			if input.KeyCode == Enum.KeyCode.Z then
				local part = Instance.new("Part", workspace)
				for times = 1,10,1 do
					wait(0.1)
					part.Transparency += 0.1
					part.Color = Color3.new(math.random(0,255), math.random(0,255), math.random(0,255))
				end
				part:Destroy()
			end
			
			--Coin Upgrades
			if input.KeyCode == Enum.KeyCode.One then
				local hasBeenPurchased = false
				local cost = 25
				print(hasBeenPurchased)
				if hasBeenPurchased == false then
					if coins >= cost then
						print("Upgraded (1)")
						
						hasBeenPurchased = true
						print(hasBeenPurchased)
						script.Purchase.Playing = true
						coins -= cost
						
						COOLDOWN -= 0.25
					else
						script.NotEnough.Playing = true
					end
				end
			elseif input.KeyCode == Enum.KeyCode.Two then
				local hasBeenPurchased = false
				local cost = 100
				print(hasBeenPurchased)
				if hasBeenPurchased == false then
					if coins >= cost then
						print("Upgraded (2)")

						hasBeenPurchased = true
						print(hasBeenPurchased)
						script.Purchase.Playing = true
						coins -= cost

						GIVE_AMOUNT += 1
					else
						script.NotEnough.Playing = true
					end
				end
			end
		end
	end
end)

It’s because you set IsPressed to false every time you press the Mouse button. You need to set the variable outside the function and check it inside the function.

You are changing the denounce to false each time the function is ran like here:

Put it outside the function then it should work

2 Likes

Yeah that seemed the fix the problem