Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can you change TextBox text into Variables if the text is a number?

Asked by
ax_447 2
6 years ago

I'm trying to do a sort of calculator thing, where it multiplies whatever numbers you put into two Text Boxes when you press a Textbutton. Everytime I press the button, this error shows on the output:

Players.ax_447.PlayerGui.ScreenGui.Script:6: attempt to perform arithmetic on global 'firstvariable' (a string value)

I know I can make it work by making a variable that changes when the string value changes, but for that I have to type in every number individually. Is there any way to do this? If not, is there another way to make the type of thing I'm trying to make?

1 answer

Log in to vote
0
Answered by
arshad145 392 Moderation Voter
6 years ago

Hello,

This is the current order of my Gui.

local textbox = script.Parent.Parent.TextBox
local pattern = '%d+'

function apply(a)
    textbox.Text = tostring(a)
end



script.Parent.MouseButton1Click:Connect(function()
    local tex = (textbox.Text:match(pattern))
    if tex == nil then return end
    print(tex)
    local fv = tonumber(tex)
    local sum = fv + 999 
    local mul = fv * 99
     apply(sum * mul)
end)

As you can see you can convert a string into a number by simply doing tonumber(String) or the other way tostring(number).

But you will notice that if you type letters together with number you will get an error, I will be using string patterns to bypass those.

Thank you for reading! Goodluck!

0
Thanks! It pretty much worked after I changed a couple of things. I changed the names of my Gui to match the script, and I had to change the local sum to 0 and the local mul to 1. Then it worked perfectly! ax_447 2 — 6y
Ad

Answer this question