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
I’m aware what he is trying to, but what you suggested wouldn’t help. Here’s why
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.