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

How do you run a mathematical equation when the operator is a variable?

So I am making a calculator and I have set it up so that the operator is one of four different operations. But i can not seem to figure out how to execute the equation while using a variable.

function execute_eqation()
	one = string1.Text
	two = string2.Text
	output.Text = one..op..two.." = "..one(tostring(op))two
	end
equals.MouseButton1Down:connect(execute_eqation)

1 answer

✓ Accepted answer

this is how I did it for my homemade homework program (I was bored, and it gave me homework, not did it for me.)


function execute_eqation()

one = string1.Text
two = string2.Text

if opchoice == 1 then
	output.Text = one .. "+" .. two .. "=" .. one + two
elseif opchoice == 2 then
	output.Text = one .. "-" .. two .. "=" .. one - two
elseif opchoice == 3 then
	output.Text = one .. "x" .. two .. "=" .. one * two
elseif opchoice == 4 then
	output.Text = one .. "/" .. two .. "=" .. one / two
end

end

equals.MouseButton1Down:connect(execute_eqation)

I know it's not what You wanted, but I honestly have no idea how to do it otherwise. just have it so it changes the opchoice value depending on which operator you click.

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