Debounce not working properly?

Hello!

I’m working on a pad that once a player steps on the pad, they receive a value with a maximum of 4. But instead, I was getting anywhere from 16,000-3,000,000 so, I looked up Debounce, And I added it into my script so hoping that it would fix my problem, but, now I’m not receiving anything in my Crates in my Warehouses Folder. If you’re able to help, thank you. If not have a great day!

local PLAYERDATA 	= game.ReplicatedStorage.PlayerData
local Remotes 		= game:GetService("ReplicatedStorage").Remotes
local PLAYERS 		= game:GetService("Players")
local debounce 		= false


script.Parent.Touched:Connect(function(Part)
	if Part.Parent:FindFirstChild("Humanoid") then
		debounce = true
		local Player = game.Players:GetPlayerFromCharacter(Part.Parent)
		if Player then		
			local CrateInfo = PLAYERS:WaitForChild(Player.Name):WaitForChild("Crates")
			local CrateAmnt = PLAYERDATA:FindFirstChild(Player.Name):WaitForChild("Warehouses"):WaitForChild("Crates")

			local Data = PLAYERDATA:FindFirstChild(Player.Name)
			local Warehouses = Data:WaitForChild("Warehouses")
			local Stats = Data:WaitForChild("Stats")
			local Plr = game.Players:FindFirstChild(Part.Parent.Name)

			-- fire event with gui to appear when touched showing collected crates and money earned
			if CrateInfo.TypeOfCrate.Amount.Value >= 1 then
				Remotes.showGui:FireClient(Player, "ShowCrates")
				wait(5)
				local model	 		= workspace.WarehouseClaim
				local Crates		= workspace.Crates

				model:SetPrimaryPartCFrame(CFrame.new(468.064, 7.446, -265.517))


				-- crate value handler
				Warehouses.Crates.Value = Warehouses.Crates.Value + CrateAmnt.Value 


				if Warehouses.Crates.Value == Warehouses.Max.Value then
					print("player cannot carry more crates, they must sell them")
				end

				debounce = false

				for _,v in pairs(PLAYERS:WaitForChild(Player.Name):GetChildren()) do
					if v:IsA("Folder") and v.Name == "Crates" then
						v:Destroy()
					end
				end

			else
				print("player does not have more than 1 crate to sell")

			end
		end
	end
end)

You need to run a check to see if debounce if debounce is true, and if it is, then and only then should you run the rest of your code. The issue with this program is that even though the state of the debounce is being set, you aren’t doing anything with that information as of right now. If you need any more help/clarification, please let me know.

So, right before my debounce = true on line 9, instead would i say if debounce == true then and leave everything below the same?

I made a mistake in my original reply. Where I said that you should run a check to see if debounce was true, I meant to say that you should add a check to see if it is false. I believe everything below should stay the same, but if something happens that isn’t supposed to, please let me know.

So should I add the if debounce == true then and continue with the script? Or do I need to have it somewhere else

Put “if debounce == false then” and then continue with the script.

Okay, after adding that and testing it, the value is still not being added into my Crates value.

You should put an “end” to close the if statement after you set debounce to false again.

This is my current code:

local PLAYERDATA 	= game.ReplicatedStorage.PlayerData
local Remotes 		= game:GetService("ReplicatedStorage").Remotes
local PLAYERS 		= game:GetService("Players")
local debounce 		= false


script.Parent.Touched:Connect(function(Part)
	if Part.Parent:FindFirstChild("Humanoid") then
		if debounce == false then -- Change added here
		local Player = game.Players:GetPlayerFromCharacter(Part.Parent)
		if Player then		
			local CrateInfo = PLAYERS:WaitForChild(Player.Name):WaitForChild("Crates")
			local CrateAmnt = PLAYERDATA:FindFirstChild(Player.Name):WaitForChild("Warehouses"):WaitForChild("Crates")

			local Data = PLAYERDATA:FindFirstChild(Player.Name)
			local Warehouses = Data:WaitForChild("Warehouses")
			local Stats = Data:WaitForChild("Stats")
			local Plr = game.Players:FindFirstChild(Part.Parent.Name)

			-- fire event with gui to appear when touched showing collected crates and money earned
			if CrateInfo.TypeOfCrate.Amount.Value >= 1 then
				Remotes.showGui:FireClient(Player, "ShowCrates")
				wait(5)
				local model	 		= workspace.WarehouseClaim
				local Crates		= workspace.Crates

				model:SetPrimaryPartCFrame(CFrame.new(468.064, 7.446, -265.517))


				-- crate value handler
				Warehouses.Crates.Value = Warehouses.Crates.Value + CrateAmnt.Value 



				if Warehouses.Crates.Value == Warehouses.Max.Value then
					print("player cannot carry more crates, they must sell them")
				end

				debounce = true

				for _,v in pairs(PLAYERS:WaitForChild(Player.Name):GetChildren()) do
					if v:IsA("Folder") and v.Name == "Crates" then
						v:Destroy()
					end
				end

			else
				print("player does not have more than 1 crate to sell")

				end -- Added the end here
			end
		end
	end
end)

This is what the code for a debounce should look like:

local debounce = false
function blahblahblah()
    if debounce == false then
        debounce = true
        -- more code here
        debounce = false
    end
end

Would this still work with my .Touched function?

Yes, it would.
30 charactersss

Thank you! Turns out, I was adding a value that was 0, could’ve been working the whole time and I just didn’t even know. But thank you for helping me out!