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: can someone explain 'return'

Previous Thread :: Next Thread 
Tokimonu is not online. Tokimonu
Joined: 18 Sep 2009
Total Posts: 643
05 Jun 2015 11:55 PM
i've never really used it, nor learned it, and i have no idea what it is for but i have a feeling that i'd need to use it in my chat gui

so like, basically, the player types stuff in the chat bar and then it needs to save the message

if i needed to return the text into a string, would i do like

return cl.Text
or return msg
or wat

like, i need to return cl.Text into msg for my function to work
i'm just a lost puppy guys pls help
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
05 Jun 2015 11:57 PM
function color(x,y,z)
return Color3.new(x/255,y/255,z/255)
end

gui.TextColor3 = color(50,50,50)

Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
05 Jun 2015 11:58 PM
return means returning a variable in the function.
Report Abuse
amanda is not online. amanda
Joined: 21 Nov 2006
Total Posts: 5925
05 Jun 2015 11:58 PM
k so im going to do the oldest example

--

u wanna add 2 numbers

--

local add = function(num, num2)
local sum = num + num2
end


add(2, 5)

That's no good, let's set it equal to a variable

local seven = add(2, 5)
print(seven) -->nil

OGM WT HAPPN

this is why we return

local add = function(num, num2)
local sum = num + num2
return sum
end

local seven = add(2, 5)
print(seven) -->7

the function, when called, becomes whatever is returned, so that function just became the value of 7 because i returned 7
Report Abuse
nQqzRYVpIKA5jLP is not online. nQqzRYVpIKA5jLP
Joined: 05 Mar 2015
Total Posts: 4135
05 Jun 2015 11:58 PM
You need it for your chatbar? You need it for a lot more than your chatbar. Return "returns" execution back to the thread and can also return a value to the caller. Example:

function add(num1, num2)
local sum = num1 + num2 --calculate the sum
return sum --give this back to the caller
end

local number = add(5, 10) --This variable will be 15 because that is what the function returns


Also:

function test(somevalue)
if somevalue == 5 then
return --this is the same as returning nil. If this code is ran, the rest of this function will not be ran
end

return 0
end

local test1 = test(2) --this value will be 0
local test2 = test(5) --this will be nil because the code returns and thus the rest of the code is never executed
Report Abuse
amanda is not online. amanda
Joined: 21 Nov 2006
Total Posts: 5925
06 Jun 2015 12:00 AM
wuhuhuhuhuhuh
Report Abuse
Tokimonu is not online. Tokimonu
Joined: 18 Sep 2009
Total Posts: 643
06 Jun 2015 12:06 AM
@everyone
alright, thanks

@nQqz
how would i even do this e.e
so like, i have a function:

function mtl(plr, msg)

it creates 2 textlabels, one containing the players name, and the other containing the message

then, I have UserInputService to register when they press / and enter (aka Enum.KeyCode.Return, it took me 5 minutes to find that because i forgot it is return gg) and at the end of it, should it just do mtl(plr.Name, cl.Text) ?

tl;dr my function is function mtl(plr, msg)
at the end of my UIS should i do mtl(plr.Name, cl.Text)?

or something else?
Report Abuse
nQqzRYVpIKA5jLP is not online. nQqzRYVpIKA5jLP
Joined: 05 Mar 2015
Total Posts: 4135
06 Jun 2015 12:08 AM
I'm not sure I quite understand?
Report Abuse
Tokimonu is not online. Tokimonu
Joined: 18 Sep 2009
Total Posts: 643
06 Jun 2015 12:10 AM
yeah, sorry
that took like 500 years to explain because of how bad i am at explaining

so, lemme retry

i have a function called function mtl(plr, msg)
the arguments are plr and message

in the chatbar portion of the script, when the user hits enter, should i save their message (aka textbox.Text) to a value? or call the function like:

mtl(plr.Name, textbox.Text)

hopefully that's a little bit better
Report Abuse
nQqzRYVpIKA5jLP is not online. nQqzRYVpIKA5jLP
Joined: 05 Mar 2015
Total Posts: 4135
06 Jun 2015 12:16 AM
It should work either way.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
06 Jun 2015 12:19 AM
Returns control flow to the caller and literally returns value(s).
Report Abuse
Tokimonu is not online. Tokimonu
Joined: 18 Sep 2009
Total Posts: 643
06 Jun 2015 12:24 AM
okay, thanks
Report Abuse
nQqzRYVpIKA5jLP is not online. nQqzRYVpIKA5jLP
Joined: 05 Mar 2015
Total Posts: 4135
06 Jun 2015 12:46 AM
cnt, you're rather verbose in your explanation.
Report Abuse
RegularTetragon is not online. RegularTetragon
Joined: 10 Oct 2009
Total Posts: 3085
06 Jun 2015 12:51 AM
--As an output
function add(a,b)
return a+b
end
print(add(1,1))--Prints 2
print(add(2,2))--Prints 4
print(add(3,3))--Prints 6
print(add(4,4))--Prints 8


--As an escape
function doStuff(isTrue)
print("This will happen every time")
if isTrue then
return
end
print("This will only happen if isTrue is false")
end
doStuff(false)--prints "This will happen every time" followed by "This will only happen if isTrue is false"
doStuff(true)--prints "This will happen every time"

--As an output and escape
function isBetweenInclusive(n,lowerBound,upperBound)
if (n>=lowerBound and n<=upperBound) then
return true
end
return false--This will run if neither of the previous were true
end

It's delightful, It's delicious, It's delovely!
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