Trouble with Proximity Prompt debounce

I want to have a 60-second client-sided cooldown on a vending machine that gives you an item upon trigger of a ProximityPrompt
Here is an absolute muddling mess of a script (please don’t judge)

local ProximityPrompt = script.Parent
local debounceTime = 60
local isDebouncing = false
local ToolName = "AlmondWater"
local Storage = game.ServerStorage

ProximityPrompt.Triggered:Connect(function(player)
    if not isDebouncing then
        if player and player.Character then
            local Backpack = player:FindFirstChild("Backpack")
            if not Backpack then
                Backpack = Instance.new("Folder")
                Backpack.Name = "Backpack"
                Backpack.Parent = player
            end

            local Tool = Storage:FindFirstChild(ToolName)
            if Tool then
                Tool:Clone().Parent = Backpack
                print("proximityPrompt triggered")

                isDebouncing = true
                wait(debounceTime)
                isDebouncing = false
            else
                print("almond water not found")
            end
        else
            print("player not found")
        end
    else
        print("debounce successful")
    end
end)

I’ve tried searching for solutions on the forum, but none came up, so I just gave up and now I’m here

If you want a “Client-Side” cooldown, do you mean that the button doesn’t work?
The way you can handle a cooldown is by creating a table of players, and then adding them to the list once they are on cooldown

Try this:

local ProximityPrompt = script.Parent
local debounceTime = 60
local isDebouncing = false
local ToolName = "AlmondWater"
local Storage = game.ServerStorage

local playerDebounces =  {}

ProximityPrompt.Triggered:Connect(function(player)
    if not table.find(playerDebounces, player) then
        if player and player.Character then
            local Backpack = player:FindFirstChild("Backpack")
            if not Backpack then
                Backpack = Instance.new("Folder")
                Backpack.Name = "Backpack"
                Backpack.Parent = player
            end

            local Tool = Storage:FindFirstChild(ToolName)
            if Tool then
                Tool:Clone().Parent = Backpack
                print("proximityPrompt triggered")

                table.insert(playerDebounces, player)
                wait(debounceTime)
               table.remove(playerDebounces, table.find(playerDebounces, '123'))
            else
                print("almond water not found")
            end
        else
            print("player not found")
        end
    else
        print("debounce successful")
    end
end)

Pretty much what we are doing is, just adding the player to a list and using table.find() to see if they are in the table.

If you want that “client-side” effect where you can’t see the button at all, create a stringvalue parented to the proximity button, and using game.HttpService:JSONEncode and game.HttpService:JSONDecode to reference the table to the client.

When I was testing this out in a random part I placed on the ground, I kept getting stuck on “almond water not found.”

I made this changes:

local ProximityPrompt = script.Parent
local debounceTime = 5
local isDebouncing = false
local ToolName = "AlmondWater"
local Storage = game.ServerStorage

ProximityPrompt.Triggered:Connect(function(player)
	if not isDebouncing then
		isDebouncing = true
		if player and player.Character then
			local Backpack = player:FindFirstChild("Backpack")
			if not Backpack then
				Backpack = Instance.new("Folder")
				Backpack.Name = "Backpack"
				Backpack.Parent = player
			end

			local Tool = Storage:FindFirstChild(ToolName)
			if Tool then
				Tool:Clone().Parent = Backpack
				print("proximityPrompt triggered")
				wait(debounceTime)
				isDebouncing = false
			else
				print("almond water not found")
				wait(debounceTime)
				isDebouncing = false
			end
		else
			print("player not found")
			isDebouncing = false
		end
	elseif isDebouncing then
		print("You cannot use this right now.")
	end
end)

It’s definitely not pretty, but it did work. I think your error stemmed from the code going down different if statements and not triggering you debounce variable changes. I think you should probably always change the debounce immediately right after the if statement and then make sure regardless of where your code goes (which if statement), it always changes the debounce back. Because with how it was before, certain paths of the if statement did not lead to a debounce change. I am unsure whether you want to include the debounce time if the player does have the item in their inventory, so I did not include it in the above example.

I also changed the debounce time in the above for testing purposes.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.