Crafting with tables

You aren’t actually looping through anything, you’re just using the function several times a second or less to get updated the crafting status. The function itself isn’t very heavy in terms of computation, so it shouldn’t really create an impact when called in loops.

1 Like

Which one do you think is the best for this type of situation?

I think the coroutine would be the best way to go.

1 Like

Spawn and coroutine are both used for pseudothreading and perform relatively the same function, but you should aim to use coroutines moreso than spawn.

Spawn runs on the legacy task scheduler and has a built-in wait. Wait, as you may or may not know, never actually waits the amount of time that you expect. There are two parts: waiting for the amount of time specified (or the minimum of ~0.03) as well as until there’s an available slot in the scheduler to resume. This is why wait returns a value which is the actual time wait spent waiting.

Always try to go for coroutines.

This discussion has been held a few times before. Most recent thread if you’re interested:

2 Likes

Is there a way to have .Changed like this(I want it so I don’t have to do .Changed for every item)?

local Items =  {Item1, Item2}

Items.Changed:Connect(function()
    coroutine.resume(Coroutine)
end)

So when an item value changes in the table, the Changed event fires. Is that the way to do it?

You don’t necessarily need to do this. Once the coroutine is resumed/started, the loop will keep on updating the values. However, if you’d still wish to have this kind of setup, you could create something like this:

local Items = {Item1, Item2}
local CraftData = {
    [1] = {Item1, 1, Item2, 1}
    [2] = {Item1, 1, Item2, 100}
}

local function VerifyRequirements(CraftTable)
    local Success = 0
    for i, v in ipairs(CraftTable) do
        if v:IsA("IntValue") then
            if v.Value >= CraftTable[i + 1] then
                Success = Success + 1
                if Success == #CraftTable/2 and i == #CraftTable then
                    print("Requirements Complete")
                elseif Success ~= #CraftTable/2 and i== #CraftTable then
                    print("Requirements Incomplete")
                end
            end
        end
    end
end

for i, v in ipairs(Items) do
    v.Changed:Connect(function()
        VerifyRequirements(CraftData[i])
    end
end
1 Like

I thought that I can use pairs, but then, doesn’t in ipars run only once? Thank you!

Pairs and ipairs doesn’t have much of a difference other than pairs supposedly being used with dictionaries and ipairs being used with numerically arranged tables (array was the word I was looking for). The only good thing about using ipairs is readability and actually using its intended function. You could always use pairs since it supports both dictionaries and tables with numbered indexes arrays.

Read more here:

pairs:

  • Designed for dictionaries
  • Calls next, which finds the next key based on a given key
  • Ordered iteration is not guaranteed
  • Optimised but slower than ipairs
  • All keys are accounted for
  • Does not stop at a nil value

ipairs:

  • Designed for arrays
  • Index lookup starts at 1 and increases every iteration
  • Ordered iteration is guaranteed
  • Quicker than pairs with known indices
  • Ignores non-numeric indices
  • Stops iterating at the first nil value for an indice

There’s a noticeable difference when using each depending on the circumstances. Use ipairs for arrays.

3 Likes