Item detecter with string.sub isn't working properly

Here’s the script:

 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.

Any help would be great.

The issue is in your first if statement.

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.

If i’m not mistaken you could just use string.find()
and check to see if it returns a number or nothing


here is a link to the devhub post

-- string.find will either return a number or null
if string.find(string.lower(Item.Name), string.lower(String)) then
    print("Item Found")
end

EDIT: Changed some things to be lowercase because of issues below this response

2 Likes

Wait, I’m bad at reading, sorry, disregard my last answer.

1 Like

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

Don’t forget to do string.lower on the item name too.

1 Like

Actually I think string.find ignores caps anyway because of the patterns it uses.

1 Like

Oh, my bad, I didn’t know that.

1 Like

Sure about that?

image

Case still matters for plaintext matches. This is the result of performing find on different cases in both Roblox Studio and the Lua demo page.

5 Likes

Ah sorry must’ve missed that, I was quite unaware of it glad you actually tested it

1 Like

This does not work; it is doing the same as before.

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

Can’t reproduce your issue at all.

Yes, I’m doing partial name collection.

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"))

Here is a screenshot of the "items" I am testing:

and here is the output:

Everything works perfectly

You’re using a server script, and I’m using a local script. Is that the issue?

I just tested on a localscript placed in StarterPlayerScripts with all the models parented to it like in the server scripts and it works as before

I’m using a GUI to test this, and I’ll test your code in a moment.

This works, but I’m unsure why mine isn’t working; I see a difference in yours and my code with the.Name. I’ll keep you updated.

Item.Name and Item still doesn’t work.