UFAIL2
|
  |
| Joined: 14 Aug 2010 |
| Total Posts: 6905 |
|
|
| 27 Jan 2012 05:44 PM |
| Will someone explain to me how to use 'return'? |
|
|
| Report Abuse |
|
|
|
| 27 Jan 2012 05:48 PM |
Return can be used to stop a function. function onTouch(hit) print("Function started yay") return print("This should not display becuz of return")
script.Parent.Touched:connect(onTouch)
It can also be used to return bool values from functions, I know less about this though, but I believe it goes like this.
function check() player = script.Parent if player:findFirstChild("Humanoid") then return true else print("No humanoid :(") end end
function onTouchtwo(hit) print("Function started yay") if check() then print("Der was a humanoid yaaaaaaa") end end
script.Parent.Touched:connect(onTouchtwo) |
|
|
| Report Abuse |
|
|
UFAIL2
|
  |
| Joined: 14 Aug 2010 |
| Total Posts: 6905 |
|
| |
|
swmaniac
|
  |
| Joined: 28 Jun 2008 |
| Total Posts: 15773 |
|
|
| 27 Jan 2012 06:02 PM |
return ends a function and sends any data after it back to the calling function.
function getOne() return 1 print("This will never print") end
local number = getOne() print(number)
>1
Return can be used for any datatype. |
|
|
| Report Abuse |
|
|
Cyrok
|
  |
| Joined: 11 Jan 2012 |
| Total Posts: 630 |
|
|
| 27 Jan 2012 08:46 PM |
Return is a statement that immediately stops the function and gives you the optional choice of making the function it's inside of return any data type value.
function cout() print("Hello ") return "World!" print("Fin.") end
print(cout())
Output: Hello World! |
|
|
| Report Abuse |
|
|
|
| 27 Jan 2012 08:51 PM |
If you use return by itself, it stops the function it's in.
If you use return followed by a variable or data type, then wherever you called the function will "be" that piece of data. For example:
function func1() return 5 end
print(func1())
Would print "5".
If you want to check if the function returned something, you would want to use "not", which works like this:
function func1() return end
function func2() return nil end
function func3() return false end
function func4() return true end
if not func1() then print("func1") end
if not func2() then print("func2") end
if not func3() then print("func3") end
if func4() then print("func4") end
Would print: func1 func2 func3 func4
Make sense? |
|
|
| Report Abuse |
|
|
miz656
|
  |
| Joined: 19 Jul 2010 |
| Total Posts: 15336 |
|
| |
|
|
| 27 Jan 2012 10:16 PM |
it can return multiple values too!
local mr = math.random function getThreeRandomNumbers(max) max = max or 100 return mr(max),mr(max),mr(max) end
v1,v2,v3 = getThreeRandomNumbers() print(v1,v2,v3)
> 31 85 4 |
|
|
| Report Abuse |
|
|
miz656
|
  |
| Joined: 19 Jul 2010 |
| Total Posts: 15336 |
|
|
| 27 Jan 2012 10:25 PM |
@blue
Did you actually test that? |
|
|
| Report Abuse |
|
|
| |
|
|
| 27 Jan 2012 10:28 PM |
| lol just tested it and it works, lol im so good i dont need to test, i get it right first time every time :P jks. :P |
|
|
| Report Abuse |
|
|
miz656
|
  |
| Joined: 19 Jul 2010 |
| Total Posts: 15336 |
|
| |
|
|
| 27 Jan 2012 10:29 PM |
| but i dont normally test examples because im pretty fluent with Lua, because 95% of the time the tests are right, so i dont test them unless someone says that it doesn't work :P |
|
|
| Report Abuse |
|
|
|
| 27 Jan 2012 10:29 PM |
| wait a sec... you were that troll that said that my lighting place was sucky! >:O |
|
|
| Report Abuse |
|
|
miz656
|
  |
| Joined: 19 Jul 2010 |
| Total Posts: 15336 |
|
|
| 27 Jan 2012 10:31 PM |
Well I am a troll and I do suxonthings....
|
|
|
| Report Abuse |
|
|
|
| 27 Jan 2012 10:32 PM |
| ya, but I actually thought I had someone with a better lighting place (apart from oysi's realtime lighting) so I went to that persons place and it sucked >.< lol |
|
|
| Report Abuse |
|
|
miz656
|
  |
| Joined: 19 Jul 2010 |
| Total Posts: 15336 |
|
|
| 27 Jan 2012 10:34 PM |
| Lol. Yeah. One time there was this guy that thought his soccer game was better than mine but yet there was seriously NO SCRIPTS in the game XD |
|
|
| Report Abuse |
|
|
|
| 27 Jan 2012 10:39 PM |
| lol, i can see why you have the troll face on your shirt :P |
|
|
| Report Abuse |
|
|
miz656
|
  |
| Joined: 19 Jul 2010 |
| Total Posts: 15336 |
|
|
| 27 Jan 2012 10:44 PM |
| This is a troll face? I thought it was a blueymaddog face :) |
|
|
| Report Abuse |
|
|
|
| 27 Jan 2012 10:54 PM |
| nope, im gonna put on my blueymaddog face now, you cant take me serious when I put my bluey face on even though im in a buisness suit with a fedora :P |
|
|
| Report Abuse |
|
|
miz656
|
  |
| Joined: 19 Jul 2010 |
| Total Posts: 15336 |
|
|
| 27 Jan 2012 11:00 PM |
| LOL!!!YOU LOOK LIKE A SNOOKIE WITH CHEST MUSCLES :P |
|
|
| Report Abuse |
|
|
|
| 28 Jan 2012 04:21 AM |
| Bluey, Change Topic lol xD |
|
|
| Report Abuse |
|
|
|
| 28 Jan 2012 04:24 AM |
Return means making the function return an object
function Me() Me = game.Players.LuisPambid return Me end
Luis = Me() --Making the function to be a variable Luis:Destroy() --Removes ME! |
|
|
| Report Abuse |
|
|