Debounce isnt functioning properly

In this script, a part gets spawned into the cframe of another (already existing part) with the same size, then gets deleted. It works on debounce and an “On and off” switch. The script works other then the fact that its “spammable” and can be used many times. When this happens, it makes another part onto the same area, instead of deleting the last one. I have tried adding an extra check, but It didnt work, I am very confused on why this script doesnt work.

The script:

local Detection = script.Parent.ClickDetector
local LocationWater = script.Parent.Parent.Location
local OnOff = "off"
local DebounceOn = false
Spawnedwater = ""

Detection.MouseClick:Connect(function()
	 Spawnedwater = Instance.new("Part")
	if OnOff == "off" then
		if DebounceOn == false then
			DebounceOn = true
			-- Specifications for water (so extra)
			Spawnedwater.CFrame = LocationWater.CFrame
			Spawnedwater.Size = LocationWater.Size
			Spawnedwater.CanCollide = false
			Spawnedwater.Anchored = true
			Spawnedwater.BrickColor = BrickColor.new("Baby blue")
			Spawnedwater.Material = 	Enum.Material.Sand
			Spawnedwater.Parent =  script.Parent.Parent

			wait(0.5)
			DebounceOn = false
			OnOff = "on"
		end
	end
end)

Detection.MouseClick:Connect(function()
	
	if OnOff == "on" then
		if DebounceOn == false then
			DebounceOn = true
			OnOff = "off"
			
			Spawnedwater:Destroy()
			wait(0.5)
			DebounceOn = false
		end
	

	end
end)

thank you in advance

Did you forget to put Spawnedwater inside the debounce?

what do you mean?

if you mean the top value, thats just there so the different click functions can still have the same instance

Try using return if you want to send that data

(Accidentally replied to me)

tried this, Still works. But didnt fix the debounce problem

Also. im not sure why you have two different Events for this, just do this:

Detection.MouseClick:Connect(function()
	 Spawnedwater = Instance.new("Part")
	if OnOff == "off" then
		if DebounceOn == false then
			DebounceOn = true
			-- Specifications for water (so extra)
			Spawnedwater.CFrame = LocationWater.CFrame
			Spawnedwater.Size = LocationWater.Size
			Spawnedwater.CanCollide = false
			Spawnedwater.Anchored = true
			Spawnedwater.BrickColor = BrickColor.new("Baby blue")
			Spawnedwater.Material = 	Enum.Material.Sand
			Spawnedwater.Parent =  script.Parent.Parent

			wait(0.5)
			DebounceOn = false
			OnOff = "on"
		end
	end
if OnOff == "on" then
		if DebounceOn == false then
			DebounceOn = true
			OnOff = "off"
			
			Spawnedwater:Destroy()
			wait(0.5)
			DebounceOn = false
		end
	

	end

end)

	
	

1 Like
if Debounce then Debounce = not Debounce -- if debounce == true then make it false and move on.
 if OnOff == "off" then warn"MACHINE IS POWERLESS" return end -- end here if OnOff == "off". 
 ...--Code Here
 Delay(0.5,function()
  Debounce = not Debounce
 end)
else warn"DEBOUNCE REJECTED" return Debounce -- alt warning for clicks during debounce
end

Try adapting something like this into it maybe? I do think it could be wrapped into a single function. Unless I am mistaken.

1 Like

Maybe just move
Spawnedwater = Instance.new(“Part”)
into the debounce. Not sure if it will help, but it would be worth a try. Also make the two functions into the same function, making them into two is not needed.

1 Like

There’s a lot of unnecessary code here.

Does this help?

local debounce : boolean

local function main()
	debounce = not debounce

	if debounce then return end

	if script.Parent.Parent:FindFirstChild("water") then
		-- destroy
	else
		-- spawn water
	end

	task.wait(0.5)
end

script.Parent.ClickDetector.MouseClick:Connect(main)
1 Like

I dont do it like this because it creates a feed back loop on click, causing the on to activate the other trigger, making it off and so on so forth

Try this. I merged the two :Connect functions, and changed around some checks so you’re now checking if DebounceOn isn’t activated first, then doing stuff depending on what OnOff is. After you do what you need based off of OnOff, we wait for half a second and turn OnOff to “off” and DebounceOn to false.

Edit: changed Spawnedwater to be nil at the start, then be assigned at the beginning to either what it already is, or a new part. I also set Spawnedwater to be nil after we destroy it, just in case.
(edit 2 & 3: formatting)

local Detection = script.Parent.ClickDetector
local LocationWater = script.Parent.Parent.Location
local OnOff = "off"
local DebounceOn = false
Spawnedwater = nil

Detection.MouseClick:Connect(function()
	Spawnedwater = Spawnedwater or Instance.new("Part")
	if DebounceOn == false then
		DebounceOn = true
		if OnOff == "off" then
			-- Specifications for water (so extra)
			Spawnedwater.CFrame = LocationWater.CFrame
			Spawnedwater.Size = LocationWater.Size
			Spawnedwater.CanCollide = false
			Spawnedwater.Anchored = true
			Spawnedwater.BrickColor = BrickColor.new("Baby blue")
			Spawnedwater.Material =  Enum.Material.Sand
			Spawnedwater.Parent =  script.Parent.Parent
		else
			OnOff = "off"
			Spawnedwater:Destroy()
			Spawnedwater = nil
		end
		wait(0.5)
		DebounceOn = false
		OnOff = "on"
	end
end)
1 Like

had to edit the script a bit, but other than that this does indeed work. Very smart ide to make spawnedwater to spawned water, or a new instance.

Final script:

local Detection = script.Parent.ClickDetector
local LocationWater = script.Parent.Parent.Location
local OnOff = "off"
local DebounceOn = false
Spawnedwater = nil

Detection.MouseClick:Connect(function()
	Spawnedwater = Spawnedwater or Instance.new("Part")
	
	if DebounceOn == false then
		DebounceOn = true
		if OnOff == "off" then
			Spawnedwater.CFrame = LocationWater.CFrame
			Spawnedwater.Size = LocationWater.Size
			Spawnedwater.CanCollide = false
			Spawnedwater.Anchored = true
			Spawnedwater.BrickColor = BrickColor.new("Baby blue")
			Spawnedwater.Material =  Enum.Material.Sand
			Spawnedwater.Parent =  script.Parent.Parent
			OnOff = "on"
		else
			OnOff = "off"
			Spawnedwater:Destroy()
			Spawnedwater = nil
			DebounceOn = false
		end
		wait(0.5)
		DebounceOn = false
		OnOff = "on"
	elseif DebounceOn == true then
		DebounceOn = false
	end
end)
1 Like

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