Adding/Subtracting Difference between IntValue Changes

I know this topic has been brought up before and Ive seen the basic solutions, but i need advice on how to handle this issue on a larger level.

This script is for my inventory syste, it detects when the value in the players “inventory” folder changes. That all works. However I have a section that creates a notification for when the value of an item goes up, like you get “+1 wheat” or “-2 Corn” etc.

The problem I have with this current code is that it starts with the initial value of 0 then goes up by whatever number for whatever vlue, say Corn goes up by one. Then when I get another corn it follows the math. However if I get 2 corn, then go get a potato, the value which will start at 0, it does the math between having 2 corn, and then getting +1 potato.

Idk if that explains it well, but thats the issue. Ive never done stuff like this with int values before.

for i,v in pairs(ItemsInInventory) do
		FirstVal = v.Value
		v.Changed:Connect(function()
			if v.Value > FirstVal then
				local Notification = game.ReplicatedStorage.UIStorage.Notification:Clone()
				Notification.Parent = script.Parent.Parent.PlayerMenu.InventoryNotification
				Notification.Script.Enabled = true
				Notification.TextLabel.Text = ("+"..(v.Value - FirstVal).." "..v.Name)
			else
				local Notification = game.ReplicatedStorage.UIStorage.Notification:Clone()
				Notification.Parent = script.Parent.Parent.PlayerMenu.InventoryNotification
				Notification.Script.Enabled = true
				Notification.TextLabel.Text = ("-"..(v.Value - FirstVal).." "..v.Name)
			end
			FirstVal = v.Value
			
			
		end)
				
	end

I hope I explained it well, im happy to go into more detail if it helps.

Also im sorry if the answer is glaringly obvious and im not seeing it lol

  1. Put this topic in Help and Feedback > Scripting Support, not creations feedback.
  2. Replace that with local FirstVal = v.Value and remove references to FirstVal outside of the loop. You’re using a global variable for that, so any changes to it made by any item will overwrite other values.

oops lol. Thanks. And thanks for the edit, that worked perfectly!

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