Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Detect the difference between a value that has changed?

Asked by
Pengdoo 26
7 years ago

Hi there. Yes the title might seem weird but here let me try to explain my problem.

I want to make like some sort of cash gained indicator whenever the player gets cash so if they get 10 cash, it would say in a TextLabel +10$ or something.

The thing is, I'm trying to work out a way (and if it's even possible) to find the difference between a value that has changed. So, for instance: the player's cash is 3 and it has changed to 13. Is there a way for me to know that the difference is 10? Do I need to like, register the value before the change and after the change? And if that's the solution, how do I do it?

Now this is not really needed in my game it's just for aesthetic purposes

Any help would be much appreciated, thanks!

1 answer

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

You could use a variable that stated the last known value.

For example

local LastAmmount = 0

value.Changed:connect(function(property)
        if value.Value ~= LastAmmount then --Is the new value the same as the last?
            local change = value.Value - LastAmmount --Lets find the change

            LastAmmount = value.Value --Change the LastAmmount to the new value

            if change < 0 then --If the change is a negative, then we want it to dispaly as "-"
                print(change.."$")
            else --If the change is positive, then we want it to display as "+"
                print("+"..change.."$")
            end
        end
end)

Tested, and works.

Ad

Answer this question