How to detect which Item(s) a player is typing in a textbox?

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)
1 Like

Errored, [Players.raid6n.PlayerGui.Register.Background.LocalScript:16: invalid argument #2 (string expected, got nil)]

Edit: When I tried this with 1 item, it worked, 2 or more, it did not.

ok i just edited it, try it out now

1 Like

Errored when I did “Swor, Gu”, worked when I did “Swor”. (Didn’t work when I did 2 or more items.

wait are you trying to find multiple objects with one string, or one object whose name matches really closely with that string?

1 Like

im trying to find 1 or more items, sorry if i wasn’t clear enough

its okay, but i am a bit confused, since you do
image

(A single Item name)

1 Like

ah, apologies. im doing 1 or more items, i can change it if you would like

so you want to set the name of the textlabel to the names of multiple items put together? :face_with_monocle:

1 Like

yes, but instead of changing a textlabel, it’s the text box’s text

oh my bad, i didnt see what you were trying to do in the first post. I see what your trying to do now

1 Like

ok try this out:

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)

Errored at line 21. attempt to concatenate Instance with string

sorry, its fixed now.

Input:
“ha, sw, cu”
Output:
“Hammer, Sword, Cutlass”

eek, it errored for me though 19:39:19.992 - Players.raid6n.PlayerGui.Register.Background.LocalScript:12: invalid argument #2 (string expected, got nil)

fixed.

30char30char30char30char

worked for 2 or more items, but it didn’t for one item.

output when typed in one item:
Players.raid6n.PlayerGui.Register.Background.LocalScript:21: attempt to concatenate string with nil

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)
1 Like

i used a pcall function so if the line errors, it uses this code

1 Like