Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to check if table index is this string?

How to check if index of a table is a string?

So for example, how to check if the index 1 of table "InvTable" is a string "Iron Ore"?

EDIT:

	if player.Smelting.Value == true then
		if getIndex(invtab, "Copper Ore") == true then	
			 local copper = getIndex(invtab, "Copper Ore")		
				table.remove(invtab, copper)
				CheckForEmptyBox()
				local numb = selectedbox.Numb
				table.insert(invtab, numb.Value, "Copper Ingot")
				player.Smelting.Value = false
				player.Smeltwhat.Value = ""
		else print("Nope")
		end

this is the code im trying to use. It prints "Nope", even though im sure there is a value "Copper Ore" in the invtab table. What am i doing wrong?

No errors btw.

1 answer

✓ Accepted answer

This should return the spot in the table whatever you specify is in, and nil if none:

function getIndex(table, item)
	for i, v in pairs (table) do
		if v == item then
			return i
		end
	end
end

EDIT: I didn't include an example..

if getIndex(InvTable, "Iron Ore") ~= nil then
	-- Do stuff
else
	-- Do other stuff?

Hope this helped.

EDIT 2: The getIndex function returns which spot it's in, not a boolean value. So, it should look like this:

if player.Smelting.Value == true then
    if getIndex(invtab, "Copper Ore") ~= nil then 
         local copper = getIndex(invtab, "Copper Ore")      
            table.remove(invtab, copper)
            CheckForEmptyBox()
            local numb = selectedbox.Numb
            table.insert(invtab, numb.Value, "Copper Ingot")
            player.Smelting.Value = false
            player.Smeltwhat.Value = ""
    else print("Nope")
    end

1 comment

0

Updated my post.

This discussion is preserved as it was. New answers, comments, and votes are closed.