Module issue with looping through table

I have a module that contains a table and a few functions. Within that module, there is a function called ‘TotalWorkerCPS’. I need to loop through the table called ‘Upgrades’ in a for loop in order to calculate a value. The code never loops through the table however.

For the life of me, I do not know why my for loop gets skipped each time in this function.

I used breakpoints to debug and the table gets recognized when I hover over it and it lists all items in the table.

I can access and loop through the table from another script without a problem, but not within the module.

I’m sure the solution is probably obvious, but I cannot see it.

Any help or pointers would be much appreciated :slight_smile:

local module = {}


--------- Upgrade Items Available ---------------
module.Upgrades = {
	["Worker1"]= {name = "Worker"; statName = "Worker1"; basePrice = 100; baseCpS = 1; RReq = 0; PReq =""};
	["Worker2"]= {name = "Pet"; statName = "Worker2"; basePrice = 1100; baseCpS = 8; RReq = 0; PReq ="Worker1"};
	["Worker3"]= {name = "Farm"; statName = "Worker3"; basePrice = 12000; baseCpS = 47; RReq = 0; PReq ="Worker2"};
	["Worker4"]= {name = "Factory"; statName = "Worker4"; basePrice = 130000; baseCpS = 260; RReq = 0; PReq ="Worker3"};

function module.TotalWorkerCPS(plr)
	local plrStats = plr:WaitForChild("PlayerStats")
	local totalWorkerMulti = 0
		
	for i,v in ipairs (module.Upgrades) do
		print("calculating total workerCPS ")
		totalWorkerMulti = totalWorkerMulti+((plrStats:FindFirstChild(v.statName).Value) * v.baseCpS)
	end
	
	return totalWorkerMulti
end



return module

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

use pairs instead of ipairs, you cant order a dictionary

1 Like

Did you by any chance forget to put a } to end the declaration?

module.Upgrades = {
	["Worker1"]= {name = "Worker"; statName = "Worker1"; basePrice = 100; baseCpS = 1; RReq = 0; PReq =""};
	["Worker2"]= {name = "Pet"; statName = "Worker2"; basePrice = 1100; baseCpS = 8; RReq = 0; PReq ="Worker1"};
	["Worker3"]= {name = "Farm"; statName = "Worker3"; basePrice = 12000; baseCpS = 47; RReq = 0; PReq ="Worker2"};
	["Worker4"]= {name = "Factory"; statName = "Worker4"; basePrice = 130000; baseCpS = 260; RReq = 0; PReq ="Worker3"};

Shouldn’t it be?

module.Upgrades = {
	["Worker1"]= {name = "Worker"; statName = "Worker1"; basePrice = 100; baseCpS = 1; RReq = 0; PReq =""};
	["Worker2"]= {name = "Pet"; statName = "Worker2"; basePrice = 1100; baseCpS = 8; RReq = 0; PReq ="Worker1"};
	["Worker3"]= {name = "Farm"; statName = "Worker3"; basePrice = 12000; baseCpS = 47; RReq = 0; PReq ="Worker2"};
	["Worker4"]= {name = "Factory"; statName = "Worker4"; basePrice = 130000; baseCpS = 260; RReq = 0; PReq ="Worker3"};
}

Sorry if it’s not the answer, it was the first thing that I noticed

Edit: @PostVivic’s is most likely the correct one since ipairs is typically used with lists and pairs is msotly used whe nyou need no order and don’t really work well with dctionaries

1 Like

Omg, thank you so much!! Can’t believe I didn’t think to look at that. :sweat_smile:

It works now.

Thank you for the reply. I did have the } in the code, but it got lost when I copy and pasted the code on here.

It was indeed the pairs vs. ipairs that was the issue.

I see, I’m glad that you managed to get your solution at the end! If you more issues, you can post them in #help-and-feedback:scripting-support and I or others can help you!

1 Like