How do I find out how much a leaderstat changed by?

I have a Gui that pops up whenever a player gets points.

When they get points, I want the Gui to display the number that was just added to the total amount of points. Currently, the Gui only displays the total amount of points that have been acquired instead of just showing what was just added.

How do I make it so the TextLabel only displays what was just added and not the total amount of points?

for _,plr in pairs(game.Players:GetChildren()) do
	local stats = plr:FindFirstChild("leaderstats")
	if stats then
		stats["Points"].Changed:Connect(function()
			script.Parent.PointsAdded.TextLabel.Visible = true
			script.Parent.PointsAdded.TextLabel.Text ="+".. stats["Points"].Value 
			print(stats["Points"].Value)
        end)
    end
end
1 Like

Add a variable that states the stat’s previous value.

for _,plr in pairs(game.Players:GetChildren()) do
	local stats = plr:FindFirstChild("leaderstats")
	if stats then
		local prev = stats.Points.Value
		stats["Points"].Changed:Connect(function()
			script.Parent.PointsAdded.TextLabel.Visible = true
			script.Parent.PointsAdded.TextLabel.Text ="+".. (stats["Points"].Value-prev)
			prev = stats.Points.Value
			print(stats["Points"].Value)
        end)
    end
end
2 Likes

I see how that works now, thanks a ton!

2 Likes
local Game = game
local Players = Game:GetService("Players")

local Stats = {} --Table to cache stat's values.

local function OnChanged(Value, Stat, Player)
	print(string.format("Stat '%s' changed from %d to %d.", Stat.Name, Stats[Player][Stat], Value))
	Stats[Player][Stat] = Value
end

local function OnPlayerAdded(Player)
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "Leaderstats"

	local Knockouts = Instance.new("IntValue")
	Knockouts.Name = "Knockouts"
	Knockouts.Changed:Connect(function(Value) OnChanged(Value, Knockouts, Player) end)
	Knockouts.Parent = Leaderstats

	local Wipeouts = Instance.new("IntValue")
	Wipeouts.Name = "Wipeouts"
	Wipeouts.Changed:Connect(function(Value) OnChanged(Value, Knockouts, Player) end)
	Wipeouts.Parent = Leaderstats

	Leaderstats.Parent = Player

	Stats[Player] = {[Knockouts] = 0, [Wipeouts] = 0}

	Knockouts.Value = 123
end

local function OnPlayerRemoving(Player)
	Stats[Player] = nil --Prevent memory leaks.
end

Players.PlayerAdded:Connect(OnPlayerAdded)
Players.PlayerRemoving:Connect(OnPlayerRemoving)

image