#Help understanding "return"
122 messages · Page 1 of 1 (latest)
this might help
One message removed from a suspended account.
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?”
the way it was done here, he assigned that value he returned to a variable
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?
One message removed from a suspended account.
** You are now Level 2! **
when you set a variable equal to a function, the variable will retain the value of whatever (if anything) that the function returns
the function itself prints “hi”
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”
One message removed from a suspended account.
One message removed from a suspended account.
One message removed from a suspended account.
function add(a, b)
return a + b
end
local answer = add(3, 2)
print(answer) -- 5
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
One message removed from a suspended account.
Then what has confused you then?
One message removed from a suspended account.
One message removed from a suspended account.
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
When you assign a variable to a function with a return, whatever's in the return will be the value of that very variable.
One message removed from a suspended account.
yeah you got it
just an question
return its like call back?
wydm
i mean
One message removed from a suspended account.
return is literally return
yeah
so if not in scripting
function example()
return true
end
local result = example() -- result is the return value.
its gonna be equal by words with back to start
i have no clue what you're trying to get at
this is a scripting thread
we arent talking outside of scripting
One message removed from a suspended account.
good! once you use it frequently it will be like second nature
cuz im aslo dont know how that working because i cant imagine how i can use it
this is a good example of practical use
basic but practical
its also with remote and bindable event i just cant understand where i can use it
remote events are useful for server-client communication
One message removed from a suspended account.
One message removed from a suspended account.
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
glad to hear! you're always fine to make a new post if you get stuck again, too!
would it help if i showed a segment of one of my scripts where i used returning?
You can also return multiple things if you wish.
function getData()
return "hi", true, nil
local str, myboolean, nothing = getData()
sure
ig its gonna help a lot
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
One message removed from a suspended account.
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)
ok i just updated it
quick quest table is local t = {} ?
sorry i mean quick question about table it
local t = {}
is this table?
you can make a table like that, yes
okay thx
np
You don't need to check if the target is nil like that, doing if not target can work as well with the method I just showcased.
ipairs and and pairs are not needed anymore for a long while, you can replace those with nothing and it will work thanks to an old Luau update.
me doing target == nil is just a bad habit i have
but the pairs/ipairs thing?
replace them with nothing?
You can just erase it
** You are now Level 19! **
Yes
Yes it works faster too
i understand ipairs is slightly faster that pairs and stuff like that
oh good to know, thanks!
No problem
so
it seems i understand this
like you said
return is literally return
right?
yes, pretty much
yes
And this
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
i can suppose that in line 3 this is checking if its player and if not its back to start and waiting to be called?
No, it means the function will just end itself like a kill switch.
or im just not experienced
oh
okay