Crafting with tables

So I will have to craft in my game, and I decided to use tables because they are more efficient. And now I am stumped.

local player = game.Players.LocalPlayer
local Stats = player:WaitForChild("Stats")
local Level = Stats:WaitForChild("Level")
local Item1= Stats:WaitForChild("Item1")
local Item2= Stats:WaitForChild("Item2")

local Craft = {"Item1" == 1, "Item2" == 1} -- These are the requirements to craft


for i, v in pairs(Craft) do
	if Stats[v] then -- This is the error part, what I am trying to do is detect if the Stats has the required amount of each item in the table
		print("a")
	end
end

10:36:08.642 - Players.TOP_Crundee123.PlayerGui.UI.CraftingInterface.ScrollingFrame.Register:12: bad argument #2 to ‘?’ (string expected, got boolean)

I know why it is a boolean error, but I can’t find any other way. All help is appreciated.

1 Like

if statements require a true or false condition. Stats[v] is a string, not a boolean. Can you provide information on what Stats is so I can help you further?

1 Like

The stats is the leaderstats and the Items are IntValues. I am trying to detect if the Values are equal to the table

To get the value of IntValue objects, you must use Stats.Value

1 Like

You may try rewriting it to look like this:

local player = game.Players.LocalPlayer
local Stats = player:WaitForChild("Stats")
local Level = Stats:WaitForChild("Level")
local Item1= Stats:WaitForChild("Item1")
local Item2= Stats:WaitForChild("Item2")

local Craft = {Item1 = 1, Item2 = 1} -- These are the requirements to craft


for i, v in pairs(Craft) do
	if Stats[i].Value then
		print("a")
	end
end

What you were previously doing was indexing the Stats object with a number. I don’t know how that came about, but oh well.

1 Like

It now says that it is not parented to folder

I tried Stats[v].Value, still the same error

local Craft = {["Item1"] = 1, ["Item2"] = 1}

if Item1.Value == Craft.Item1 and Item2.Value == Craft.Item2 then
    print("a")
end

Your table was not formatted properly and I fixed the if statement

1 Like

Replace the [v] with an [i].

it works now, but, then, whats the point of using a table when you can just do if Item1.Value >= ?

It really depends on your implementation. You could use a table to store all the requirements, or explicitly input them in the if statement.

Edit:
Also, since you mentioned it’s a leaderstats, you may want to use >= instead of ==

So how would I do it with a table without using if Item1.Value == Craft.Item1 ?If there isn’t, ill just use that

You could simply do

if Item1.Value >= 1 and Item2.Value >= 1 then
    print("a")
end

Wait I misread

Sorry, but my explanation was not that great. I want it so it loops through the table

I personally think looping would be unnecessary. I’d stick with the code I posted because I’d only use loops for big tables.

1 Like

No, I asked for it because there is going to be a big crafting recipe that would use 10 things or so. And I want to keep things organized

local Stats = {Player.Item1, Player.Item2}
local Craft = {1,1}

local Success = 0

for i, v in ipairs(Stats) do
    if v.Value >= Craft[i] then
        Success = Sucess + 1
        if Success == #Craft then
            print("All Requirements are Met")
            Success = 0
        end
    end
end
1 Like

Sorry if I am asking too much, but is there a way to shrink it to one table?

You mean combine the Stats and Craft table?

Yes, but would I do, {"Item1", 1, "Item2", 1} ?