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: In ROBLOX Lua, what would be a practical use for 'return'?

Previous Thread :: Next Thread 
Vid_eo is not online. Vid_eo
Joined: 30 Jul 2013
Total Posts: 2580
16 Jan 2017 08:42 PM
They're kind of confusing to me, and I don't see an extremely practical use for them, even though I'm sure there is one.



Report Abuse
Vid_eo is not online. Vid_eo
Joined: 30 Jul 2013
Total Posts: 2580
16 Jan 2017 08:43 PM
1


Report Abuse
Disillusions is not online. Disillusions
Joined: 10 Jul 2011
Total Posts: 6365
16 Jan 2017 08:45 PM
take the method findFirstChild(name)

if you were to type it like a function, it would most likely be this:

function FindFirstChild(Parent,ChildName)
for i, v in pairs(Parent:GetChildren()) do
if v.Name == ChildName then
return v
end
end
return nil
end)

its useful in the case when you use the same block of code multiple times, and need a different output for each one.


#code self = nil
Report Abuse
Vid_eo is not online. Vid_eo
Joined: 30 Jul 2013
Total Posts: 2580
16 Jan 2017 08:45 PM
2


Report Abuse
Salinas23 is not online. Salinas23
Joined: 28 Dec 2008
Total Posts: 37141
16 Jan 2017 08:45 PM
let's say you wanna do certain stuff if a player bought your vip shirt

local function ownsVIP(player, assetID)
return marketplaceService:PlayerOwnsAsset(player, assetID)
end

then somewhere else

if ownsVIP(player, 123456) then
print("this player is vip")
else
print("this player is not vip")
end



I have a spider on my back. R$24,985
Report Abuse
Vid_eo is not online. Vid_eo
Joined: 30 Jul 2013
Total Posts: 2580
16 Jan 2017 08:53 PM
Any simple explanations? I kind of understand what dis says, but I have another question;
can return become a variable? like a = return SOMETHING

Is return technically like a 'print', except it stops a function?


Report Abuse
Vid_eo is not online. Vid_eo
Joined: 30 Jul 2013
Total Posts: 2580
16 Jan 2017 08:59 PM
1


Report Abuse
Laedere is online. Laedere
Joined: 17 Jun 2013
Total Posts: 23601
16 Jan 2017 09:04 PM
function a()
return 2
end

b = a()
print(b) -- 2
Report Abuse
fireflame09225 is not online. fireflame09225
Joined: 04 Jan 2010
Total Posts: 1818
16 Jan 2017 09:06 PM
So, return does exactly what it's name suggests. It returns something. This used to confuse me a lot too, but I was just overthinking it, and so are you.

Here's a simple example:

function AddNumbers(num1, num2)
return num1 + num2
end


So now, whenever you call AddNumbers(), it will return the sum of the numbers you pass through it. So you could do print(AddNumbers(2, 3)) and it would print out 5.

It does have practical uses. Just an example is if you wanted to test if a player completed an objective. You could do

function check()
if player.Objective == true then
return true
else
return false
end

Then later in the script you can do

if check() == true then
print("Player completed it!!")
end


Report Abuse
Vid_eo is not online. Vid_eo
Joined: 30 Jul 2013
Total Posts: 2580
16 Jan 2017 09:11 PM
okay i kind of am understanding it more
but can't you just make a variable called, say, 'objectiveCompleted/, then set it to true/false, and do if objective == true then?


Report Abuse
Salinas23 is not online. Salinas23
Joined: 28 Dec 2008
Total Posts: 37141
16 Jan 2017 09:41 PM
In most cases I use return is to return a true/false variable. In my case, the whole function becomes a variable. Here's a more simpler example

local function math()
return 2+2 == 5
end

local m = math()

print(m) --> false


I have a spider on my back. R$24,985
Report Abuse
foreverpower is not online. foreverpower
Joined: 05 Feb 2011
Total Posts: 5578
16 Jan 2017 09:42 PM
^
What?


Report Abuse
foreverpower is not online. foreverpower
Joined: 05 Feb 2011
Total Posts: 5578
16 Jan 2017 09:43 PM
That was directed at Vid_eo


Report Abuse
SunTzu16 is not online. SunTzu16
Joined: 10 Jul 2012
Total Posts: 999
16 Jan 2017 09:57 PM
I think return is good for situations where you don't want to repeat code.

For example, I have an algorithm that'll take some inputs and it'll give me an output depending on those inputs. I'll need to use this algorithm in more than one situation, so I make it a separate function that I can call.

I can set a value equal to what this function will return.

