Help with for loops with an if script

I dont understand by what you mean by any of the comments you added

and if you want to check if itemName is equal to sum do this :

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

    if Name == "bananananaananna" then
    -- do stuff
    else
       return
    end
end

Sure.

This is a dictionary. This means keys aren’t numbers, like they are normally. When you make a normal array:

local myArray = {
    "hi there",
    "hello"
}

it is actually laid out and referenced to like so:

local myArray = {
    [1] = "hi there",
    [2] = "hello"
}

The item on the left is the key, and the item on the right is the value. We use the key to reference the value in a script. So, if we said:
print(myArray[1]), it would look to what value is associated with the key 1, which is “hi there”.

In a dictionary, we don’t use the numerical key. I’m using the product’s name as a shortcut to that area of the array, storing the product’s info. So, if the products were:

local products = {
    ["Bread"] = {
        ["Name"] = "Bread",
        ["Cost"] = 2.99
    },
    ["Bananas"] = {
        ["Name"] = "Bananas",
        ["Cost"] = 2.99
    }
}

Each product name is holding information about that product.
So, the following would output:

print(products.Bread.Name) --prints "Bread"
print(products.Bananas.Cost) --prints "2.99"

The key is just a shortcut to that information. You can add as many products as you want.

Edit:
The square brackets are also an alternative to the dot method of referencing an array. Like so:

local product = "Bread" --you might not know what value this is
print(products[product].Name) --outputs whatever "Name" key is associated with the product name, if it is found.
1 Like

Screenshot 2024-03-10 095601
Im getting some errors when I put that in my code and I got to eat breakfast

Yes, that code wasn’t meant to be used in the module. It was just an example. For your module:

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

Examples:

print(products.Products.Bread.Name) --prints "Bread"
print(products.Products.Bananas.Cost) --prints "2.99"

I would suggest using this module, right now is a file on the Statergui folder named Products:

local module = {}

module.Products = {
	Bread = {
		Label = "Super Bread",
		Price = 1.99
	},
	Orange = {
		Label = "Orange Max",
		Price = 2.99
	},
	Apple = {
		Label = "Apple Fun",
		Price = 3.99
	}
}

return module

Now just for test create a Part name it button add a clickDectector with a script inside. (Please dont add localscript, must be script) Just we can test the module.

local clickDetector = script.Parent

local function onClicked(player)
	local backpack = player:FindFirstChild("Backpack")	
	if backpack and #backpack:GetChildren() > 0 then
		local ProductsModule = require(game:GetService("StarterGui"):WaitForChild("Products"))
		local priceSum = 0.00
		for _, tool in backpack:GetChildren() do			
			if tool:IsA("Tool") then
				if ProductsModule.Products[tool.Name] ~= nil then
					print("Item found: ",ProductsModule.Products[tool.Name].Label)
					print("Item Price: ",ProductsModule.Products[tool.Name].Price)
					print("------------------------")
					priceSum = priceSum + tonumber(ProductsModule.Products[tool.Name].Price)
				end
			end
		end
		print("Total is:",priceSum)
	else
		print("Doesn't have a backpack or empty.")
	end
	
end

clickDetector.MouseClick:Connect(onClicked)

Now at StarterCharacterScripts add tool named “Orange”, “Bread” and another like “Example”. The name of the tool is important because it is how we are going to use to access the module. Now please remember to equip a tool before clicking the Part.

Do you had success trying the new module?

Im not making a item grab system, Im making a store checkout POS.

I dont understand. The itens are in the backpack?

You scan the item thats a tool on the checkout when it touches the scan part, then it appears on screen. After that when all items are done scanned, you can pay for your items on the card reader/pinpad thingy.

Is the item a tool and the scanner a part?

Yes, The item is a tool which has an identifer that is an int value inside of it so the checkout knows when a scanable item has been scanned, and it uses that value to find which product it is inside of a module script.

Use this module:

local module = {}

module.Products = {
	[123]= {
		Label = "Super Bread",
		Price = 1.99
	},
	[345] = {
		Label = "Orange Max",
		Price = 2.99
	},
	[334] = {
		Label = "Apple Fun",
		Price = 3.99
	}
}

return module

In studio create a part and add this script to it:

local part = script.Parent
local debounce = false

-- Function to handle when a player touches the part
local function onTouch(hit)
	if debounce then
		return  -- If debounce is true, exit the function
	end

	local character = hit.Parent
	local ProductsModule = require(game:GetService("StarterGui"):WaitForChild("Products"))

	local humanoid = character:FindFirstChild("Humanoid")

	if humanoid then
		print("Player touched the part!")	

		local player = game.Players:GetPlayerFromCharacter(character)		
		if character:FindFirstChildOfClass("Tool") then
			local tool = character:FindFirstChildOfClass("Tool")
			
			-- Read the id from the tool value Int
			local itemToScan = tool:FindFirstChild("Value").Value			
			
			
			if ProductsModule.Products[itemToScan] ~= nil then -- Search the module for the id
				print("Item found: ",ProductsModule.Products[itemToScan].Label)
				print("Item Price: ",ProductsModule.Products[itemToScan].Price)
				print("------------------------")				
			end			
		end
		debounce = true
		wait(2)
		debounce = false
		-- Your code for what happens when a player touches the part goes here
	end
end

-- Connect the onTouch function to the Touched event of the part
part.Touched:Connect(onTouch)

Nesting if statements can make your code a bit confusing to read. Instead try to use early returns:

if A then
   if B then
      If C then
         RunCodeHere()
      end
   end
end

Could instead be:

if (not A) or (not B) or (not C) then return end
RunCodeHere()

or

if not A then return end
if not B then return end
if not C then return end
RunCodeHere()