Help with for loops with an if script

You already iterate, then check a value again… And you pass an instance, which doesn’t like to pass to functions. Try changing this loop to:

for i, v in pairs(products.Products) do
    print(i, v) --let me know what outputs here
    if v.Name == it.Handle.POSItem.Value then
        --do other stuff here
    end
end
1 Like

Where do I put that in the script?

Replace the other loop under script.Parent.Sensor.Touched:Connect(function() --etc, etc.

That just shows a ton of errors when I try doing that
Screenshot 2024-03-10 092801

Apologies, silly me, encase the 1 with brackets, but not have it as a string: [1]

Screenshot 2024-03-10 093015
Like this?

Yes, like that, but replace “do other stuff here” with what you want to happen if it finds the item.

What I want it to do is go through the module script to see if something equals something, and if it can find a match, then it will grab product name and price, but I don’t know how to do that like I said at the beggining of this post.

Which is what Im trying to figure out here

1 Like

What you need to do this:

  1. The product’s name, as a string.
  2. The table of elements to be iterated over.

An example array I will use:

module.Products = {
    ["Bread"] = {
        ["Name"] = "Bread",
        ["Cost"] = 2.99
    }
}

Next, we aren’t going to iterate. If we have the name as a string, my example array above uses that as the key. We could simply do:

local name
local cost

if module.Products[productNameHere] then --"productNameHere" would need to be equal to "Bread"
    name = module.Products[productNameHere].Name
    cost = module.Products[productNameHere].Cost
end
1 Like


So do I put on scan here like this?

What is POSItem’s value equal to?

I want it to be able to be compatiable with multiple differnet products/items, and not just one, just by adding another POSitem into the tool in just configuring it in the products script for ease of use.

Screenshot 2024-03-10 093914
Its equal to 1
Omgggg why do I always accdently mark the solution when Im trying to heart something

You would just add your other products into the array, like:

["Bananas"] = {
    ["Name"] = "Bananas",
    ["Cost"] = 0
}

POSItem is a StringValue, so the number content is expressed as a string. This is not good for storing a number - you are therefore comparing a string and a number, which don’t mix. Just store the product’s name inside of POSItem, and use my example above that searches for the string key in the array.

So do I just store the products name inside of POS item, and whats the point in having to type in Bananas two different times?

One is for the key, and one is for the name. Setting the name variable equal to the key would store the secondary array inside, which we don’t want - instead we store the ["Name"] key.

Can you explain a little bit more about what you mean by secondary array?


And If I set product name here to bread, doesn’t that mean it will only be able to scan bread, which is a single item, which is I dont want that because I want it to scan multiple items with the ability to easily add more items

Module :


return {
 ["Bananas"] = {
     ["Name"] = "Bananas",
     ["Cost"] = 0
 }
}

Other script :

-- i recommend using promise but I won't use it in this example

for itemName, itemData in pairs(your_required_module) do
     local Name = itemData.Name or "undefined"
     local Cost = itemData.Cost or 0

   -- do stuff
end

-- if you want to index thru it directly

local Name = your_required_module[itemName].Name or nil
local Cost = your_required_module[itemName].Cost or nil