local function getItem(String)
for i, Item in pairs(Items:GetChildren()) do
if string.sub(string.lower(Item.Name), 1, string.len(String)) == string.lower(String) then
return Item.Name
end
end
end
local Item = getItem(script.Parent.Text)
if Item then
print(Item.Name)
end
It works (sort of), the problem is that it prints a different item, for example, when I type in Coil (one of the items), it prints a different item, Sword.
What have I tried to change: I tried removing Sword from the items, and it when I type in ‘6534276523674’, (which is not an item), it prints Coil.
string.sub(string.lower(Item.Name), 1, string.len(String)) is just giving you String in lowercase. You’re then comparing that to string.lower(String), which is also just String in lowercase.
What’s the issue, it’s purposely in lowercase so the player doesn’t need to talk it in the correct caps so like if i remove this, it would say ‘coil isn’t a valid item’, which it is
Are you trying to do partial name collection here? Somehow I feel that you’ve made a mistake somewhere along the way and can’t find it. As a quick test:
local a = "Sword"
local b = "Swo"
print(a:lower():sub(1, #b) == b:lower()) -- true
And another:
local items = {"Sword", "Coil", "Foo", "FooBar", "Qaz"}
local function getItem(partialString)
for _, item in ipairs(items) do
if item:lower():sub(1, #partialString) == partialString:lower() then
return item
end
end
return "Invalid"
end
print(getItem("Coil")) -- Coil
print(getItem("6534276523674")) -- Invalid
table.remove(items, 1)
print(getItem("6534276523674")) -- Invalid
I also ran his code using a built in list of items I also cannot reproduce the issue
Assuming it might be something with .Name or model names EDIT: Also tested with models and it worked fine
EDIT2:
Here is the code:
local items = script:GetChildren()
local function getItem(String)
-- // Loops through all the items
for i, Item in pairs(items) do
-- // Tries to find anything within search criteria
if string.find(Item.Name:lower(), String:lower()) then
return Item
end
end
end
print("Item Found: ", getItem("coi"))