Heyy, developers! I’m currently trying to add the numbers inside of a text label. for example (random number + or - random number). I’m not sure how to fix this.

if player.PlayerGui.Math.ImageLabel.TextBox.Text == mathSplit[1] mathSplit[2] mathSplit[3] then -- 1 is a number, 2 is a - or +, 3 is a number

So it looks like this? 1 + 2
and you want to split it into: {1, "+", 2}
to then get: 3
and set text to that?

Try this:

local values = string.split("1 + 2", " ")
local operators = {
["+"] = function(a,b)
return a+b
end,
["-"] = function(a,b)
return a-b
end,
["*"] = function(a,b)
return a*b
end,
["/"] = function(a,b)
return a/b
end,
}
if #values == 3 then --there are enough values
if operators[values[2]] then --operator can be solved
if tonumber(values[1]) and tonumber(values[3]) then--LHS and RHS are numerical
print(operators[values[2]](values[1], values[3]))
else
warn("Bad numbers")
end
else
warn("Bad operator")
end
else
warn("Bad expression")
end

You’ll need to adapt it to fetching/setting your textbox’s text instead since I didn’t replicate your UI setup.

local result
if mathSplit[2] == "+" then
result = tonumber(mathSplit[1]) + tonumber(mathSplit[3])
elseif mathSplit[2] == "-" then
result = tonumber(mathSplit[1]) - tonumber(mathSplit[3])
end
if player.PlayerGui.Math.ImageLabel.TextBox.Text == tostring(result) then -- 1 is a number, 2 is a - or +, 3 is a number

Needs to be like this.

This may error if mathSplit[1] or mathSplit[3] aren’t numbers though.

Tjis doesn’t work for some reason. Whenever I print that value, it becomes nil.

local result
local input
player.PlayerGui.Math.ImageLabel.TextBox:GetPropertyChangedSignal("Text"):Connect(function()
input = player.PlayerGui.Math.ImageLabel.TextBox.Text
end)

You can modify the result of the operation using math.abs(n), assuming all the numbers are positive. If they arent, you can use math.max(n1, n2) - math.min(n1, n2)