Number Commas + Tweening

How would I go about modifying this script so it can have commas when reaching 1,000, 10,000 etc.
I know how to do it normally but not with the tweening included so I need some help please.

local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(.3)

local label = script.Parent:WaitForChild("BalanceNum")

local plrs = game:GetService("Players")
local plr = plrs.LocalPlayer

local plrStats = plr:WaitForChild("leaderstats")
local plrMoney = plrStats:WaitForChild("Crash")

local tweenNumber = script.Parent:WaitForChild("TweenNumber")
local goal = {}

local function RoundNumber(number)
	return(math.floor(number+.5))
end

wait(.5)

label.Text = tostring(plrMoney.Value)
function plrMoneyChanged(value)
	goal.Value = value
	tweenService:Create(tweenNumber, tweenInfo, {Value = goal.Value}):Play()
	
	tweenNumber.Changed:Connect(function(tweenValue)
		local newTweenValue = RoundNumber(tweenValue)
		label.Text = tostring(newTweenValue)
	end)
	
end

plrMoney.Changed:Connect(plrMoneyChanged)

number tweening when value increases ^ I want the number tweening but also have commas.

If any of you can help it would be greatly appreciated!

This post might help: How would i make a large number have commas? - #7 by bellaouzo

1 Like

yea ive seen those but I have no clue how to add tweening to that properly each time I do there is an error.

Hi, there might be an easier way, but

local commaString = ""
local valueString = tostring(value)
local valueKilos = 1
while 10 ^ (valueKilos * 3) < value do
	local seperatedKilo = string.sub(valueString, #valueString - 3 * valueKilos + 1, #valueString - 3 * (valueKilos - 1))
	commaString = "," .. seperatedKilo .. commaString
	valueKilos += 1
end
commaString = string.sub(valueString, 1, #valueString - 3 * (valueKilos - 1)) .. commaString

goal.Value = commaString
tweenService:Create(tweenNumber, tweenInfo, {Value = commaString}):Play()

should work

and where would this go because it’s not working for some reason

Should be able to place it inside the PlrMoneyChanged function

function plrMoneyChanged(value)
	local commaString = ""
	local valueString = tostring(value)
	local valueKilos = 1
	while 10 ^ (valueKilos * 3) < value do
		local seperatedKilo = string.sub(valueString, #valueString - 3 * valueKilos + 1, #valueString - 3 * (valueKilos - 1))
		commaString = "," .. seperatedKilo .. commaString
		valueKilos += 1
	end
	commaString = string.sub(valueString, 1, #valueString - 3 * (valueKilos - 1)) .. commaString

	goal.Value = commaString
	tweenService:Create(tweenNumber, tweenInfo, {Value = commaString}):Play()
	
	tweenNumber.Changed:Connect(function(tweenValue)
		local newTweenValue = RoundNumber(tweenValue)
		label.Text = tostring(newTweenValue)
	end)
	
end

though I’m not sure if it is possible to tween a string value

ah this is the wrong spot. it should be placed at the label

tweenNumber.Changed:Connect(function(tweenValue)
	local commaString = ""
	local valueString = tostring(RoundNumber(tweenValue))
	local valueKilos = 1
	while 10 ^ (valueKilos * 3) < value do
		local seperatedKilo = string.sub(valueString, #valueString - 3 * valueKilos + 1, #valueString - 3 * (valueKilos - 1))
		commaString = "," .. seperatedKilo .. commaString
		valueKilos += 1
	end

	label.Text = string.sub(valueString, 1, #valueString - 3 * (valueKilos - 1)) .. commaString
end)

not sure if I did it right but If I did it says “attempt to connect failed: passed value is not a function”

tell me if this errors

local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(.3)

local label = script.Parent:WaitForChild("BalanceNum")

local plrs = game:GetService("Players")
local plr = plrs.LocalPlayer

local plrStats = plr:WaitForChild("leaderstats")
local plrMoney = plrStats:WaitForChild("Crash")

local tweenNumber = script.Parent:WaitForChild("TweenNumber")
local goal = {}

local function RoundNumber(number)
	return(math.floor(number+.5))
end

wait(.5)

local function AddCommas(value)
	local commaString = ""
	local valueString = tostring(RoundNumber(value))
	local valueKilos = 1
	while 10 ^ (valueKilos * 3) < value do
		local seperatedKilo = string.sub(valueString, #valueString - 3 * valueKilos + 1, #valueString - 3 * (valueKilos - 1))
		commaString = "," .. seperatedKilo .. commaString
		valueKilos += 1
	end

	label.Text = string.sub(valueString, 1, #valueString - 3 * (valueKilos - 1)) .. commaString
end

AddCommas(plrMoney.Value)
function plrMoneyChanged(value)
	goal.Value = value
	tweenService:Create(tweenNumber, tweenInfo, {Value = goal.Value}):Play()
	
	tweenNumber.Changed:Connect(function(tweenValue)
		AddCommas(tweenValue)
	end)	
end

plrMoney.Changed:Connect(plrMoneyChanged)
1 Like


image
line 16

edited my above post so that it should be fixed

1 Like

it works thank you so much man.

1 Like