Wait 1 letter before executing (For loop)

Sorry if the title was confusing but what I have right now is when the user types a letter it trys to autofill using a string of words I give it. But once you type one letter it tryes to autofill and what I need is for it to wait till you type 2 or 3 letters before autofillings. Here is my code:

local SearchBar = script.Parent
local commands={"HelloPlayer","HelperBot","ExampleList"}
local EnterOpen = false --If the notification saying press eneter to autofill is on screen.

SearchBar:GetPropertyChangedSignal("Text"):Connect(function()
	local PartialName = SearchBar.Text
	local foundtarget
	for i = 1, #commands do
		local Command = commands[i]
		if string.lower(Command):sub(1, #PartialName) == string.lower(PartialName) then
			foundtarget = Command
			if EnterOpen == false then
					script.Parent.Parent.Frame.Visible = true
					EnterOpen = true
			end
			--SearchBar.Text = Command
			break
		end
	end

	-- use foundPlayer variable to do the other stuff
end)

Thanks!

1 Like

Simply add an if statement before the for-loop (or, use a guard clause). In this case, it would be the following:

...
if #PartialName < 2 then
    for i = 1, #commands do
        local Command = commands[I]
        ...
    end
end
...

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