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

How can I calculate a stringed math problem?

calculation = "1+1"

print(calculation)

--output: 1+1

How do I unstringify this in Roblox Lua? I don't want to use loadstring due to security issues nor does math.eval work...

What are my options to make this work?

I've tried to format it like this but it doesn't work, it outputs nil:

--I used string.byte to get the values of 1 and + and then I did this:

print(tonumber(string.char(49)) .. string.char(43)) .. tonumber(string.char(49)))

--I want to calculate it in a print but it doesn't work.

The characters + and -, are they strings or numbers???

1 comment

0

Use :sub()

2 answers

✓ Accepted answer

I have made a new script to calculate it.

local calculate = "3+31"
print(calculate)
print()

local num1 = nil
local numA = nil
local symbol = nil
local num2 = nil
for i = 1,100,1 do
	local sub = calculate:sub(1,i)
	if tonumber(sub) ~= nil then
		num1 = sub
		numA = i
	end
end
symbol = calculate:sub(numA+1,numA+1)
for i = numA+2,100,1 do
	local sub = calculate:sub(numA+2,i)
	if tonumber(sub) ~= nil then
		num2 = sub
	end
end
if symbol == "+" then
	print(num1 + num2)
elseif symbol == "-" then
	print(num1 - num2)
elseif symbol == "*" then
	print(num1 * num2)
elseif symbol == "/" then
	print(num1 / num2)
elseif symbol == "^" then
	print(num1 ^ num2)
elseif symbol == "%" then
	print(num1 % num2)
end

1 comment

0

Thanks for this, I actually had something similar done but my version didn't work.

idk what unstring mean so i just be dumb and put this

print(1 + 1)

 -- output: 2

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