Help with tables

Hello,
I am trying to make an upgrade capacity script. But I’m having some issues.
Script:

local Player = game:GetService('Players').LocalPlayer
local Max = Player:WaitForChild('BackpackCapacity')
local Whacks = Player:WaitForChild('Whacks')
local MainFrame = script.Parent:WaitForChild('Frame')

local tFind,tInsert,tRemove = table.find,table.insert,table.remove

local CapacityAmounts = {
	
	Capacity0 = {
		Price = 0,
		Capacity = 10
	},
	
	Capacity1 = {
		Price = 50,
		Capacity = 25
	},
	
	Capacity2 = {
		Price = 100,
		Capacity = 50
	},
	
	Capacity3 = {
		Price = 250,
		Capacity = 100
	},
	
}

local function GetNextCapacity(CurrentValue)
	
	print(CurrentValue)
	
	CurrentValue = tostring(CurrentValue)
	
	local CurrentCapacity
	
	for _, aCapacity in pairs(CapacityAmounts) do
		if aCapacity.Capacity == CurrentValue then
			CurrentCapacity = aCapacity
			break
		end
	end
	
	if CurrentCapacity == nil then
		print('Unable to find the capacity in the table')
		return nil
	end
	
	local Calculation = tonumber(CurrentCapacity) + 1
	
	Calculation = tostring(Calculation)
	
	local NextCapacity = CapacityAmounts['Capacity'..Calculation]
	
	if NextCapacity then
		return NextCapacity
	else
		print('No next capacity, invalid num provided or last value in the table reached')
		return nil
	end
	
	
end

Unable to find the capacity in the table is printing. And where I do print(CurrentValue, it prints 25! But 25 is in my table, so I don’t understand it! Thanks for any help!

1 Like

The capacity values in the table are numbers, but you convert CurrentValue to string before you compare it to them. A string is never equal to a number.

Now, No next capacity, invalid num provided or last value in the table reached prints.

If you print CurrentCapacity, what is it? It seems like you are setting a table as its value.

How would I only get the number, so instead of getting ‘Capacity1’, to get ‘1’?

local numberAsAString = string.match(yourString, "%d+")

How would I get the name of a table?

I edited my script to this:

local CapacityAmounts = {
	
	Capacity0 = {
		Price = 0,
		Capacity = 10
	},
	
	Capacity1 = {
		Price = 50,
		Capacity = 25
	},
	
	Capacity2 = {
		Price = 100,
		Capacity = 50
	},
	
	Capacity3 = {
		Price = 250,
		Capacity = 100
	},
	
}

local function GetNextCapacity(CurrentValue)
	
	print(CurrentValue)
	
	
	local CurrentCapacity
	
	for _, aCapacity in pairs(CapacityAmounts) do
		if aCapacity.Capacity == CurrentValue then
			CurrentCapacity = aCapacity
			break
		end
	end
	
	if CurrentCapacity == nil then
		print('Unable to find the capacity in the table')
		return nil
	end
	
	local numberAsAString = string.match(CurrentCapacity.Name, "%d+")
	
	print(numberAsAString)
	
	local Calculation = tonumber(CurrentCapacity) + 1
	
	Calculation = tostring(Calculation)
	
	
	print('Capacity'..Calculation)
	
	local NextCapacity = CapacityAmounts['Capacity'..Calculation]
	
	if NextCapacity then
		return NextCapacity
	else
		print('No next capacity, invalid num provided or last value in the table reached')
		return nil
	end
	
	
end

But CurrentCapacity.Name is nil.

The name is given by pairs. It’s the value of the first loop variable, which is _ in your current code

1 Like