|
| 02 Feb 2017 08:26 PM |
So when using the changed event, I seem to have found a difference between ~= and not, which, used in the context I'm using it in, should theoretically have no difference.
--Changing the value of value, a **StringValue**, to "test" using the following script:
script.Parent.value.Changed:connect(function(val) [TAB]if not val == "" then --Note my use of not [TAB][TAB]print("ok") --Used as a reference [TAB]end end
> (nothing is printed)
--Changing the value to "" also results in the same
--Whereas, changing the value to "" using the following script...
script.Parent.value.Changed:connect(function(val) [TAB]if val ~= "" then --Note my use of ~= [TAB][TAB]print("ok") [TAB]end end
> ok
--While it should not be doing so. Changing the value to test also results in it doing the same, as previous.
So what's going on here? |
|
|
| Report Abuse |
|
|
|
| 02 Feb 2017 08:26 PM |
| ~= is an exact thing and not just checks for false and nil. |
|
|
| Report Abuse |
|
|
|
| 02 Feb 2017 08:29 PM |
| Please read my post before commenting |
|
|
| Report Abuse |
|
|
| |
|
| |
|
|
| 02 Feb 2017 08:34 PM |
nvm, you're just using the function parameters wrong
http://wiki.roblox.com/index.php?title=API:Class/Instance/Changed |
|
|
| Report Abuse |
|
|
|
| 02 Feb 2017 08:34 PM |
| OH, didn't even notice he did that ^ good catch. |
|
|
| Report Abuse |
|
|
|
| 02 Feb 2017 08:38 PM |
Lemme make it easier to understand
hierarchy:
ReplicatedStorage -folder --value --LocalScript
LocalScript:
script.Parent.value.Value = "awegiunaweigfu" --Not using this, just here for example
script.Parent.value.Changed:connect(function(val) [TAB]if not value ~= "" then [TAB][TAB]print("ok") ...
>(nothing)
whereas using "if value then" (not a string) will *always* result in it printing ok, even if the value is nil. |
|
|
| Report Abuse |
|
|
|
| 02 Feb 2017 08:39 PM |
| I was typing the above as BlueWizard was typing, I don't exactly see what I did wrong. |
|
|
| Report Abuse |
|
|
chimmihc
|
  |
| Joined: 01 Sep 2014 |
| Total Posts: 17143 |
|
|
| 02 Feb 2017 08:55 PM |
~= : takes two parameters and returns true if they are not equal, false if otherwise not : takes one parameter and returns true if it is false or nil, true if otherwise |
|
|
| Report Abuse |
|
|
chimmihc
|
  |
| Joined: 01 Sep 2014 |
| Total Posts: 17143 |
|
|
| 02 Feb 2017 08:56 PM |
| not : takes one parameter and returns true if it is false or nil, false if otherwise* |
|
|
| Report Abuse |
|
|
|
| 02 Feb 2017 09:04 PM |
| Again, I repeat: Read my post before commenting. |
|
|
| Report Abuse |
|
|
|
| 02 Feb 2017 09:06 PM |
val ~= ""
^ my friend that's not correct. |
|
|
| Report Abuse |
|
|
|
| 02 Feb 2017 09:51 PM |
not is a prefix to a boolean which inverses it. You can call ~= a shorthand for using not.
if(not (1 + 1 == 3))then
if(1 + 1 ~= 3)then
However, not have some additional funky usage you should use.
-> inversing a boolean local debounce = false
local func = function() if(debounce)then return end debounce = not debounce -- do stuff debounce = not debounce end end
-> psuedo-ternary operators local a = (not (1 + 1 == 2) and 1 or 0)
Which, in programming, translates to... int a = (!(1+1 == 2) ? 1 : 0)
Additional example: local state = "dead"
local max_health = 100 local health = state == "head" and 0 or max_health |
|
|
| Report Abuse |
|
|
|
| 02 Feb 2017 09:55 PM |
This is working as it should. ~= is a binary operator, val ~= "" returns true if val is not "".
When you call not val == "" it evaluates as (not val) == "". First, the interpreter evaluates not val. Presumably val exists, so this evaluates to false. You're basically executing this:
if false == "" then
Change it to:
if not (val == "") then
and you should get a proper result. |
|
|
| Report Abuse |
|
|
|
| 03 Feb 2017 02:37 PM |
^ I don't think it actually does that.. just change "if val" to "if script.Parent.value.Value".
The parameter for the changed event is the property that was changed, not the new value. |
|
|
| Report Abuse |
|
|
OzzyFin
|
  |
| Joined: 07 Jun 2011 |
| Total Posts: 3600 |
|
|
| 03 Feb 2017 02:49 PM |
"The parameter for the changed event is the property that was changed, not the new value"
Not exactly true for value objects. If a name of the value object was changed I believe it does pass the property name to the function, however when the value property is changed the new value is passed to the function instead of the string, "Value". |
|
|
| Report Abuse |
|
|
|
| 03 Feb 2017 03:58 PM |
| You're right.. interesting.. |
|
|
| Report Abuse |
|
|
|
| 03 Feb 2017 04:01 PM |
| So here is the real problem: if not value ## ## then It basically does this: if not ("awegiunaweigfu" ## ### then Since "awegiunaweigfu" is not equal to "", the statement inside the parenthesis is true, so it comes down to this: if not (true) then Since "not" reverses a bool value it will end up as: if false then So it will not run the code after the if statement. |
|
|
| Report Abuse |
|
|