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: Using "or" while stating paths & values

Previous Thread :: Next Thread 
edenojack is online. edenojack
Joined: 18 Jul 2008
Total Posts: 989
03 Jan 2016 02:37 PM
I've encountered this in a lot of scripts, but still do not understand its use.
Lines such as


function SubtleDifference(PB)
local Jelly-Fish = "Jellyfish" or PB
...
...
end

SubtleDifference("Jelly Fish")

What are its uses? And what happens when you add more "or" vairables ?
Report Abuse
Darkmist101 is not online. Darkmist101
Joined: 30 Jul 2010
Total Posts: 6208
03 Jan 2016 02:42 PM
I know it's pseudo code; but that wouldn't work as intended.

local function Test(Var)
Var = Var or "No variable supplied"
print(Var)
end

Test("Hello world!")
Test()

> "Hello world!"
> "No variable supplied"

It allows for optional arguments in this case.

We can also do something like this:

local function Test(Var)
print(Var or "No variable supplied")
end

Keep in mind though, if Var could be false; this method should not be used.

Report Abuse
CloneTrooper1019 is not online. CloneTrooper1019
Joined: 19 Jan 2009
Total Posts: 6227
03 Jan 2016 02:42 PM
local a = false
local b = "Test"

print(a or b)

In this case, it will print "Test" because in the case that a condition before "or" is false or nil, the condition after "or" will be used instead.
Report Abuse
chimmihc is not online. chimmihc
Joined: 01 Sep 2014
Total Posts: 17143
03 Jan 2016 02:42 PM
Well that code will error because you have - between Jelll and Fish. But in that case it doesn't do anything, the variable will always equal "Jellyfish".


Report Abuse
Darkmist101 is not online. Darkmist101
Joined: 30 Jul 2010
Total Posts: 6208
03 Jan 2016 02:43 PM
http://wiki.roblox.com/index.php?title=Boolean#Conditional_expression
Report Abuse
FlyNormal is not online. FlyNormal
Joined: 30 Sep 2015
Total Posts: 344
03 Jan 2016 04:24 PM
http://wiki.roblox.com/index.php?title=Operator#Logical
Report Abuse
ForeverDev is not online. ForeverDev
Joined: 04 Oct 2008
Total Posts: 13300
03 Jan 2016 04:30 PM
'or' and 'and' can be used as ternary operators in Lua

for example:

local bool = true
local name = (bool and "Fred" or "Jake")

print(name) --> Fred

bool = false
name = (bool and "Fred" or "Jake")

print(name) --> Jake

https://en.wikipedia.org/wiki/%3F:
Report Abuse
edenojack is online. edenojack
Joined: 18 Jul 2008
Total Posts: 989
03 Jan 2016 05:17 PM
Cheers guys :)
Report Abuse
LegendaryAccount is not online. LegendaryAccount
Joined: 02 Jun 2010
Total Posts: 13193
03 Jan 2016 05:22 PM
if you used the ? operator in c it would do the same.
its a compacted ifstatement for setting a value basically


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