Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Attempt to Compare Number and String?

Asked by 4 years ago

I am trying to make a gui that picks a random number, but then I ran into an error saying Attempt to compare number and string.

This is the LocalScript:

local setNumberRangeFunction = game:GetService("ReplicatedStorage"):WaitForChild("SetNumberRangeFunction")
local button = script.Parent

button.MouseButton1Click:Connect(function()

    local buttonDefaultText = "Set Number Range"

    local function makeErrorMessage(message)
        button.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
        button.Text = message
        wait(2)
        button.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
        button.Text = buttonDefaultText     
    end

    local maxNumText = script.Parent.Parent:WaitForChild("MaxNumber")
    local minNumText = script.Parent.Parent:WaitForChild("MinNumber")

    if tonumber(maxNumText.Text) or tonumber(minNumText.Text) then
        -- Ignore This
    else
        makeErrorMessage("The number(s) has to be an actual number.")
        return 
    end

    local numberRange = setNumberRangeFunction:InvokeServer(maxNumText.Text, minNumText.Text)

    if numberRange == "Min > Max" then
        makeErrorMessage("The min number is greater than the max number.")
    end

    if numberRange == "Value = nil" then
        makeErrorMessage("One or more numbers are not set.")
    end

    if numberRange == "Same value" then
        makeErrorMessage("The numbers are the same.")
    end

    if numberRange == "Number too big" then
        makeErrorMessage("The max number is too big. The max number allowed is 1000000000.")
    end

    if numberRange == "Success" then
        button.Text = "Success!"
        wait(2)
        button.Text = buttonDefaultText
    end
end)

This is the ServerScript:

local numberRangeFunction = game:GetService("ReplicatedStorage"):WaitForChild("SetNumberRangeFunction")

numberRangeFunction.OnServerInvoke = function(player, maxNum, minNum)

    if maxNum == "" or minNum == "" then
        return "Value = nil"
    end

    if minNum > maxNum then
        return "Min > Max"
    end

    if minNum == maxNum then
        return "Same value"
    end

    if maxNum > 1000000000 then
        return "Number too big"
    end

    if maxNum ~= "" and minNum ~= "" then
        if minNum < maxNum then
            local maxNumValue = script:WaitForChild("MaxNumber")
            local minNumValue = script:WaitForChild("MinNumber")

            maxNumValue.Value = tonumber(maxNum)
            minNumValue.Value = tonumber(minNum)
            return "Success"
        end
    end
end
1
Well tell us where the error is instead of telling us the problem then giving us a book thatwalmartman 404 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
if maxNum > 1000000000 then
    return "Number too big"
end

You're comparing maxNum, a string, to 1000000000, a number.

maxNum is a string because you're firing over strings to the server:

setNumberRangeFunction:InvokeServer(maxNumText.Text, minNumText.Text)

What you should do is convert maxNum to a number whenever you're comparing it to a number:

if tonumber(maxNum) > 1000000000 then
    return "Number too big"
end

There is another issue, where you're trying to compare if minNum is bigger than maxNum even though they are both strings:

if minNum > maxNum then
    return "Min > Max"
end

And another instance where you compare if minNum is lesser than maxNum, even though they are both strings:

if minNum < maxNum then
    local maxNumValue = script:WaitForChild("MaxNumber")
    local minNumValue = script:WaitForChild("MinNumber")

    maxNumValue.Value = tonumber(maxNum)
    minNumValue.Value = tonumber(minNum)
    return "Success"
end

You should convert both variables to a number when comparing them like this.

What I suggest doing is setting minNum and maxNum to numbers at the start of the script. This way you don't have to call tonumber every single time you want to compare them. (tonumber will return nil if the string cannot be converted to a number, e.g. when the string is empty, so you can use that to check if the string is empty.) Here's what your code should look like:

numberRangeFunction.OnServerInvoke = function(player, maxNum, minNum)

    maxNum = tonumber(maxNum)
    minNum = tonumber(minNum)

    if maxNum == nil or minNum == nil then
        return "Value = nil"
    end

    if minNum > maxNum then
        return "Min > Max"
    end

    if minNum == maxNum then
        return "Same value"
    end

    if maxNum > 1000000000 then
        return "Number too big"
    end

    if maxNum ~= nil and minNum ~= nil then
        if minNum < maxNum then
            local maxNumValue = script:WaitForChild("MaxNumber")
            local minNumValue = script:WaitForChild("MinNumber")

            maxNumValue.Value = maxNum
            minNumValue.Value = minNum
            return "Success"
        end
    end
end
0
Thank you. youtubemasterWOW 2741 — 4y
Ad

Answer this question