Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is there a way to use string.match on one character strings?

Im making an admin gui, and i would like it to find all the strings that match the first string and put them in a table, which eventually i can use to make suggestions appear below where the client is typing.

What i would like if for example, I type "t" it should match up with "to" and "tp" but it doesnt, only when i type "to" will it match with "to" is it possible to fix?

local tb = script.Parent.tb
local TC = tb.Toggle_Clear
local plr = game.Players.LocalPlayer

local cmds = {
	
	"kill";
	"to";
	"bring";
	"tp";
	"kick";
	"ban";
	"unban";
	
}


local clearon = true

TC.Activated:Connect(function()
	if clearon == true then
		clearon = false
		tb.ClearTextOnFocus = false
		TC.Text = "Clear Text On Click: OFF"
	else
		clearon = true
		tb.ClearTextOnFocus = true
		TC.Text = "Clear Text On Click: ON"
	end
end)


local lastfoundnum = 0
tb.FocusLost:Connect(function()
	local s1 = tb.Text
	local matches = {}
	
	
	
	local arg1 = nil
	local arg2 = nil
	local arg3 = nil
	local arg4 = nil
	local arg5 = nil
	local arg6 = nil
	
	lastfoundnum = string.find(s1, " ")
	arg1 = string.sub(s1,1,lastfoundnum)
	print(arg1)
	
	for i,v in pairs(cmds)do
		if string.match(string.lower(arg1),v,0) then
			table.insert(matches,v)
		else
			print("no match")
		end
	end
	
	for i,v in pairs(matches)do
		print(v)
	end
end)

2 answers

✓ Accepted answer

####Accept this question if it helped. (ok you can read the answer now)

##Question

Is there a way to use string.match on one character strings?

##What I think you're asking how Based on this context:

for example, I type "t" it should match up with "to" and "tp" but it doesnt, only when i type "to" will it match with "to" is it possible to fix?

I think you're trying to autocomplete words using the starting character.

##Solution We can use the ^ magic character to look at the start of the string using :match since that would be most useful here. (We could also use sub and get a string of the length of our predicting phrase and see if its equal to that)

We can iterate through all our commands and check if the start of the string matches what we are looking for. If it does then we can add it to an array called matchedCommands or something related to what we are doing. At the end we return the array and we can use that array else where.

###Example Code:

local animals = { --// animals array
	"aardvark",
	"crocodile",
	"dog",
	"cat",
	"alligator"
}

local function predictAnimals(predictingPhrase) --// function we are using to get the animals from a word
	local predictedAnimals = {} --// array to store animals we are predicting
	local pattern = ("^%s"):format(predictingPhrase) --// our pattern. Just puts `^` at the start so we are looking at the start of the string. 

	for i, animal in next, animals do --// iterate over animals
		local animalName = animal:lower() --// make it so its not case sensitive

		if animalName:match(pattern) then --// does it start with the word
			predictedAnimals[#predictedAnimals + 1] = animalName --// add to predicted animals array
		end
	end

	return predictedAnimals --// return the array
end

local animalsStartingWithA = predictAnimals("a")

for i, animal in next, animalsStartingWithA  do
	print("starts with a: ", animal)
end

This isn't really a answer so please don't accept it as one, but I wanted to make a search script in the past for some projects to and I made this. Probably inefficient but works great and is less annoying then roblox's search.

--------------------------------------------------
---------------------Settings---------------------
--------------------------------------------------
local searchResults = {
	{"Motor6D", Vector3.new(0,0,0)}, 
	{"Motor", Vector3.new(0,0,0)}, 
	{"Animating Parts", Vector3.new(0,0,0)}, 
	{"Sprint", Vector3.new(0,0,0)}, 
	{"NoJump", Vector3.new(0,0,0)}, 
	{"Admin Commands", Vector3.new(0,0,0)}, 
	{"Developer Products", Vector3.new(0,0,0)}, 
	{"Search", Vector3.new(0,0,0)}, 
}
--------------------------------------------------
local function seperateWords(words)
	local spaceLocations = {}
	local keyTerms = {}
	for i=1, string.len(words) do
		if string.sub(words, i, i) == " " then
			table.insert(spaceLocations, i)
		end
	end
	for i=1,#spaceLocations+2 do
		if i == 1 then
			table.insert(keyTerms, string.sub(words, 1, spaceLocations[1]))
		elseif i == #spaceLocations+2 then
			--Do nothing.
		else
			table.insert(keyTerms, string.sub(words, spaceLocations[i-1]+1, spaceLocations[i]))
		end
	end
	for i,v in ipairs(keyTerms) do
		if i == #keyTerms then
			--Do nothing.
		else
			keyTerms[i] = string.sub(keyTerms[i], 1, string.len(keyTerms[i])-1)
		end
	end
	return keyTerms
end

local function matchWords(wordTable)
	if searchResults[1][3] ~= nil then
		for i,v in ipairs(searchResults) do
			v[3] = nil
		end
	end
	for i,v in ipairs(searchResults) do
		table.insert(v, 0)
	end
	for i,v in ipairs(wordTable) do
		for i1,v1 in ipairs(searchResults) do
			if string.match(string.lower(v1[1]), string.lower(v)) then
				v1[3] = v1[3]+1
			end
		end
	end
	table.sort(searchResults, function(a,b) return a[3] > b[3] end)
	return searchResults
end

script.Parent.MouseButton1Click:Connect(function()
	local run = matchWords(seperateWords(script.Parent.Parent.InputBox.Text))
	for i,v in ipairs(script.Parent.Parent.SearchResultsScrollingFrame:GetChildren()) do
		v:Destroy()
	end
	for i,v in ipairs(searchResults) do
		local clone = script.Parent.Parent.SearchResultExampleButton:Clone()
		clone.Parent = script.Parent.Parent.SearchResultsScrollingFrame
		clone.Name = "SearchResult"
		clone.Text = v[1]
		clone.PositionValue.Value = v[2]
		clone.Position = UDim2.new(0,10,0,((i-1)*35)+10)
		clone.Visible  = true
	end
	script.Parent.Parent.SearchResultsScrollingFrame.CanvasSize = UDim2.new(0,0,0,(#searchResults*35)+10)
end)

2 comments

0

Also, I didn't know how to use string.match at the time so I just used loops and string.sub

0

so then why did you answer the question if you have no idea what you're doing

This discussion is preserved as it was. New answers, comments, and votes are closed.