Number being added on to original

Basically whenever a players data has changed, this function is fired and the goal of the ‘OriginalCoins’ is to basically see how much coins they spent.

So the idea behind this was to see how many coins the player originally had, minus their coins now and that would show how much coins they spent right. However I get a problem if I buy a ton of stuff at once, it adds up


As you can see in the red text, it keeps adding 5 everytime I buy an item (in quick succession) However the item only costs 5 coins. So it should say -5 5 times

local function updateData(data)
	if not OriginalCoins then
		OriginalCoins = data.Coins
	end
	
	CoinsAmount.Text = data.Coins
	
	if OriginalCoins > data.Coins then
		local NewCoins = Instance.new('TextLabel')
		NewCoins.BackgroundTransparency = 1
		NewCoins.Size = UDim2.new(1, 0, 0.25, 0)
		NewCoins.Font = 'GothamBlack'
		
		NewCoins.TextColor3 = Color3.fromRGB(255, 50, 50)
		NewCoins.Text = '-' .. OriginalCoins - data.Coins
		
		NewCoins.TextScaled = true
		NewCoins.TextXAlignment = 'Left'
		
		NewCoins.Parent = CoinsSpent
		
		wait(3)
		NewCoins:Destroy()
	end
	
	if OriginalCoins > data.Coins then
		OriginalCoins = data.Coins
	end
end

Im not sure if this is right but should you update the actual value of the original coins in the
if OriginalCoins > data.Coins statement like this

OriginalCoins = OriginalCoins - data.Coins
1 Like

Didn’t work, caused it only say -5 and not create any new ones

local function updateData(data)
	if not OriginalCoins then
		OriginalCoins = data.Coins
	end
	
	CoinsAmount.Text = data.Coins
	
	if OriginalCoins > data.Coins then
		local NewCoins = Instance.new('TextLabel')
		NewCoins.BackgroundTransparency = 1
		NewCoins.Size = UDim2.new(1, 0, 0.25, 0)
		NewCoins.Font = 'GothamBlack'
		
		NewCoins.TextColor3 = Color3.fromRGB(255, 50, 50)
		NewCoins.Text = '-' .. OriginalCoins - data.Coins
		
		NewCoins.TextScaled = true
		NewCoins.TextXAlignment = 'Left'
		
		NewCoins.Parent = CoinsSpent
		
		if OriginalCoins > data.Coins then
			OriginalCoins = OriginalCoins - data.Coins
		end
		
		wait(3)
		NewCoins:Destroy()
	end
end

Try doing your function like this instead

local function updateData(data)
	local OriginalCoins = tonumber(CoinsAmount.Text)	
	CoinsAmount.Text = data.Coins
	
	if OriginalCoins > data.Coins then
		local NewCoins = Instance.new('TextLabel')
		NewCoins.BackgroundTransparency = 1
		NewCoins.Size = UDim2.new(1, 0, 0.25, 0)
		NewCoins.Font = 'GothamBlack'
		
		NewCoins.TextColor3 = Color3.fromRGB(255, 50, 50)
		NewCoins.Text = '-' .. OriginalCoins - data.Coins
		
		NewCoins.TextScaled = true
		NewCoins.TextXAlignment = 'Left'
		
		NewCoins.Parent = CoinsSpent
		
        wait(3)
		NewCoins:Destroy()
	end
end