How do I perform Arithmetic on a String Value

How would I make the number ‘amount’ increase inside the string value everytime.
So I want the number of the textlabel to increase by 1 every .1 seconds (The textlabels text will default at the current currency for example if the player has 10 currency it’ll be 10.) I am pretty damn stuck on this.

I apologize if this doesn’t make any sense though I tried my best to word this but for some reason I couldn’t find the right way to word it. Will elaborate upon request.

local Remote = game:GetService("ReplicatedStorage").RemoteEvents:WaitForChild("Currency")


local Amount = player.PlayerGui.Currency_Control.LocalScript.PreviousCurrency.Value

local CashText = player.PlayerGui.ScreenGui.TextLabel


    CashText.Text = ""..Amount..""


    CashText.Text == ""..player.Cash.Value..""


Also ignore the pointless method of storing the previous currency inside a new intvalue, I’m changing that soon but I need to fix this part firstly.

You’re asking the wrong question here. Don’t try that.

Instead of trying to perform an arithmetic function on a StringValue, perform it on a ValueObject that actually supports numbers (IntValue, for example). You can also use tonumber on the value of the StringValue object if it is strictly a number, but I don’t find that to be good practice. If there are any non-numerical characters in your string, it’ll return nil.

There’s also the option of extracting the numbers from a string. There are two ways you can do this: you can either match the first set of numbers in a string or anchor the search pattern to the end.

local String = "Cash: $500"
local Numbers = tonumber(String:match("%d+$"))

For obvious reasons, again, I wouldn’t really recommend doing this. This still doesn’t solve the initial issue of you attempting to perform arithmetic functions on a string which is the root of another issue, which is using that StringValue as a literal data container at all.

Store your literal cash value (strictly numbers only) somewhere else, such as an IntValue or a variable in your script. Use your TextLabel or StringValue only as aids for your displays (Guis), don’t try to perform actual math operations on them.

1 Like

It just returns nil, it doesn’t error. And your pattern will match numbers anywhere in the string, as long as they’re all next to each other–not just at the end. If you want to only match at the end, you can do string:match("%d+$").

1 Like

Ooh, right. I forgot about the tonumber return for strings containing characters being nil.

As for the string pattern match, I defined it wrong. It’ll return the first match of numbers in a string until the end of it. You’re right about that one.

local A = "000 AAA 111"
print(A:match("%d+")) --> 000

I will edit my post to reflect this information. Thanks for informing me of these behaviours.


Thank you for the help, it helped me a ton with the design for currency being added.