I’m making a smoothie game, whatever bla bla I’m not sparing the details I just need help with my code
I have a proximityprompt that when triggered should check the player’s inventory for the ingredients they have in hand and the ingredients in a smoothie recipe, and then if they match then it should make the smoothie, however it doesn’t and I don’t know why since I printed the outcomes for both tables and they are the exact same
This is my code, the objects in the ingredients table are alphabetically ordered, as well as the objects in the playertable, so the sorting is not the problem I think.
local playerTable = {}
for i,v in pairs(plr.FruitFolder:GetChildren()) do
if v:IsA("StringValue") then
table.insert(playerTable, v.Value)
if #playerTable > 1 then
table.sort(playerTable,function(a,b)
return a < b
end)
end
print(playerTable)
end
end
for i,v in pairs(Smoothies) do
print(v.Ingredients)
print(v.Ingredients == playerTable)
if v.Ingredients == playerTable then
print("BLEND!!!!")
local news = RS.Smuuthie:Clone()
news.Name = v.." Smoothie"
news.Parent = plr.Backpack
end
end
this is the output in the console for the ingredients the player has and then the ingredients for the smoothie recipe and a boolean on whether they match or not
despite it literally matching it still returns false
That is because two tables are unique objects, they can never be equal to each other unless they’re references to the same table.
If your tables are shallow and the order doesn’t matter, you can check if each entry has a match in the other table like so:
local function check(t1, t2)
for _, value in t1 do
if not table.find(t2, value) then -- if an entry from t1 isn't found in t2 then return false
return false
end
end
-- check again in t2 for missing values in t1.
for _, value in t2 do
if not table.find(t1, value) then
return false
end
end
return true
end
v.Ingredients and playerTable are different tables that just happen to contain the same value. When you check them against each other, you’re asking if Table A is the same table as Table B, not if they contain the same values.
You can show the table addresses in the output settings and see what’s happening.
You’ll need to compare them manually. A shallow comparison checks just the top level, and is the most simple.
local function ShallowComparison(tableA, tableB)
for key, value in tableA do
-- Nothing stored at this key on table b
if not tableB[key] then
return false
end
-- tableA[key] doesn't match tableB[key]
if tableB[key] ~= value then
return false
end
end
return true
end