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 » Scripting Helpers
Home Search
 

Re: Ternary operators

Previous Thread :: Next Thread 
blockoo is not online. blockoo
Joined: 08 Nov 2007
Total Posts: 17202
15 Dec 2011 06:25 PM
I want to know how to use ternary operators...how would I condense this?

if (m == 1) then
x = "Pass"
else
x = "Fail"
end
Report Abuse
swimguy777 is not online. swimguy777
Joined: 30 May 2009
Total Posts: 17092
15 Dec 2011 06:27 PM
x = m == 1 and "Pass" or "Fail"

I think.

-[::ƧѡÎḾḠΰῩ::]-[::Maker of stuff and Helper of Scripting::]-
Report Abuse
blockoo is not online. blockoo
Joined: 08 Nov 2007
Total Posts: 17202
15 Dec 2011 06:28 PM
Thanks.
Report Abuse
Penguine236 is not online. Penguine236
Joined: 13 Apr 2011
Total Posts: 1303
15 Dec 2011 06:29 PM
yeah its what swim did except like this:

x=(m==1 and "Pass" or "Fail")
Report Abuse
blockoo is not online. blockoo
Joined: 08 Nov 2007
Total Posts: 17202
15 Dec 2011 06:30 PM
swimguy's works.
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
15 Dec 2011 06:31 PM
The only bad thing is this:


local rightAnswer = answer == 1 and false or true


This will be true every time.
Report Abuse
Penguine236 is not online. Penguine236
Joined: 13 Apr 2011
Total Posts: 1303
15 Dec 2011 06:31 PM
yeah. he did it right. i was just suggesting the use of parantheses so its more organized.
Report Abuse
Merlin11188 is not online. Merlin11188
Joined: 20 Dec 2007
Total Posts: 4158
15 Dec 2011 06:38 PM
Ternary is faster than if ... else. And it's more fun to think about :D

local x = 3
local y = "3"
local z = {3}
local a = (x == tonumber(y) and tonumber(y) == z[1] and x == z[1] and z or x == tonumber(y) and y or x)
print(type(a) .. ": " .. tostring(a)) --> table: table: [hexadecimal]
Report Abuse
SDuke524 is not online. SDuke524
Joined: 29 Jul 2008
Total Posts: 6267
15 Dec 2011 07:56 PM
It isn't actually ternary operators, it's binary operators that are being used to mimic

    x=m==1 ? 1 : 0
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • 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