|
| 07 Apr 2016 07:44 PM |
| I just read the wiki and didn't really understand it much, does anyone have a simple and easier to understand explanation or example? |
|
|
| Report Abuse |
|
|
|
| 07 Apr 2016 07:45 PM |
function bruh () return "BRUHHH" end
print(bruh())
> "BRUHHH" |
|
|
| Report Abuse |
|
|
|
| 07 Apr 2016 07:47 PM |
"return" returns a variable. Here's an example:
local function AddNumbers(num1,num2) return(num1+num2) end
local solution = AddNumbers(1,5)
FilteringDisabled is for squares |
|
|
| Report Abuse |
|
|
|
| 07 Apr 2016 07:48 PM |
Well when you call a function, it has to eventually stop so it the script can continue where it left off. What return does is tell the current function to return control back to whoever called it so it can actually go back. Additionally, sometimes you want information back from a function, so you can return value(s) if you want so that whoever called it will receive those values.
function divide(a, b) if b == 0 then -- technically, -0 can exist but that's beyond the scope of this post return "could not divide!" else return a/b end end
local result = divide(1, 2) -- divide 1 by 2, the return value will be in the result variable print(result) print(divide(1, 0)) |
|
|
| Report Abuse |
|
|
|
| 07 Apr 2016 07:48 PM |
when making a function you may want it to be able to send data back from it. thats when the return is needed
function Name() return "Bob" end name = Name() -- name is Bob now -------------------------------- or this
function Add(num1,num2) return num1+num2 end print(Add(9,10))
|
|
|
| Report Abuse |
|
|
|
| 07 Apr 2016 07:48 PM |
'"return" returns a variable. Here's an example:' Not accurate, you probably meant value though. And that's not its primary purpose. |
|
|
| Report Abuse |
|
|
|
| 07 Apr 2016 07:49 PM |
| uhh im pretty sure the primary purpose of return is to return a value :/ |
|
|
| Report Abuse |
|
|
|
| 07 Apr 2016 07:50 PM |
| No, although that's what it may sound like. Technically all functions have to return, and most people don't think about the purpose of return but think of it more as a way to "get values back" when it's more than that :) |
|
|
| Report Abuse |
|
|
|
| 07 Apr 2016 07:50 PM |
| I'm not very experienced with scripting and is there really any reason to use "print"? |
|
|
| Report Abuse |
|
|
|
| 07 Apr 2016 07:51 PM |
| Pretty much the only reason you might want to use print is for debugging purposes since in-game you won't necessarily see it (blah server console blah). |
|
|
| Report Abuse |
|
|
|
| 07 Apr 2016 07:53 PM |
| i use print to make entire games, what are u even saying..? |
|
|
| Report Abuse |
|
|
|
| 07 Apr 2016 07:53 PM |
Could I use return for other purposes rather than printing results? Like can I do this?
function divide(a, b) if b == 0 then -- technically, -0 can exist but that's beyond the scope of this post return "could not divide!" else return a/b end end
local result = divide(1, 2) -- divide 1 by 2, the return value will be in the result variable game.Workspace.Name = result |
|
|
| Report Abuse |
|
|
| |
|
|
| 07 Apr 2016 07:54 PM |
well that idea would work
but u cant change the name of the workspace |
|
|
| Report Abuse |
|
|
|
| 07 Apr 2016 07:54 PM |
| Well, thank you guys so much for helping me understand this better. I appreciate the help from you all! |
|
|
| Report Abuse |
|
|
|
| 07 Apr 2016 07:55 PM |
Let me correct myself then, yes I meant value. Also, "return" can theoretically return any number of things. It can be used to return an error message, values, etc.
local function CanDoTheDo(canDo) if canDo then return("Can do the do") else return("Can't do the do") end end
print(CanDoTheDo(true)) error(CanDoTheDo(false))
There's all kinds of ways to use return, and I don't see how it can have any one primary purpose. Scripts wait to run the next line of code after calling a function regardless if you return anything, it's just there in case you need to get data back from a function.
FilteringDisabled is for squares |
|
|
| Report Abuse |
|
|
|
| 07 Apr 2016 07:57 PM |
Not trying to be pedantic but you can set the name of workspace :)
"There's all kinds of ways to use return, and I don't see how it can have any one primary purpose" By primary purpose I meant the most important purpose. |
|
|
| Report Abuse |
|
|
|
| 07 Apr 2016 07:59 PM |
| I am trying to use remote functions. How would I make a script so that when I click a textbutton then the workspace's name changes into another different name? |
|
|
| Report Abuse |
|
|
|
| 07 Apr 2016 07:59 PM |
oh im changing the name of every single workspace i use now, thx
and interesting how it doesn't really break any scripts |
|
|
| Report Abuse |
|
|
|
| 07 Apr 2016 08:02 PM |
| haha im having fun renaming all of the services (i guess thats what they are called) in my game |
|
|
| Report Abuse |
|
|
|
| 07 Apr 2016 08:03 PM |
| This has been very helpful |
|
|
| Report Abuse |
|
|
|
| 07 Apr 2016 08:04 PM |
Well it won't break it because Roblox did something clever to prevent this stuff from breaking, 'Workspace' and 'workspace' are properties of game that reference to the workspace object.
But if you change the name of Players, then it'll mess up any script that doesn't already have a reference to players and uses game.Players instead of GetService. |
|
|
| Report Abuse |
|
|
Iplaydev
|
  |
| Joined: 19 Dec 2011 |
| Total Posts: 292 |
|
|
| 07 Apr 2016 08:35 PM |
it will 'return' a variable
function lolno() return 1+1 end
print(lolno())
> 2
snort |
|
|
| Report Abuse |
|
|