Lua tonumber returs nil和我可以';我找不到解决办法

Lua tonumber returs nil和我可以';我找不到解决办法,lua,scripting,lua-table,roblox,Lua,Scripting,Lua Table,Roblox,所以我用Roblox做了一个高级计算器,我被equal按钮卡住了,我已经尝试了一切,甚至可以重新启动整个代码,但是当我按下equal按钮(假设我输入了1+1)或者它返回nil,1+1或者只是一个错误时,我尝试了一切,但它从来没有返回我想要的2。 代码如下: -- Geral Variables local Screen = game.workspace.Cauculator.Screen local Potency2 = game.workspace.Cauculator.Potency2 lo

所以我用Roblox做了一个高级计算器,我被equal按钮卡住了,我已经尝试了一切,甚至可以重新启动整个代码,但是当我按下equal按钮(假设我输入了1+1)或者它返回nil,1+1或者只是一个错误时,我尝试了一切,但它从来没有返回我想要的2。 代码如下:

-- Geral Variables
local Screen = game.workspace.Cauculator.Screen
local Potency2 = game.workspace.Cauculator.Potency2
local Potency3 = game.workspace.Cauculator.Potency3
local Potency4 = game.workspace.Cauculator.Potency4
local Minus = game.workspace.Cauculator.Minus
local Plus = game.workspace.Cauculator.Plus
local Percent = game.Workspace.Cauculator.Percent
local Times = game.Workspace.Cauculator.Mutiply
local SquareRoot = game.Workspace.Cauculator.SquareRoot
local Divide = game.Workspace.Cauculator.Divide
local Equal = game.Workspace.Cauculator.Equal
local Number = {}
local Result = nil
-- functions
function Press1 ()
    table.insert(Number, "1")
    game:GetService("LogService").MessageOut:Connect(function (M, T)
        
        Screen.SurfaceGui.TextLabel.Text = string.gsub(tostring(M), '%s+', '')
        
    end)
    print(unpack(Number))
end
game.Workspace.Cauculator["1"].ClickDetector.MouseClick:Connect(Press1)

function Press2 ()
    table.insert(Number, "2")
    game:GetService("LogService").MessageOut:Connect(function (M, T)

        Screen.SurfaceGui.TextLabel.Text = string.gsub(tostring(M), '%s+', '')

    end)
    print(unpack(Number))
end
game.Workspace.Cauculator["2"].ClickDetector.MouseClick:Connect(Press2)

function Press3 ()
    table.insert(Number, "3")
    game:GetService("LogService").MessageOut:Connect(function (M, T)

        Screen.SurfaceGui.TextLabel.Text = string.gsub(tostring(M), '%s+', '')

    end)
    print(unpack(Number))
end
game.Workspace.Cauculator["3"].ClickDetector.MouseClick:Connect(Press3)

function Press4 ()
    table.insert(Number, "4")
    game:GetService("LogService").MessageOut:Connect(function (M, T)

        Screen.SurfaceGui.TextLabel.Text = string.gsub(tostring(M), '%s+', '')

    end)
    print(unpack(Number))
end
game.Workspace.Cauculator["4"].ClickDetector.MouseClick:Connect(Press4)

function Press5 ()
    table.insert(Number, "5")
    game:GetService("LogService").MessageOut:Connect(function (M, T)

        Screen.SurfaceGui.TextLabel.Text = string.gsub(tostring(M), '%s+', '')

    end)
    print(unpack(Number))
end
game.Workspace.Cauculator["5"].ClickDetector.MouseClick:Connect(Press5)

function Press6 ()
    table.insert(Number, "6")
    game:GetService("LogService").MessageOut:Connect(function (M, T)

        Screen.SurfaceGui.TextLabel.Text = string.gsub(tostring(M), '%s+', '')

    end)
    print(unpack(Number))
end
game.Workspace.Cauculator["6"].ClickDetector.MouseClick:Connect(Press6)

function Press7 ()
    table.insert(Number, "7")
    game:GetService("LogService").MessageOut:Connect(function (M, T)

        Screen.SurfaceGui.TextLabel.Text = string.gsub(tostring(M), '%s+', '')

    end)
    print(unpack(Number))
end
game.Workspace.Cauculator["7"].ClickDetector.MouseClick:Connect(Press7)

function Press8 ()
    table.insert(Number, "8")
    game:GetService("LogService").MessageOut:Connect(function (M, T)

        Screen.SurfaceGui.TextLabel.Text = string.gsub(tostring(M), '%s+', '')

    end)
    print(unpack(Number))
end
game.Workspace.Cauculator["8"].ClickDetector.MouseClick:Connect(Press8)

function Press9 ()
    table.insert(Number, "9")
    game:GetService("LogService").MessageOut:Connect(function (M, T)

        Screen.SurfaceGui.TextLabel.Text = string.gsub(tostring(M), '%s+', '')

    end)
    print(unpack(Number))
end
game.Workspace.Cauculator["9"].ClickDetector.MouseClick:Connect(Press9)

function Press0 ()
    table.insert(Number, "0")
    game:GetService("LogService").MessageOut:Connect(function (M, T)

        Screen.SurfaceGui.TextLabel.Text = string.gsub(tostring(M), '%s+', '')

    end)
    print(unpack(Number))
end
game.Workspace.Cauculator["0"].ClickDetector.MouseClick:Connect(Press0)

function PressPlus ()
    table.insert(Number, "+")
    game:GetService("LogService").MessageOut:Connect(function (M, T)

        Screen.SurfaceGui.TextLabel.Text = string.gsub(tostring(M), '%s+', '')

    end)
    print(unpack(Number))
end
Plus.ClickDetector.MouseClick:Connect(PressPlus)

function PressMinus ()
    table.insert(Number, "-")
    game:GetService("LogService").MessageOut:Connect(function (M, T)

        Screen.SurfaceGui.TextLabel.Text = string.gsub(tostring(M), '%s+', '')

    end)
    print(unpack(Number))
end
Minus.ClickDetector.MouseClick:Connect(PressMinus)

function PressEqual ()
    
    game:GetService("LogService").MessageOut:Connect(function (M, T)

        Result = tonumber(string.gsub(tostring(M), '%s+', ''))
        print(Result)

    end)
    print(unpack(Number))
    
end
Equal.ClickDetector.MouseClick:Connect(PressEqual)```

tonumber
不计算表达式,其目的是从字符串中解析数字


要制作一个工作正常的计算器,您需要制作一个解析器,用于检测数字和数学运算符,然后执行计算逻辑。

@CarlosEduardoVeras如果我回答了您的问题,请将问题标记为“已回答”,这是您在StackOverflow上的操作方式。