What my code is supposed to do: I have many items located in a folder in ReplicatedStorage. When a player types in a few or more characters of 1 or more items, it is supposed to change the text to the item(s) full names.
Example: I have 2 items and type in a few characters of them, and separated them with commas. My text in the textbox: “Sw, Gu”, which should change the text to Sword and Gun, but it only changes it to Sword.
(Also, if you did it with one item, it works perfectly.)
Code:
local function getItems(String)
for i, Item in pairs(ReplicatedStorage:WaitForChild("Items"):GetChildren()) do
local gsub = String:gsub(",", "")
local substring = gsub:sub(1, #gsub)
local Table = string.split(substring, " ")
for i, v in pairs(Table) do
if string.sub(string.lower(Item.Name), 1, string.len(v)) == string.lower(v) then
return Item
end
end
end
end
ItemHandler.FocusLost:Connect(function()
local Items = getItems(ItemHandler.Text)
ItemHandler.Text = Items.Name
end)
try this:
This finds the item with the most characters that it matched with
local function getItems(String)
local matches = {}
local closestmatchcount = 0
local closestmatchname;
for _, Item in pairs(ReplicatedStorage:WaitForChild("Items"):GetChildren()) do
if Item.Name:match(String) and Item.Name:match(String):len() > closestmatchcount then
closestmatchcount = Item.Name:match(String):len()
closestmatchname = Item.Name
end
end
return ReplicatedStorage.Items[closestmatchname]
end
ItemHandler.FocusLost:Connect(function()
local Item = getItems(ItemHandler.Text)
ItemHandler.Text = Item.Name
end)
function replacestring(x)
local function finditemmatchingwithname(String)
local matches = {}
local closestmatchcount = 0
local closestmatchname;
for _, Item in pairs(ReplicatedStorage:WaitForChild("Items"):GetChildren()) do
if Item.Name:lower():match(String) and Item.Name:lower():match(String):len() > closestmatchcount then
closestmatchcount = Item.Name:lower():match(String):len()
closestmatchname = Item.Name
end
end
return closestmatchname
end
local _, numberitems = x:gsub(",", ",")
local newstr = ""
for i=1, numberitems + 1 do
local islast = i == numberitems + 1
local itemsegment = x:sub(1, not islast and x:find(",") - 1 or x:len())
x = x:gsub("^" .. itemsegment .. ", ", "")
itemsegment = finditemmatchingwithname(itemsegment)
newstr = not islast and newstr .. itemsegment .. ", " or newstr .. itemsegment
end
return newstr
end
ItemHandler.FocusLost:Connect(function()
local newstring = replacestring(ItemHandler.Text)
ItemHandler.Text = newstring
end)
i updated it since that glitch, you should use the last one i posted:
function replacestring(x)
local function finditemmatchingwithname(String)
local matches = {}
local closestmatchcount = 0
local closestmatchname;
for _, Item in pairs(ReplicatedStorage:WaitForChild("Items"):GetChildren()) do
if Item.Name:lower():match(String) and Item.Name:lower():match(String):len() > closestmatchcount then
closestmatchcount = Item.Name:lower():match(String):len()
closestmatchname = Item.Name
end
end
return closestmatchname
end
local _, numberitems = x:gsub(",", ",")
local newstr = ""
for i=1, numberitems + 1 do
local islast = i == numberitems + 1
local itemsegment = x:sub(1, not islast and x:find(",") - 1 or x:len())
x = x:gsub("^" .. itemsegment .. ", ", "")
itemsegment = finditemmatchingwithname(itemsegment)
newstr = not islast and newstr .. itemsegment .. ", " or newstr .. itemsegment
end
return newstr
end
ItemHandler.FocusLost:Connect(function()
local newstring = replacestring(ItemHandler.Text)
ItemHandler.Text = newstring
end)