I was wondering how can I get the previous value of a leaderstat and then get the changed value of it, then subtracting it, giving me the difference of the 2. I’ve had problems on how to do this and have tried multiple things, but nothing seems to work. In some games you see when you get currency added to your stats it will do an animation on how much you got. Here is some code I tried:
local function GetDifferenceGold(plr, newGoldVal)
local leaderstats = plr:WaitForChild("leaderstats")
local gold = leaderstats:WaitForChild("Gold")
local difference = gold.Value - newGoldVal
return difference
end
new.Changed:Connect(function(val)
if new.Name == "XP" then
local XP = plr.leaderstats.XP
local MaxXP = plr.leaderstats.MaxXP
local Level = plr.leaderstats.Level
if XP.Value >= MaxXP.Value then
XP.Value -= MaxXP.Value
MaxXP.Value *= 1.25
Level.Value += 1
end
elseif new.Name == "Level" then
local XP = plr.leaderstats.XP
local MaxXP = plr.leaderstats.MaxXP
local Level = plr.leaderstats.Level
if XP.Value >= MaxXP.Value then
XP.Value -= MaxXP.Value
MaxXP.Value *= 1.15
Level.Value += 1
end
elseif new.Name == "Gold" then
local diff = GetDifferenceGold(plr, new.Value)
print(diff)
end
At the end I tried to print difference, but it just prints 0 everytime. If anyone has any idea on how to do this it would be appreciated!