String does not equal the expected string please help

im a bad scripter

local function getItem(String)
	for i, Item in pairs(game.ReplicatedStorage.Items:GetChildren()) do
		if string.sub(string.lower(Item.Name), 1, string.len(String)) == string.lower(String) then
			return Item
		end
	end
end

local function getIndex(Table, Value)
	for i, v in pairs(Table) do
		if v == Value then
			return i
		end
	end
end

local strings = {"Swo", "Swo", "Swo"}
local expectedstring = {"Sword, Sword, Sword"}
local String = ""

for i, v in pairs(strings) do

	local index2 = getIndex(strings, strings[#strings])
	if i ~= index2 then
		local item = getItem(v)
		if item then
			String = String..item.Name..", "
		end
	else
		local item = getItem(v)
		if item then
			String = String..item.Name..""
		end
	end
end
if String == expectedstring then
	print('works')
else
	print(String)
end

it prints SwordSword, Sword, how can I make it print Sword, Sword, Sword
thanks so much :slight_smile:

Since all elements in the table are the same, getIndex will always be returning 1. That’s why the first one is the one that never gets a comma. Is there a reason you need it like this instead of just if i ~= #strings?

4 Likes

no there is no reason, i just never knew i could so it like that

1 Like

I’m having some trouble understanding exactly what you’re trying to do, but it looks like you want to come up with a string where several elements of a table are conjoined by a comma. There’s a built-in lua way to do this, maybe it’s for you? It seems to do waht you’re trying to do, I may be misunderstanding

local someThings = {'Sword', 'Sword', 'Sword'}
print(table.concat(someThings, ', ')) -- will print "Sword, Sword, Sword"

There’s also a built-in “getIndex” function:

print(table.find({'a', 'b', 'c'}, 'b')) -- will print "2"

I’ve noticed a few other things that I don’t exactly understand; expectedstring is a table with the desired string as the only entry so your expression if String == expectedstring then will never be true. You may want to remove the brackets from {"Sword, Sword, Sword"}

I wish I could offer more help, but I don’t know entirely what you’re trying to do. Let me know if you have any questions!

1 Like