#Help understanding "return"

122 messages · Page 1 of 1 (latest)

gleaming grove
#

One message removed from a suspended account.

#

One message removed from a suspended account.

gleaming grove
#

One message removed from a suspended account.

rigid turtle
#

so returning is weird, took me a minute to understand myself

#

it allows you to send back any data you want, and you can create several conditions too

#

for example if a door is open, i can return “Hello!”

if the door is closed, i can return “Who’s there?”

rigid turtle
#

that function will run, and then make hotdogs equal “Awesome”

#

now when you print hotdogs, we’ll get the string “Awesome”

#

does that make sense?

gleaming grove
simple nymphBOT
#

studio** You are now Level 2! **studio

rigid turtle
#

when you set a variable equal to a function, the variable will retain the value of whatever (if anything) that the function returns

rigid turtle
#

so when he calls the function, it just prints that

#

but when he stores the returned value and print it, the function runs normally and then the script also prints the variable

#

did that help clarify?

#

oh btw the function isn’t the thing printing “Awesome”

#

which is why just calling the function won’t print “Awesome”

gleaming grove
#

One message removed from a suspended account.

#

One message removed from a suspended account.

#

One message removed from a suspended account.

rigid turtle
#

not quite

#

let me do something real quick

austere sky
#
function add(a, b)
return a + b
end

local answer = add(3, 2)
print(answer) -- 5
rigid turtle
#

thats exactly what i was gonna type out

#

return literally will return anything you provide it

#

so you got the numbers 2 and 3

#

and you're returning the sum of them

gleaming grove
#

One message removed from a suspended account.

rigid turtle
#

you recieve the number 5

#

oh alright

austere sky
#

Then what has confused you then?

gleaming grove
#

One message removed from a suspended account.

#

One message removed from a suspended account.

rigid turtle
#
local doorOpen = true

local function isOpen()
  if doorOpen == true then
    return "Yes!"
  else
    return "No..."
  end
end

local open = isOpen()
print(open) --This is where the value we return gets printed
#

@gleaming grove do you know what will be printed in this example

austere sky
#

When you assign a variable to a function with a return, whatever's in the return will be the value of that very variable.

gleaming grove
#

One message removed from a suspended account.

rigid turtle
#

yeah you got it

noble belfry
#

return its like call back?

rigid turtle
#

wydm

noble belfry
#

i mean

gleaming grove
noble belfry
#

return is literally return

rigid turtle
#

yeah

noble belfry
#

so if not in scripting

austere sky
#
function example()
return true
end
local result = example() -- result is the return value.
noble belfry
#

its gonna be equal by words with back to start

rigid turtle
#

i have no clue what you're trying to get at

#

this is a scripting thread

#

we arent talking outside of scripting

gleaming grove
#

One message removed from a suspended account.

rigid turtle
noble belfry
rigid turtle
#

basic but practical

noble belfry
#

its also with remote and bindable event i just cant understand where i can use it

rigid turtle
#

remote events are useful for server-client communication

gleaming grove
#

One message removed from a suspended account.

rigid turtle
#

bindables are good for when non-event conditions are met (i think)

#

dont quote me on that

#

i dont use them, they arent super helpful anyway

rigid turtle
rigid turtle
austere sky
#

You can also return multiple things if you wish.

function getData()
return "hi", true, nil

local str, myboolean, nothing = getData()
noble belfry
#

ig its gonna help a lot

rigid turtle
#

i made an admin script

#

the thing i needed was a function that could return a table of all the players the command targeted

#

just fyi it may be a lot to look at

#

dont confuse yourself with it if you can

#
local function getSubjects(target, plr)
    local subject_table ={}
    
    if target == nil or target == "me" then
        table.insert(subject_table, plr)
        
    elseif target == "all" then
        subject_table = game:GetService("Players"):GetChildren()
        
    elseif target == "others" then
        for _, person in ipairs(game:GetService("Players"):GetChildren()) do
            if person == plr then continue end
            table.insert(subject_table, person)
        end
        
    else
        for _, person in ipairs(game:GetService("Players"):GetChildren()) do
            local name = string.sub(string.lower(person.Name),1,#target)

            if name ~= target then continue end
            table.insert(subject_table, person)
        end
    end
    
    return subject_table or {plr} --returns here
end
#

omg that is so unoptimal...

#

its fine for now

gleaming grove
#

One message removed from a suspended account.

austere sky
#

Many experienced developers use return to exit functions quickly to optimize their code and have a cleaner look.

function giveGearTo(Player)

if not Player or not Player.Character then return end

local gear = Folder.Gear:Clone()
gear.Parent = Player.Character
end

giveGearTo(Player)
noble belfry
rigid turtle
#

quick quest table?

#

no idea what that is

noble belfry
#

is this table?

rigid turtle
#

you can make a table like that, yes

noble belfry
#

okay thx

rigid turtle
#

np

austere sky
rigid turtle
#

me doing target == nil is just a bad habit i have

#

but the pairs/ipairs thing?

#

replace them with nothing?

austere sky
#

You can just erase it

simple nymphBOT
#

studio** You are now Level 19! **studio

austere sky
#

Yes

rigid turtle
#

huh, didn’t know that

#

does removing it change the speed it works at

austere sky
#

Yes it works faster too

rigid turtle
#

i understand ipairs is slightly faster that pairs and stuff like that

#

oh good to know, thanks!

austere sky
#

No problem

noble belfry
#

it seems i understand this

#

like you said

#

return is literally return

#

right?

rigid turtle
#

yes, pretty much

noble belfry
#

so

#

it literally return the value and allow to use this function multiple times?

rigid turtle
#

yes

noble belfry
#

tysm

#

now i get it

rigid turtle
#

right returns don’t always have to be used to return values

#

they’ll also void all code within the current scope you’re in

noble belfry
austere sky
noble belfry
#

or im just not experienced