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

Script is outputting the incorrect item, any help?

The output isn't printing out "Hot Dog". I am trying to learn table.find() and I started adding more tables in side of tables to see how it would work, but it just prints out nil, why is that? Here's the code


local t = {
	{"Burger",1000},
	{"Taco",5000},
	{"Hot Dog",150000},
	{"Steak",60},
	{"Grilled Cheese",10}
}
for i, number in pairs(t) do
	print(number)
	print(table.find(t,"Hot Dog",5))
end


1 comment

0

legit just asking this so i can learn

2 answers

✓ Accepted answer

The reason it is printing nil is because you are searching the table t for "Hot Dog", however, it only has tables inside of itself. Instead, you should search each sub-table:

local t = {
    {"Burger",1000},
    {"Taco",5000},
    {"Hot Dog",150000},
    {"Steak",60},
    {"Grilled Cheese",10}
}
for i, tbl in pairs(t) do
    local number = tbl[2]
    print(number)
    print(table.find(tbl,"Hot Dog"))
end

-- Output:
-- 1000
-- nil 
-- 5000
-- nil
-- 150000
-- 1<-- This is where "Hot Dog" is located.
-- 60
-- nil
-- 10
-- nil

Here is a script I toke from roblox references] https://developer.roblox.com/en-us/api-reference/lua-docs/table

local t = {"a", "b", "c", "d", "e"}

print(table.find(t, "d")) --> 4
print(table.find(t, "z")) --> nil, because z is not in the table
print(table.find(t, "b", 3)) --> nil, because b appears before index 3

Judging from that script maybe try changing yours to

local t = {"Burger",1000, "Taco",5000,"Hot Dog",150000,  "Steak",60, "Grilled Cheese",10}

for i, number in pairs(t) do
    print(number)
    print(table.find(t,"Hot Dog",5))
end

2 comments

0

I was trying to attach the numbers to each item, like Burger's number would be 1,000, and Taco's is 5,000 etc

0

Mabye do somthing like: Local "Grilled Cheese" = 10 Or make them a Number vaule or something some where else and link them like: Local "Grilled Cheese" = script.Parent.Value1

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