New stat showing incorrect difference between previous

Hello everyone! :wave: :slightly_smiling_face:
So I was creating a UI that would show how much a stat has changed compared to the value the stat had before it changed. But how much the stat changed is doubled for both XP as Cash. This is the script:

local player = game:GetService("Players").LocalPlayer
local TweenService = game:GetService("TweenService")

local info = TweenInfo.new(.5, Enum.EasingStyle.Linear)

local XPData = player:WaitForChild("LevelData").Exp
local CashData = player:WaitForChild("leaderstats").Cash

local Template = script.Template

local OnGameJoinXP = XPData.Value
local OnGameJoinCash = CashData.Value

local oldXP = {}
local oldCash = {}

table.insert(oldXP, OnGameJoinXP)
table.insert(oldCash, OnGameJoinCash)

XPData.Changed:Connect(function(newValue)
	task.spawn(function()
		local Text = Template:Clone()
		Text.TextLabel.TextTransparency = 1
		Text.Parent = script.Parent
		Text.TextLabel.Text = "+ "..tostring(newValue - oldXP[1]).." XP"
		
		table.insert(oldXP, newValue)
		oldXP[#oldXP] = nil
		
		TweenService:Create(
			Text.TextLabel,
			info,
			{ TextTransparency = 0 }
		):Play()

		task.wait(3)

		TweenService:Create(
			Text.TextLabel,
			info,
			{ TextTransparency = 1 }
		):Play()
		
		task.wait(1)
		
		Text:Destroy()
	end)
end)

CashData.Changed:Connect(function(newValue)
	task.spawn(function()
		local Text = Template:Clone()
		Text.TextLabel.TextTransparency = 1
		Text.Parent = script.Parent
		Text.TextLabel.Text = "+ "..tostring(newValue - oldCash[1]).."$"
		
		table.insert(oldCash, newValue)
		oldCash[#oldCash] = nil
		
		TweenService:Create(
			Text.TextLabel,
			info,
			{ TextTransparency = 0 }
		):Play()

		task.wait(3)

		TweenService:Create(
			Text.TextLabel,
			info,
			{ TextTransparency = 1 }
		):Play()

		task.wait(1)

		Text:Destroy()
	end)
end)

Does anyone have an idea on how to fix this problem?

1 Like

Bump

Ignore this text (o_____o)

This is the cause:

		table.insert(oldXP, newValue)
		oldXP[#oldXP] = nil

The problem with this is that it basically inserts a value at the end of the table, in this case second position. On the next line you immediately set the new tablevalue to nil, leaving the one that should have been replaced left.

To solve this, you can simply just clear the table before inserting the newValue using table.clear(oldXP).

I’d honestly not recommend using a table for this at all. Since you’re only storing one value you can just reuse the OnGameJoinXP variable and keep the old xp there.

I tried your solution but it still shows the double amount of what gets added to the stat.

Really strange, I tried it out (both using the clear method and variable) and it worked perfectly fine. Have u tried printing the oldvalue and newvalue to see which one is messing it up?

Heres my variant without the usage of tables.

local player = game:GetService("Players").LocalPlayer
local TweenService = game:GetService("TweenService")

local info = TweenInfo.new(.5, Enum.EasingStyle.Linear)

local XPData = player:WaitForChild("LevelData").Exp
local CashData = player:WaitForChild("leaderstats").Cash

local Template = script.Template

local oldXP = XPData.Value
local oldCash = CashData.Value

XPData.Changed:Connect(function(newValue)
	local Text = Template:Clone()
	Text.TextLabel.TextTransparency = 1
	Text.Parent = script.Parent
	Text.TextLabel.Text = "+ "..tostring(newValue - oldXP).." XP"

	oldXP = newValue

	TweenService:Create(
		Text.TextLabel,
		info,
		{ TextTransparency = 0 }
	):Play()

	task.wait(3)

	TweenService:Create(
		Text.TextLabel,
		info,
		{ TextTransparency = 1 }
	):Play()

	task.wait(1)

	Text:Destroy()
end)

CashData.Changed:Connect(function(newValue)
	local Text = Template:Clone()
	Text.TextLabel.TextTransparency = 1
	Text.Parent = script.Parent
	Text.TextLabel.Text = "+ "..tostring(newValue - oldCash).."$"

	oldCash = newValue

	TweenService:Create(
		Text.TextLabel,
		info,
		{ TextTransparency = 0 }
	):Play()

	task.wait(3)

	TweenService:Create(
		Text.TextLabel,
		info,
		{ TextTransparency = 1 }
	):Play()

	task.wait(1)

	Text:Destroy()
end)

with tables

local player = game:GetService("Players").LocalPlayer
local TweenService = game:GetService("TweenService")

local info = TweenInfo.new(.5, Enum.EasingStyle.Linear)

local XPData = player:WaitForChild("LevelData").Exp
local CashData = player:WaitForChild("leaderstats").Cash

local Template = script.Template

local OnGameJoinXP = XPData.Value
local OnGameJoinCash = CashData.Value

local oldXP = {}
local oldCash = {}

table.insert(oldXP, OnGameJoinXP)
table.insert(oldCash, OnGameJoinCash)

XPData.Changed:Connect(function(newValue)
	task.spawn(function()
		local Text = Template:Clone()
		Text.TextLabel.TextTransparency = 1
		Text.Parent = script.Parent
		Text.TextLabel.Text = "+ "..tostring(newValue - oldXP[1]).." XP"
		
		table.clear(oldXP) -- clear out the old value / values
		table.insert(oldXP, newValue) -- add new old value

		TweenService:Create(
			Text.TextLabel,
			info,
			{ TextTransparency = 0 }
		):Play()

		task.wait(3)

		TweenService:Create(
			Text.TextLabel,
			info,
			{ TextTransparency = 1 }
		):Play()

		task.wait(1)

		Text:Destroy()
	end)
end)

CashData.Changed:Connect(function(newValue)
	task.spawn(function()
		local Text = Template:Clone()
		Text.TextLabel.TextTransparency = 1
		Text.Parent = script.Parent
		Text.TextLabel.Text = "+ "..tostring(newValue - oldCash[1]).."$"

		table.clear(oldCash)
		table.insert(oldCash, newValue)

		TweenService:Create(
			Text.TextLabel,
			info,
			{ TextTransparency = 0 }
		):Play()

		task.wait(3)

		TweenService:Create(
			Text.TextLabel,
			info,
			{ TextTransparency = 1 }
		):Play()

		task.wait(1)

		Text:Destroy()
	end)
end)

I got it working. Thanks for the help! So I found out, after I created a cash giving part which was working fine with the stat changing UI, that the problem was on the bank system side, because for some reason it gave twice the amount it’s supposed to give :laughing: . But anyway, once again thanks for the help.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.