Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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!

1 comment

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.