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 5 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:

01local setNumberRangeFunction = game:GetService("ReplicatedStorage"):WaitForChild("SetNumberRangeFunction")
02local button = script.Parent
03 
04button.MouseButton1Click:Connect(function()
05 
06    local buttonDefaultText = "Set Number Range"
07 
08    local function makeErrorMessage(message)
09        button.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
10        button.Text = message
11        wait(2)
12        button.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
13        button.Text = buttonDefaultText    
14    end
15 
View all 49 lines...

This is the ServerScript:

01local numberRangeFunction = game:GetService("ReplicatedStorage"):WaitForChild("SetNumberRangeFunction")
02 
03numberRangeFunction.OnServerInvoke = function(player, maxNum, minNum)
04 
05    if maxNum == "" or minNum == "" then
06        return "Value = nil"
07    end
08 
09    if minNum > maxNum then
10        return "Min > Max"
11    end
12 
13    if minNum == maxNum then
14        return "Same value"
15    end
View all 31 lines...
1
Well tell us where the error is instead of telling us the problem then giving us a book thatwalmartman 404 — 5y

1 answer

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

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

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

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

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

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

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

1if minNum > maxNum then
2    return "Min > Max"
3end

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

1if minNum < maxNum then
2    local maxNumValue = script:WaitForChild("MaxNumber")
3    local minNumValue = script:WaitForChild("MinNumber")
4 
5    maxNumValue.Value = tonumber(maxNum)
6    minNumValue.Value = tonumber(minNum)
7    return "Success"
8end

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:

01numberRangeFunction.OnServerInvoke = function(player, maxNum, minNum)
02 
03    maxNum = tonumber(maxNum)
04    minNum = tonumber(minNum)
05 
06    if maxNum == nil or minNum == nil then
07        return "Value = nil"
08    end
09 
10    if minNum > maxNum then
11        return "Min > Max"
12    end
13 
14    if minNum == maxNum then
15        return "Same value"
View all 32 lines...
0
Thank you. youtubemasterWOW 2741 — 5y
Ad

Answer this question