Subtracting IntValue Returns a Negitive Number

Hello DevFourm, I have been working on a project where you click food to earn money, and you can use that money to upgrade to get more money when clicking (For now, the game is not public yet)

Anyways, I have a problem where when the player upgrades, I want it to remove the value of the upgrade price from the money value. When I do this, it returns a negative number which makes no sense since the upgrade value would always be greater than the money value.

Heres a video:

Heres the script in the upgrade button:

local VALUE = game.ReplicatedStorage.STATS.Upgrades.EatingSpeed
local BUTTON = script.Parent
local MONEY = game.ReplicatedStorage.STATS.Money
local UPGRADE_PRICE = script.Parent.VALUES.Price
local MULTIPLY_VALUE = script.Parent.VALUES.MULTIPLYBY
local LEVEL = script.Parent.VALUES.LEVEL

BUTTON.MouseButton1Click:Connect(function()
	   if MONEY.Value >= UPGRADE_PRICE.Value then
		 VALUE.Value += 8
		 UPGRADE_PRICE.Value += UPGRADE_PRICE.Value * 3
		 MULTIPLY_VALUE.Value += 1
		 LEVEL.Value += 1
		 MONEY.Value = MONEY.Value - UPGRADE_PRICE.Value -- !!!!The line I need help on!!!!
	 end
end)

-----------------------------------------------------
while wait() do
	script.Parent.Text = "$"..UPGRADE_PRICE.Value
	script.Parent.Parent.CURRENT_VALUE.Text = "Current Level: ".. script.Parent.VALUES.LEVEL.Value
end

Thanks for your help :slight_smile:

is it because you changed the price before subtracting the money, this would mean that it would subtract 200 from your current value

fixed:

local VALUE = game.ReplicatedStorage.STATS.Upgrades.EatingSpeed
local BUTTON = script.Parent
local MONEY = game.ReplicatedStorage.STATS.Money
local UPGRADE_PRICE = script.Parent.VALUES.Price
local MULTIPLY_VALUE = script.Parent.VALUES.MULTIPLYBY
local LEVEL = script.Parent.VALUES.LEVEL

BUTTON.MouseButton1Click:Connect(function()
	   if MONEY.Value >= UPGRADE_PRICE.Value then
         MONEY.Value -= UPGRADE_PRICE.Value
		 VALUE.Value += 8
		 UPGRADE_PRICE.Value += UPGRADE_PRICE.Value * 3
		 MULTIPLY_VALUE.Value += 1
		 LEVEL.Value += 1
	 end
end)

-----------------------------------------------------
while task.wait() do
	script.Parent.Text = "$"..UPGRADE_PRICE.Value
	script.Parent.Parent.CURRENT_VALUE.Text = "Current Level: ".. script.Parent.VALUES.LEVEL.Value
end

edit: -= is a thing, it works just like += but subtracts.

1 Like

Thanks, that fixed the problem :slight_smile:

1 Like

You check if they can afford the upgrade, then (in the line of code above) you raise the price of the upgrade to 4 times as much before you subtract the code from their wallet. They go negative because you’re charging them for 4 upgrades.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.