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!
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.
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