Why does a TextBox emit a nil value sometimes?

I am working on a script that creates a part with color, size and orientation that can be modified, but with the position set where the mouse clicks

The text boxes some times emit a nil value, but other times emit an actual value, this gets fixed the more my script is used but, why is that?

This is the screenshot of the error in the "Output" tab

This is a screenshot of the code I use for the values

I have checked the explorer, everything is aligned and spelled properely.

I have read multiple times through my code, added wait()'s here and there, and I simply cannot find the error.

I came back from a long break and my skills could be a little different from before, but I could always get a hand.

What line is the 133 in the code you provided?

Did you try to check if it is not nil?

If the text is not a number and you use tonumber, then it will be nil.

Line 133 is the one where the print() command that I used to detect this error

All I could do for checking nil is to set it to a default number, which I already did it on the server side script for placing a part on the shape and material

I always typed in numbers from the number pad, but it still did not detect the number correctly but it fixes itself the more the script is used.

The server(Normal scripts) can’t see the textBox.Text. Only localscript. If you want to a script have the textBox.Text, you need to recive it from a remoteFunction.

The script you see is a LocalScript, it sends the information of the GUI to a RemoteEvent to a ServerScript which detects who is running it, and if the person is not a moderator, they get permanently banned.

Idk but I think its cause if u stop typing the same thing happened to me and I fixed it
Topic How to turn a string into a value?

I used tonumber but the issue keeps happening

I don’t think the code you’ve shared can create the error. Can you post the exact line with the error, as well as a couple lines above and below it?

No I meant something else

I meant that you have to check if the textbox.value is nil but if it isn’t then keep going

Example:

local textbox = script.Parent
local text = ("lol")

if textbox.Text ~= nil then
  if textbox.Text == text then
       print("lol")
  end
end

Thanks for telling me about that @JarodOfOrbiter i edited it

1 Like

This is a bad practice. Avoid not when you’re using eqality operators. You should just use the opposite quality operator.

a == b
a ~= b -- not (a == b)

a >= b
a < b -- not (a >= b)

It’s cleaner to read and it has one less opcode so it is technically more efficient, but the big reason is because when you write this

not a == b

the interpreter evaluates it as

(not a) == b

This is due to the Operator Precedence. It’s the order of operations, like PEMDAS, for programming languages. In Lua, unary operators such as not come right after exponents:

parentheses
exponents ( ^ )
unary operators ( not x, -x )
multiplication and division
addition and subtraction
concatenation ( .. )
relational / equality ( ==, ~=, >, <=, etc )
logical and ( i.e. if x and y then )
logical or

And so your example would end up doing this.

if (not textbox.Text) == nil then

(not textbox.Text) is always going to be either true or false, never nil, so this condition will never run.

Just so you know.

1 Like