Make a debounce for textboxes

Basically what I want to do in my TEXTBOX is create a debounce once it recognises the inputted word.

When the word is recognised by typing it in, a function of something will occur once.

My code here will spam the function continuously if it recognises the word, which is why I want a debounce to be placed so the function will only occur once. It will take 10 seconds before the function can work again.

THE CODE:

local MessageBox = script.Parent

MessageBox.Changed:Connect(function()
	if MessageBox.Text == "scary" then
		warn("ur cool") --the function example
	end
end)

I do not know how to make de-bounces, they are too confusing.

1 Like

You can try this code, it uses os.clock to check if 10 seconds has been passed.

local MessageBox = script.Parent

local lastime = os.clock()
local firstTime = true

MessageBox.Changed:Connect(function()
	local currentTime = os.clock()
	if MessageBox.Text == "scary" and (firstTime == true or currentTime - lastime >= 10) then
		warn("ur cool") --the function example
		lastTime = currentTime -- update time
        firstTime = false
	end
end)
3 Likes

Can’t you just do

local MessageBox = script.Parent
local checked = false

MessageBox.Changed:Connect(function()
	if not checked and MessageBox.Text == "scary" then
        checked = true
		warn("ur cool") --the function example
	end
end)
1 Like

that code is really nice and awesome!! though, you can simplify it even further :3!

local MessageBox = script.Parent

local LastTime = 0

MessageBox.Changed:Connect(function()
	if MessageBox.Text ~= "scary" or tick() - LastTime < 10 then return end -- avoid nesting
	warn("ur cool") --the function example

	LastTime = tick() -- update time
end)

you don’t really need to use a FirstTime variable if you set LastTime’s default to 0

at first i actually thought of disconnecting the main event and overcomplicating it even further lol

5 Likes

so, I’m wondering, why should you avoid nesting?

1 Like
local MessageBox = script.Parent
local debounce = false -- Initialize debounce variable

MessageBox.Changed:Connect(function()
    if MessageBox.Text == "scary" and not debounce then
        debounce = true -- Set debounce to true to prevent further execution
        warn("ur cool") -- The function example
        wait(10) -- Wait for 10 seconds
        debounce = false -- Reset debounce to allow the function to run again
    end
end)
1 Like

very easy! it keeps your code clean and easier to read. it might also speed it up in some cases!

if you’re going to do a function/scope that only runs if some conditions are met, the best thing would be to do if (not condition) then return end statements at the start of the function to avoid indents from accumulating (nesting).

this is a bad example, but imagine you were to check multiple conditions in multiple if checks (never do this):

if thing then
	if thing2 then
		if thing3 then
			if thing4 then
				print("hi!!!") -- this is a dramatization

			end
		end
	end
end

this just looks better and it’s easier to read:

if not thing then return end
if not thing2 then return end
if not thing3 then return end
if not thing4 then return end
print("hi!!!")
3 Likes

Your solution it is better than mine! Really cool.

2 Likes

I will try this out, and others too.

There’s a really good youtube video about this topic if you’re interested:

1 Like

now with modules

Main script


local MessageBox = script.Parent
local DebounceModule = require(game.ReplicatedStorage.DebounceModule)

local function coolFunction()
    warn("ur cool")
end

local debounceCoolFunction = DebounceModule.create(coolFunction, 10)

MessageBox.Changed:Connect(function()
    if MessageBox.Text == "scary" then
        debounceCoolFunction()
    end
end)

module script


local DebounceModule = {}

function DebounceModule.create(callback, cooldownTime)
	local isCooldown = false

	return function()
		if not isCooldown then
			isCooldown = true
			callback()
			wait(cooldownTime)
			isCooldown = false
		end
	end
end

return DebounceModule

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