Only having debounce work on certain blocks of code?

while true do
	wait()
	
	
	--limit maximum amount--
	if food.Value >= 101 then
		food.Value = 100
	elseif water.Value >= 101 then
		water.Value = 100
	elseif blood.Value >= 101 then
		blood.Value = 100
	elseif condition.Value >= 101 then
		condition.Value = 100
		
	end
	------------------------
	

	
	-- remove or add amounts naturally--
	if not fooddebounce then
		fooddebounce = true
		food.Value = food.Value - 1
		wait(5)
		fooddebounce = false
	end
	------------------------------------
	end

Im trying to have the “maximum amount” block of code run without any debounce and only have the “add or remove” block of code run debounce. but by setting fooddebounce to true the whole script is affected.

Is the problem that the code is waiting when doing the “remove or add” section of the code?

If so, you may be interested in the delay() function.

By changing the “add or remove” section to the following, the loop will continue to run even when fooddebounce is false. fooddebounce will then return to being false after the amount of time you specify.

if not fooddebounce then
    fooddebounce = true
    delay(5, function()
        -- this code will occur in 5 seconds
        fooddebounce = false
    end)
    food.Value = food.Value - 1
end

delay() will create a new thread, so multiple pieces of code will run at the same time. This means the rest of the loop will not be affected! Hope this helped :smiley:

1 Like