How would I Tween a number in a StringValue

I’m having some trouble tweening a number. It works fine when I change it to a IntValue but I want to use a StringValue since it can store numbers above the number limit.

This is the error I receive

TweenService:Create property named 'Value' cannot be tweened due to type mismatch (property is a 'string', but given type is 'double')  -  Client -

Here is a Snippet of the Code where the error is occuring

local leaderstats = players.LocalPlayer:WaitForChild("leaderstats")
local points = leaderstats:WaitForChild("Points")
local label = script.Parent

local TransitionAmount = Instance.new("StringValue") 

TransitionAmount.Parent = points

label.Text = tonumber(points.Value) -- Set to points value when not changed

TransitionAmount.Value = tonumber(points.Value)

points.Changed:Connect(function(amount) -- Smooth number changing tween
	local tween = tweenService:Create(
		TransitionAmount,
		TweenInfo.new(.2, Enum.EasingStyle.Linear, Enum.EasingDirection.In),
		{Value = tonumber(amount)}
	)

	tween:Play()
end)

For int values and number values, there is a defined method of heading towards a number, either adding or subtracting X, whether that be 1, 5, 10 or another number

However, for string, there isn’t really a defined way to do it, hence why it doesn’t work

Ill think of a method for you to use, no promises though.

(there is another person replying as of when I made this post, so they may have a solution)

You can’t tween a string; if you want to tween a number on an object value, you need to use a NumberValue or IntValue. If you really need to use a string value, you could instead use a while loop. e.g.:

local object = script.Parent

local function getNumber()
	local numberString = object.Value:gsub("[^%d%.%-]", "")
	
	return tonumber(numberString) or 0
end

local current = getNumber()

while getNumber() <= 100 do
	current += 1
	object.Value = "$" .. current
	task.wait(0.1)
end

current = getNumber()

while getNumber() >= 0 do
	current -= 1
	object.Value = "$" .. current
	task.wait(0.1)
end

Try changing this to tostring(amount)

It is already a string. tonumber transforms the string into a number.

How come that you want to store numbers above 2^63-1?

I know, thats the problem here. He is trying to set a stringValues value to a number.

If it was a stringvalue, you wouldn’t use tostring anyway in that case :smiley:

He is trying to tween a stringValue by setting its value to a number. The error OP provided shows that.

Hi,

I’m aware what he is trying to, but what you suggested wouldn’t help. Here’s why :smiley:

You suggested that

{Value = tonumber(amount)}

Should be changed into

{Value = tostring(amount)}

amount is already a string, so tostring wouldn’t affect anything, it will only be less effective, than just writing amount directly

{Value = amount}

When that is said. Writing amount directly, is also wrong, since you cannot tween a string(as mentioned before in this thread). Which is also why OP wrote tonumber in the first place.

My suggestion to OP would be, to just use a IntValue. Why?
When you try to add/substract a “StringNumber”(let’s call it that), you will temporarily turn it into a real number, which has the same limitation you tried to go around in the first place.

1 Like

Thats my bad, I totally forgot that :smiley: