How would I Tween a Number for Text?

Hello,

So I’m trying to create a Level System, and now I’m turning it into UI, So when doing the Status for EXP compared to Required EXP, But I’m not sure how to do this, I have used a for loop, but this makes the code ugly and doesn’t work when its constantly changing.

Current Code:

EXP.Changed:Connect(function()
	local Item = LFrame.Frame.S
	local Status = LFrame.Frame.Status
	
	local NewSize = UDim2.fromScale(EXP.Value/REXP.Value, 1)
	local NewText = tostring(math.floor(EXP.Value))
	
	TweenService:Create(Item, TweenInfo.new(.5), {Size = NewSize}):Play()
	task.spawn(function()
    --[[ Stupid
		for i = 1,EXP.Value do task.wait()
			Status.Text = i	
		end
    ]]
	end)

end)

What should I do?

1 Like

What exactly do you mean? I don’t understand what you want :pensive:

Tweening Numbers onto Text, Not sure how to Describe it another way.

1 Like

So you want to glide one number to another (like 1 to 5, 1 2 3 4 5)?

1 Like

Yes, If i understand what you are saying Correctly.

1 Like

For 1 to 5 you can divide 5 by 5 and you can add that result (1) to 1 4 times to glide to 5.

You could do this:

EXP.Changed:Connect(function()
		local Item = LFrame.Frame.S
		local Status = LFrame.Frame.Status

		local NewSize = UDim2.fromScale(EXP.Value/REXP.Value, 1)
		local NewText = tostring(math.floor(EXP.Value))

		local Tween = TweenService:Create(Item, TweenInfo.new(.5), {Size = NewSize})
		Tween:Play()
		Tween.Changed:Connect(function()
			Status.Text = EXP.Value
		end)
	end)

It would change the text value while the tween is running. You could also add a Status.Text = NewText at the end if the value does not finish with the correct number.

1 Like

This Doesn’t give me the Intended Effect I would want

I haven’t set up the Concatenation part, I’ll do that later.

1 Like

You can still try dividing the value your changing it by into smaller numbers and adding them (Like I’ve said), or you can multiply decimals if you feel fancy

Edit: ugh grammer

I got the Effect i wanted, I used IntValues, but is there a better way of doing this?

EXP.Changed:Connect(function()
	local Item = LFrame.Frame.S
	local Status = LFrame.Frame.Status
	
	local NewSize = UDim2.fromScale(EXP.Value/REXP.Value, 1)
	Status.New.Value = math.floor(EXP.Value)
	
	
	local Tween = TweenService:Create(Item, TweenInfo.new(.5), {Size = NewSize})
	Tween:Play()
	
	TweenService:Create(Status.Current, TweenInfo.new(.5), {Value = Status.New.Value}):Play()
	Status.Current:GetPropertyChangedSignal("Value"):Connect(function()
		Status.Text = Status.Current.Value
	end)
end)

1 Like

I bet there is, but this is how I learned how to do it from using scratch

Oh for that you just need to change it from this:

Status.Text = EXP.Value

To this:

Status.Text = tostring(EXP.Value) .. " / " .. tostring(REXP.Value)
1 Like

Yeah, I said i would do that later, Thanks tho

1 Like

You Could use multiplication of decimals for a smoothed effect but that’s just a little bit harder to do unless your ending number is 0
(ex: Value = Value*0.98)

Edit: If the number is close to 1, like really close, itay cause the number to take a while to reach 0

Edit2: You don’t need to do this if you just want a tween effect

1 Like

Yes, but wouldn’t that be a bit unnecessary?

1 Like

It can be, the only thing it changes is that it is a smoothed effect, but if all you want is the tween then don’t bother

The code I made replicates the current transition from the tween so it could also be made with a second one if you need but I think that would just be a waste of time as it should follow the bar’s animation transition.

1 Like

This is just concatenating, Do it does not

1 Like

A weird way to do it is to tween a part position and use it’s x position (Don’t do it this way, it’s a horrible way to do it)

Well, I made a Slight Update, I removed the Use of one IntValue:

EXP.Changed:Connect(function()
	local Item = LFrame.Frame.S
	local Status = LFrame.Frame.Status
	
	local NewSize = UDim2.fromScale(EXP.Value/REXP.Value, 1)
	
	
	TweenService:Create(Item, TweenInfo.new(.5), {Size = NewSize}):Play()
	
	TweenService:Create(Status.Current, TweenInfo.new(.5), {Value = EXP.Value}):Play()
	Status.Current.Changed:Connect(function()
		Status.Text = Status.Current.Value.." / "..REXP.Value
	end)
end)

I’m not sure is I should mark this as the solution since there might be a better way to this.

2 Likes