Help with for loops with an if script

Im making a store checkout POS. Read below to see my problem.

I need help figuring out how to check all things from a module script using a for loop, an d then seeing if one of those equals a specfic value from a tool that was scanned, and if it does then recieve data from the module script for that specfic value found inside the tool scanned.

The value to be checked is an IntValue called POSitem inside the tool, which it should be checked when the item gets scanned, which I don’t know how to do that because I don’t even really know how for loops work, as I never really used them before in the past.

I made another post about this a few weeks ago, which I decided to make a new one because everyone who replied is posting a bunch of scripts that dont work for the thing Im trying to make, probably because I didnt explain it very well, but now Im trying my best to explain it here. and now the few people who orginal replied to me dont even really say anything no more, probably because it’s a really old post. Im new to the devforum just got in like 2 months ago.

My script:

local products = require(script.Parent.Products) --supposing that this is the module
local screen = script.Parent.Screen
local ui = screen.SurfaceGui.Frame.Background
local start = ui.Start
local cancel = ui.Cancel
local currentpriceui = ui.CurrentPrice
local currentprice = script.currentprice
local sensor = script.Parent.Sensor

local valueToCheck = 0

function onScan(product)


	-- Code here is what is supposed to check the module script
	
	
	
end

script.Parent.Sensor.Touched:Connect(function(item)
	local it = item.Parent:FindFirstChildWhichIsA("Tool")
	if it then -- Here is where Im stuck trying to figure out how to check if the POSitem value equals one of the things in the module script
		if it.Handle:FindFirstChild("POSitem") then
			print(`POSitem exist inside {it.Name}`)
			for i, v in pairs(products.Products) do
				if products.Products[tostring(i)]["Name"] == it.Handle.POSitem.Value then
					print("It is a valid part")
					valueToCheck = it.Handle.POSitem.Value
					onScan(it.Handle)
				end
			end
		else
			print(`POSitem dont exist inside {it.Name}`)
		end
	else
		print("item not found inside the character.")
	end
end)

And the module script:

local module = {}

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

return module

So the whole point of it is, is when ever someone scans an item, if it is a valid item that has the intvalue “POSitem” inside of the tool, it will use that number inside of the intvalue and check it with the module script. For example, if it was the number 1, it will find the one value inside of the module script and grab back the Name and Price value, which the price will add onto the current price value inside of the main script, and the name of the item will appear on screen. After all items are scanned, the cashier can press start transaction which will activate the card reader/pinpad thingy for the customer to pay. After the customer pays, the current price value will be set back to 0 and all item names on screen will dissapear. Which right now Im mainly focusing on the scanning process to work.

1 Like

If I’ve understood this correctly, you want to iterate through the module, and if it matches with a number, return the name and price? Well, you could try this:

local price
local name
for i, v in pairs(module.Products) do
    if tonumber(i) == requirement then
        name = v.Name
        price = v.Price
        break
    end
end
2 Likes

Screenshot 2024-03-10 090208
Is this where I put it?

1 Like

Change the “module” part to the name of your module, but yes. Make sure you have the required number too.

1 Like

And in the module script, does this need to be an interger and not a string?
Screenshot 2024-03-10 090357

1 Like

I’ve written it so the ["1"] part would work as a string. If you want to use an integer, I can rewrite my previous code for you. It would let you use the ipairs() function, which iterates faster than pairs().

2 Likes

Screenshot 2024-03-10 090842
I tried adding it so it prints like this but it doesn’t print anything for some reason.

Might be because it didn’t find anything. Add a print() statement before break, and let me know if that prints anything. Also make it print what value you are trying to check.

It is fine if it is a string as long as it is type casted (change its data type to a number (notice, it is not integer as it involves decimals, in other programming language, this is called a Float or Double). For the sake of brevity and readability, do opt for having it as a number type initially

Now for your code and referring to what @12345koip said,

local products = require(script.Parent.Products)

Do note that that for this code: https://devforum-uploads.s3.dualstack.us-east-2.amazonaws.com/uploads/original/5X/b/1/2/d/b12dd37da6f8950d8d794120170d134097613c11.png
, change “module”, with the actual name of your module imported variable of “products”

Cool, bye

1 Like

It still doesn’t print anything when I try doing it like that.

1 Like

I did already replace module with the Products inside of the module script

1 Like

Ok, for the sake of debugging, try this code and let me know what prints and what doesn’t.

local function onScan(product)
    local price
    local name
    print(product)
    print(valueToCheck)
    for i, v in pairs(products.Products) do
        if tonumber(i) == valueToCheck then
            name = v.Name
            price = v.Price
            print(name, price)
            break
        end
    end
end
2 Likes

Still doesnt print price or name or value to check

1 Like

What did product and valueToCheck come out as?

1 Like

in your for loop, can you just print out I and v at each loop to see what the computer sees please. Make sure this is nested inside the loop

1 Like

It didnt print anything out besides the code from the very bottom that shows if the tool has pos item inside of it

1 Like

If none of the print statements outputted anything, that means product and valueToCheck are nil. Can I see the rest of your code, where those variables are defined?

2 Likes
local products = require(script.Parent.Products)
local screen = script.Parent.Screen
local ui = screen.SurfaceGui.Frame.Background
local start = ui.Start
local cancel = ui.Cancel
local currentpriceui = ui.CurrentPrice
local currentprice = script.currentprice
local sensor = script.Parent.Sensor

local valueToCheck = 0

function onScan(product)
	
	
	
	local price
	local name
	print(product)
	print(valueToCheck)
	for i, v in pairs(products.Products) do
		
		print(i)
		print(v)
		if tonumber(i) == valueToCheck then
			name = v.Name
			price = v.Price
			print(name, price)
			break
		end
	end

	
end

script.Parent.Sensor.Touched:Connect(function(item)
	local it = item.Parent:FindFirstChildWhichIsA("Tool")
	if it then -- Here is where Im stuck trying to figure out how to check if the POSitem value equals one of the things in the module script
		if it.Handle:FindFirstChild("POSitem") then
			print(`POSitem exist inside {it.Name}`)
			for i, v in pairs(products.Products) do
				if products.Products[tostring(i)]["Name"] == it.Handle.POSitem.Value then
					print("It is a valid part")
					valueToCheck = it.Handle.POSitem.Value
					onScan(it.Handle)
				end
			end
		else
			print(`POSitem dont exist inside {it.Name}`)
		end
	else
		print("item not found inside the character.")
	end
end)

The for loop is probaly not running at all if its not printing anything

1 Like

ok for this part when you loop, remove the “pairs” function, I think it adds unnecessary index cus you already have an index separating it there

Also, if you want to create your own index primary key, do not have the unnecessary complexion of making the 1 an array, rather, make it an integer

Not:

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

But,

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

Next, change:

local function onScan(product)
    local price
    local name
    print(product)
    print(valueToCheck)
    for i, v in pairs(products.Products) do
        if tonumber(i) == valueToCheck then
            name = v.Name
            price = v.Price
            print(name, price)
            break
        end
    end
end

To:

local function onScan(product)
    local price
    local name
    print(product)
    print(valueToCheck)
    for i, v in products.Products do
        if tonumber(i) == valueToCheck then -- Notice I removed the pairs here
            name = v.Name
            price = v.Price
            print(name, price)
            break
        end
    end
end
1 Like