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: What is "return"

Previous Thread :: Next Thread 
spartan6241 is not online. spartan6241
Joined: 15 Jun 2014
Total Posts: 793
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
AggressiveCatch is not online. AggressiveCatch
Joined: 17 Jul 2011
Total Posts: 5840
07 Apr 2016 07:45 PM
function bruh ()
return "BRUHHH"
end

print(bruh())

> "BRUHHH"
Report Abuse
LeitrisArcade is not online. LeitrisArcade
Joined: 15 May 2010
Total Posts: 2127
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
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
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
LegendaryAccount is not online. LegendaryAccount
Joined: 02 Jun 2010
Total Posts: 13193
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
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
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
AggressiveCatch is not online. AggressiveCatch
Joined: 17 Jul 2011
Total Posts: 5840
07 Apr 2016 07:49 PM
uhh im pretty sure the primary purpose of return is to return a value :/
Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
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
spartan6241 is not online. spartan6241
Joined: 15 Jun 2014
Total Posts: 793
07 Apr 2016 07:50 PM
I'm not very experienced with scripting and is there really any reason to use "print"?
Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
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
AggressiveCatch is not online. AggressiveCatch
Joined: 17 Jul 2011
Total Posts: 5840
07 Apr 2016 07:53 PM
i use print to make entire games, what are u even saying..?
Report Abuse
spartan6241 is not online. spartan6241
Joined: 15 Jun 2014
Total Posts: 793
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
AggressiveCatch is not online. AggressiveCatch
Joined: 17 Jul 2011
Total Posts: 5840
07 Apr 2016 07:53 PM
uh ya that should work
Report Abuse
AggressiveCatch is not online. AggressiveCatch
Joined: 17 Jul 2011
Total Posts: 5840
07 Apr 2016 07:54 PM
well that idea would work

but u cant change the name of the workspace
Report Abuse
spartan6241 is not online. spartan6241
Joined: 15 Jun 2014
Total Posts: 793
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
LeitrisArcade is not online. LeitrisArcade
Joined: 15 May 2010
Total Posts: 2127
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
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
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
spartan6241 is not online. spartan6241
Joined: 15 Jun 2014
Total Posts: 793
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
AggressiveCatch is not online. AggressiveCatch
Joined: 17 Jul 2011
Total Posts: 5840
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
AggressiveCatch is not online. AggressiveCatch
Joined: 17 Jul 2011
Total Posts: 5840
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
spartan6241 is not online. spartan6241
Joined: 15 Jun 2014
Total Posts: 793
07 Apr 2016 08:03 PM
This has been very helpful
Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
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 is not online. 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
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