Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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.

2 comments

0

is this error on line 5 or 6

0

the error no longer happens, but it just doesnt work

2 answers

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)

4 comments

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.

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.

0

It is not incorrect, it was my best guess. Also, I simply misread the original post.

0

All good. I was not saying that the code in general was wrong; it just was not correct in regards to the problem.

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.

1 comment

0

this doesnt work - the error is no longer there but just nothing happens which is quite confusing

This discussion is preserved as it was. New answers, comments, and votes are closed.