local value = alg(i#########################It might return true, so now "value" equals "true".

Now I can don't need to keep re-typing the algorithm each time I want to run similar inputs through it. Instead, I just call the function and give it some inputs, and it'll tell me what happens using return.

local alg = function(a,b,c)
if a and b and not c then
return false
elseif not a and b and c then
return true
end end

Just a really realllllllly simple representation of return being used.

Or similarly...

local checkMoney = function(cash,cost)
if cash >= cost then
return true
elseif cash < cost then
return false
end end

Whenever the player purchases something, I can call the function "checkMoney" and compare the amount of money the player has to the cost of the item. If the player has enough, then they can purchase, if they don't have enough, then they cannot. (I think it'd be a lot more efficient to just use an if/then statement each time... (and if you're using GUIs or something, you could write 1 function in 1 script for 40 buttons).

if checkMoney(cash,cost) then
Purchase(item)
end

Um... I've also used return for an algorithm that checks if you're on a team allied to another team. In a situation where you could be allied to two people, 0 people, 1 person, and it can change, and you want to be able to expand the amount of allies up to like, 100 as an example, without having to constantly add more checks, like:

if teama and teamb and teamc and not teamd and...

You could instead take the two teams in question, perhaps it's for guns so you can't hurt the allied team... Then you pass them through an algorithm that will return true if they're allied and return false if they're enemies.

Idk, just figured I'd give some examples of when you could use such a feature. I haven't actually coded in ages, so I might have messed up some syntax. Let's just call it psuedo code, ;p

Report Abuse
Bit_blox is not online. Bit_blox
Joined: 28 Aug 2016
Total Posts: 974
16 Jan 2017 10:04 PM
ah I get it now too

basically if you want to check if someone does something like the example given a few posts above this one:

function playerpassed()
if player.Passed == true then
return true
else
return false
end

in this case, the returns slap a variable on that function, which can be used later to determine the outcome of another situation:

if playerpassed() == true then
print("yay")
end

it takes the previous variable that was slapped on the function using return true/false, then uses it to see if you are worthy of the famous "yay"

:|


#code print("ro-cheezits are a thing now")
Report Abuse
foreverpower is not online. foreverpower
Joined: 05 Feb 2011
Total Posts: 5578
16 Jan 2017 10:21 PM
function playerpassed()
if player.Passed == true then
return true
else
return false
end

No, this is not how you use functions at all.


Report Abuse
SunTzu16 is not online. SunTzu16
Joined: 10 Jul 2012
Total Posts: 999
16 Jan 2017 10:30 PM
You don't really need to use 'return' most of the time.
Report Abuse
SunTzu16 is not online. SunTzu16
Joined: 10 Jul 2012
Total Posts: 999
16 Jan 2017 10:36 PM
Example of the thing I was talking about that checks allies, basically. It'll go through a table of different combinations and such, and return true/false depending on the situation. Similarly, there's a function below it that more or less converts a TeamColor into a Team. You can see that FindTeam() is called more than once at the top of the TargetShouldDie function.

Instead of typing out that twice, you can just write a function to do it and call it whenever you need to do that. It returns the value, so you're able to set a variable to what that function returns.

local TargetTeam = FindTeam(TargetTeamColor)

________________________________________________

function TargetShouldDie(Target)
local Passed = true
if Target ~= nil then
local TargetTeamColor = Target.TeamColor
local TargetTeam = FindTeam(TargetTeamColor)
local PlayerTeamColor = Getlocalplayer().TeamColor
local PlayerTeam = FindTeam(PlayerTeamColor)
local TTN = TargetTeam.Name
local PTN = PlayerTeam.Name
for _, Set in pairs(TeamSettings) do
if Set[3] == true then
if PTN == Set[1] then
if TTN == Set[2] then
Passed = false
end
elseif TTN == Set[1] then
if PTN == Set[2] then
Passed = false
end end
elseif Set[3] == false then
if PTN == Set[1] then
if TTN == Set[2] then
Passed = false
end end end end end return Passed end

_____________________________________

function FindTeam(Color)
for _,A in pairs(game.Teams:GetChildren()) do
if A.TeamColor == Color then return A end
end end
Report Abuse
Bit_blox is not online. Bit_blox
Joined: 28 Aug 2016
Total Posts: 974
16 Jan 2017 10:49 PM
how would you connect the function to make it work though? wouldnt it need to be manually triggered?


#code print("ro-cheezits are a thing now")
Report Abuse
SunTzu16 is not online. SunTzu16
Joined: 10 Jul 2012
Total Posts: 999
16 Jan 2017 11:07 PM
When something happens, like in the case of the script I posted...

When someone shoots another player, it checks if they're an allied player or an enemy player. When you shoot that person, that's when you call that function. That's when you trigger/run the function.

Honestly, if you don't know how to use return, then you probably don't need it. When you need something like return, you'll know. (You might forget that a feature like what you need exists...)

Sometimes I just use it for fun to be honest...
Report Abuse
BlueTaslem is not online. BlueTaslem
Joined: 11 May 2008
Total Posts: 11060
17 Jan 2017 12:27 AM
`return` is absolutely necessary for all functions (that are "functions" in the mathematical sense, as opposed to procedures).

A function takes in values, does some work, and then spits out values. `return` says which value to spit out.

For example, here's a silly example:

function greet(name)
return "Hello " .. name
end

function enthusiastically(say)
return say .. "!"
end

print( enthusiastically(greet("BlueTaslem")) ) --> Hello BlueTaslem!

More practical examples might be a function that gives a list of all of the players on a team:

function playersOnTeam(team)
local players = {}
for _, player in pairs(game.Players:GetPlayers()) do
if player.Team == team then
table.insert(players, player)
end
end
return players
end

for _, player in pairs(playersOnTeam(game.Teams.BlueTeam)) do
print(player.Name .. " is on BlueTeam")
end

Functions are really the main, important primitive of modern programming. Almost any program can be made better by deliberate and liberal use of functions.
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