generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Difference between ~= and not

Previous Thread :: Next Thread 
imaginaryUberlyuber is not online. imaginaryUberlyuber
Joined: 15 Jul 2012
Total Posts: 208
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
Lua_Basics is not online. Lua_Basics
Joined: 04 Jun 2012
Total Posts: 9977
02 Feb 2017 08:26 PM
~= is an exact thing and not just checks for false and nil.
Report Abuse
imaginaryUberlyuber is not online. imaginaryUberlyuber
Joined: 15 Jul 2012
Total Posts: 208
02 Feb 2017 08:29 PM
Please read my post before commenting
Report Abuse
Lua_Basics is not online. Lua_Basics
Joined: 04 Jun 2012
Total Posts: 9977
02 Feb 2017 08:31 PM
in not val then
Report Abuse
BlueWizard12345 is not online. BlueWizard12345
Joined: 31 Mar 2012
Total Posts: 906
02 Feb 2017 08:32 PM
not (val == "")
Report Abuse
BlueWizard12345 is not online. BlueWizard12345
Joined: 31 Mar 2012
Total Posts: 906
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
Lua_Basics is not online. Lua_Basics
Joined: 04 Jun 2012
Total Posts: 9977
02 Feb 2017 08:34 PM
OH, didn't even notice he did that ^ good catch.
Report Abuse
imaginaryUberlyuber is not online. imaginaryUberlyuber
Joined: 15 Jul 2012
Total Posts: 208
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
imaginaryUberlyuber is not online. imaginaryUberlyuber
Joined: 15 Jul 2012
Total Posts: 208
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 is not online. 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 is not online. 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
imaginaryUberlyuber is not online. imaginaryUberlyuber
Joined: 15 Jul 2012
Total Posts: 208
02 Feb 2017 09:04 PM
Again, I repeat: Read my post before commenting.
Report Abuse
Lua_Basics is not online. Lua_Basics
Joined: 04 Jun 2012
Total Posts: 9977
02 Feb 2017 09:06 PM
val ~= ""

^ my friend that's not correct.
Report Abuse
cody123454321 is not online. cody123454321
Joined: 21 Nov 2009
Total Posts: 5408
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
Internecivus is online. Internecivus
Joined: 31 May 2011
Total Posts: 2784
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
BlueWizard12345 is not online. BlueWizard12345
Joined: 31 Mar 2012
Total Posts: 906
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 is not online. 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
BlueWizard12345 is not online. BlueWizard12345
Joined: 31 Mar 2012
Total Posts: 906
03 Feb 2017 03:58 PM
You're right.. interesting..
Report Abuse
BlueWizard12345 is not online. BlueWizard12345
Joined: 31 Mar 2012
Total Posts: 906
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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image