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

Is the logical operator "not" ever even used?

Asked by 5 years ago

Like I've been wondering this and the only example I could come up with is something simple like this

Boolean = false

if not Boolean then
    print("It's a false boolean")
end

so how advanced can you go with this?

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

I've only used it a few times, and they were kinda niche cases. They are great if you want to check for nil immediately before doing anything else. This can be very useful for catching certain things in an if statement before running an accompanying "else".

I've used it to check if a player had data within a table like this:

local checkPlayer = table[player]
if  not checkPlayer then
    --player doesn't have data, so do something
else
    --player has data, so do something else
end

@EmilyBendsSpace Why is that? It won't break the script or anything. I don't see how this can be a problem if you know exactly when nil is possible. I suppose you're thinking if it's a datastore that checks if the player has data or not? In that case you should obviously do it the normal way so you don't overwrite anything.

I guess I'll elaborate on why I had to use it. I created a server script running behind a GUI that first checked the highest option capable by the player and chose that for the player on the very first startup. After it does that, any subsequent calls to the script needed it to be the option the player selected and not the default option. After the player logs off and then logs back on, it needed to choose the highest default option again.

In order to accomplish this you need several things. First, you must have something declared outside of the remote event on the server script (how would you declare a debounce in the remote and then check it?) .You CANNOT do this with a normal debounce variable as you would need one for every player on that server (each player would end up changing the debounce.) I added a couple of other nice features to the GUI like a cooldown on the options they could press and it became increasingly complex until I was forced to immediately check for nil.

Anyway, I don't see how it's any different from doing this in the way I used it:

local checkPlayer = table[player]
if checkPlayer then
    --In my case this section would have been blank
else
    --this is the player's first time opening the GUI
end
0
Okay thank's for elaborating on the subject menofmen1234 20 — 5y
0
It's very helpful in creating dynamic denounces or conditionals too Ziffixture 6913 — 5y
0
Checking for nil is the one thing you should not do with 'not' EmilyBendsSpace 1025 — 5y
Ad

Answer this question