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!
Here is my algorithm for controlling the display text of the notification system.
It should also be noted that “NewValue” is stored in a function variable, like this:
function(NewValue)
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?
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.
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.
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
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.