Value Checking Algorithm Displays Wrong Output Occasionally

Hello, everyone!

I’m currently trying to fix my notification system, which will, on occasion, display the incorrect number of resources you gained or lost. I am not sure why this happens, but I’m certain it has to do with my algorithm.
To explain the problem better, if you have 100 wood, and you gain, say, 7 wood, it’s supposed to display as 7 wood. However, it will sometimes display as the player’s entire wood resource, which would be at this point, 107. And when taking away from the resource value, it will display as “-107” instead of “-7.”

Any help would be greatly appreciated! :slight_smile:
Here is my algorithm for controlling the display text of the notification system.

local ChangedValue = NewValue - OldValue
OldValue = NewValue
notif.Amount.Text = ChangedValue

It should also be noted that “NewValue” is stored in a function variable, like this:


Another thing too, is if you lose resources, and then gain resources, it still displays it as a negative number, with how much wood you had before the gain. Same applies for when you lose resources, it flips it.

Your code snippets seem alright. Is there possibly another currency accessing the same function and ‘OldValue’? If so, you should pass a ‘Currency’ parameter to the function and use a table to store old values for each of the currencies. If this is not the case, care to share a little more of your code?

1 Like

Uh, yes.

I do happen to have another “Currency” accessing the same algorithm. I can share some more code snippets. :slight_smile:

local woodRe = resources:WaitForChild("Wood")
local stoneRe = resources:WaitForChild("Stone")


    	local ChangedValue = NewValue - OldValue
	    OldValue = NewValue

		notif.Amount.Text = ChangedValue

    	local ChangedValue = NewValue - OldValue
	    OldValue = NewValue
		notif.Amount.Text = ChangedValue

I’m certain there’s a better way to check the values of multiple values, but I am not sure.

Are these in the same script? If that’s the case, your stone and wood resource are sharing the same global “OldValue” variable, which will cause some odd behavior.

Yes, they are part of the same script. I was kind of wondering if that could be causing my issue.

In this case you should probably separate your OldValue into two different variables: OldStoneValue and OldWoodValue

1 Like


I’ll see how that works out. :slight_smile:

Edit: This fixed my problem! Thank you so much. :slight_smile:

One small problem.

It still displays the first time as +999999, which is how much stone I have in my inventory ( for testing purposes.)

Not sure why that is still occurring now.

It might be what you are initially setting the variables to?
Like do you have it as

local OldStoneValue = resources:WaitForChild("Stone").Value
local OldWoodValue = resources:WaitForChild("Wood").Value

I think so.

The wood and stone values are loaded from a datastore, so they can be anything from 0 up to inf pretty much, depending on if they’re a new or veteran player of the game.

How would I make it not display the current amount of wood or stone the player has the first time it shows up?

One thing you could possibly do is have a “loaded” boolean variable that only is set to true after the data is loaded, and then have some kind of check in each of your .Changed functions that looks like

if not loaded then return end

You can also not make the connections until the data is loaded

It is loaded, which is the problem. It displays the entire amount, instead of changes for whatever reason.

What @Brick_man is saying is that the .Changed connection is probably being created before the data actually loads. Thus, when the data loads, the connections fire with what should be the initial amount.

Like he said before, you can fix this by either using a “loaded” variable to determine if the initial values have already been set up, or you can change the order so that the initial values are loaded before the .Changed connections are created.

I’ll look into it, but I’m not entirely sure what you mean by loaded, since the Resource values are loaded - otherwise the notification stream wouldn’t fire an event - same deal with any of the other tools in my game.

I think it has to do with something else perhaps, not entirely sure though. I don’t see how a loaded variable would help, though. I’ll see if adding a .Value to the variables would fix the problem.

Edit: That fixed my issue. Thank you guys for the help. :slight_smile: