Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Script is outputting the incorrect item, any help?

Asked by 3 years ago

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
0
legit just asking this so i can learn jorcorrs 76 — 3y

2 answers

Log in to vote
4
Answered by
appxritixn 2235 Moderation Voter Community Moderator
3 years ago

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
Ad
Log in to vote
0
Answered by 3 years ago

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

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 jorcorrs 76 — 3y
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 Boss246813579 -6 — 3y

Answer this question