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

Confusing text box error attempt to conpare number with < nil?

Asked by 1 year ago

So this is my script:

local player = game.Players.LocalPlayer
local tokens = player.leaderstats.Tokens

script.Parent.MouseButton1Click:Connect(function()
    if tonumber(script.Parent.Text) > game.ReplicatedStorage.CurrentBid.Value or                game.ReplicatedStorage.CurrentBid.Value == nil then
        if tonumber(script.Parent.Text) <= tokens.Value then
            game.ReplicatedStorage.NewBid:FireServer(tonumber(script.Parent.Text))
        end
    end
end)

For some reason i get the error "attempt to compare number with < nil" which is really odd. I have done some investigating and it seems as if when you enter text into the text box and press enter, it doesnt register in the "Text" property of the textbox.

Help appreciated.

0
is this error on line 5 or 6 JustinWe12 723 — 1y
0
the error no longer happens, but it just doesnt work NotOreo9 8 — 1y

2 answers

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

Guessing that you're talking about tonumber(script.Parent.Text) <= tokens.Value as you've mentioned < in the error. This happens when script.Parent.Text is not a number, in which case tonumber(script.Parent.Text) will return nil.

To fix this, I recommend simply adding a check if it is a number or not and cancelling the function if it isn't:

local player = game.Players.LocalPlayer
local tokens = player.leaderstats.Tokens

script.Parent.MouseButton1Click:Connect(function()
    if tonumber(script.Parent.Text) == nil then return end -- Usually I'd recommend doing `not tonumber(script.Parent.Text)` instead of `tonumber(script.Parent.Text) == nil`, but for demonstration purposes I used the earlier instead.

    if tonumber(script.Parent.Text) > game.ReplicatedStorage.CurrentBid.Value or game.ReplicatedStorage.CurrentBid.Value == nil then
        if tonumber(script.Parent.Text) <= tokens.Value then
            game.ReplicatedStorage.NewBid:FireServer(tonumber(script.Parent.Text))
        end
    end
end)
0
This is incorrect. From the error message, it says it is comparing a number with a nil value; meaning that tonumber IS returning a number and tokens.Value is nil. If tonumber was returning nil, the error message would be the other way around. xInfinityBear 1777 — 1y
0
It doesnt work, i would assume it is still thinking the text is equal to nil, which is the problem and im not sure why. nothing appears in the output and it doesnt work so it must be thinking the text is nil, which it shouldnt be. NotOreo9 8 — 1y
0
It is not incorrect, it was my best guess. Also, I simply misread the original post. DindinYT37 246 — 1y
0
All good. I was not saying that the code in general was wrong; it just was not correct in regards to the problem. xInfinityBear 1777 — 1y
Ad
Log in to vote
0
Answered by 1 year ago

Your script may load and run even before the GUIs, Frames, Textboxes, etc. So I recommend using

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
repeat wait() until player.Character        --From my experience this usually buffers the scripts just enough for the datastore to load it.
local tokens = player:WaitForChild('leaderstats'):WaitForChild('Tokens')

Another method you may do to prevent fast loads, (though may cause a delay per click by I guess around 0.00001s depending on your processor)

Is simply to put the 'local tokens = player.leaderstats.Tokens' inside the function.

0
this doesnt work - the error is no longer there but just nothing happens which is quite confusing NotOreo9 8 — 1y

Answer this question