Upgrade system not working

Hello,
So I made an upgrade system for my simulator
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 RS = game:GetService('ReplicatedStorage')
local Remotes = RS:WaitForChild('Remotes')

local NextPrice1
local NextCapacity1

local CapacityAmounts = {
	
	Capacity0 = {
		Number = 0,
		Price = 0,
		Capacity = 10
	},
	
	Capacity1 = {
		Number = 1,
		Price = 50,
		Capacity = 25
	},
	
	Capacity2 = {
		Number = 2,
		Price = 100,
		Capacity = 50
	},
	
	Capacity3 = {
		Number = 3,
		Price = 250,
		Capacity = 100
	},
	
	Capacity4 = {
		Number = 4,
		Price = 800,
		Capacity = 500
	},
	
	Capacity5 = {
		Number = 5,
		Price = 1000,
		Capacity = 800
	},
	
	Capacity6 = {
		Number = 6,
		Price = 2500,
		Capacity = 2000
	},
	
	Capacity7 = {
		Number = 7,
		Price = 8000,
		Capacity = 6500
	},
	
}

local function GetNextCapacity(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.Number) + 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

wait(math.random(2,5))

local OldCapacity = Max.Value
local NextCapacity = GetNextCapacity(Max.Value)




if NextCapacity then
	NextPrice1 = NextCapacity.Price
	NextCapacity1 = NextCapacity.Capacity
	MainFrame.Description.Text = 'Upgrade Capacity from '.. tostring(Max.Value)..' whacks to '.. tostring(NextCapacity.Capacity)..' whacks for '.. tostring(NextCapacity.Price)..' bux?'
end


Max:GetPropertyChangedSignal('Value'):Connect(function()
	wait()
	local OldCapacity = Max.Value
	local NextCapacity = GetNextCapacity(Max.Value)
	
	if NextCapacity then
		NextPrice1 = NextCapacity.Price
		NextCapacity1 = NextCapacity.Capacity
		MainFrame.Description.Text = 'Upgrade Capacity from '..Max.Value..' whacks to '..NextCapacity.Capacity..' whacks for '..NextCapacity.Price..' bux?'
	end
end)

MainFrame.UpgradeButton.MouseButton1Click:Connect(function()
	local Result = Remotes.CanAfford:InvokeServer('Capacity',NextPrice1)
	
	if Result then
		local BuyItem = Remotes.BuyItem:InvokeServer('Capacity',NextPrice1,NextCapacity.Capacity)
		
		if BuyItem then
			workspace.SoundEffects.CashOut:Play()
		else
			Player:Kick('Exploiting')
		end
	else
		workspace.SoundEffects.Error:Play()
	end
end)



The problem is, that when I upgrade the first time, it works perfectly, but when I press it again, I loose my money but the capacity doesn’t change. The next time it works! But I don’t know why this happens! I get no errors.

Well, it appears you are using the script I provided earlier. But as to the current problem it might be because you need to update it each time you try buying something. Other than that, I see no problems.

Within the function you’re redefining OldCapacity and NewCapacity by adding the word “local” beforehand. This means its going to create new variables specifically for the function, not something you need in this case.

To fix this, all you have to do is remove the “local” from both the variables:

Max:GetPropertyChangedSignal('Value'):Connect(function()
	wait()
	OldCapacity = Max.Value
	NextCapacity = GetNextCapacity(Max.Value)
	
	if NextCapacity then
		NextPrice1 = NextCapacity.Price
		NextCapacity1 = NextCapacity.Capacity
		MainFrame.Description.Text = 'Upgrade Capacity from '..Max.Value..' whacks to '..NextCapacity.Capacity..' whacks for '..NextCapacity.Price..' bux?'
	end
end)

This will modify existing variables in the script instead of creating new ones for the function. Hope this helps!! :slight_smile:

1 Like