Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Detect the difference between a value that has changed?

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

✓ Accepted answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.