|
| 14 Jun 2015 11:43 PM |
| Can someone explain return to me? |
|
|
| Report Abuse |
|
|
|
| 14 Jun 2015 11:45 PM |
function hello() return "Hello" end
function hello2() print("Hello") end
ok.. so...
for return... it just is stutffffff
local hi = hello()
print(hi)
-> "Hello" because we made hello() return "Hello"
print(hello()) does the same thing |
|
|
| Report Abuse |
|
|
|
| 14 Jun 2015 11:46 PM |
That was a horrible example.
function add(one, two)
return one _ two
end
print(add(5,5)) --> 10 print(add(6,6)) --> 12 |
|
|
| Report Abuse |
|
|
| |
|
| |
|
|
| 14 Jun 2015 11:50 PM |
function add(one, two)
return one + two
could you explain that a little more?
like what does the "one, two" mean in the "function add()"
and what would returning them do? e.o |
|
|
| Report Abuse |
|
|
|
| 14 Jun 2015 11:56 PM |
one would be a parameter, two would be aswell.
you know how we do
function add(one, two)
one and two are basically variables for when you call it like
add(1,2)
then the return
function add(one, two) return one + two end
in return it adds the two numbers (hopefully numbers...) and then returns it.
so like
local three = add(2,1)
because 2 + 1 = 3, right?
then if we wanted to print 3 we could do it two ways now
print(three) or print(add(2,1))
so yeah |
|
|
| Report Abuse |
|
|
|
| 14 Jun 2015 11:59 PM |
what's the purpose of it if you could just print the numbers?
is return even necessary to use? |
|
|
| Report Abuse |
|
|
|
| 15 Jun 2015 12:01 AM |
yes, yes it is.
look at this snippet i made for loading asset ids for a hat;
function GetMeshAndTextureIds(hatId) for _,c in next, game:GetService("InsertService"):LoadAsset(hatId):GetChildren() do if c:IsA("Hat") then return {c.Handle.Mesh.MeshId, c.Handle.Mesh.TextureId} end end end
it returns the meshid and textureid of a hat.
so, to get one of them you'd do local MeshId = GetMeshAndTextureIds(some_random_hat_id_here)[1] and yeah
so it is useful |
|
|
| Report Abuse |
|
|
|
| 15 Jun 2015 12:05 AM |
| this whole "return" thing confuses me, idk why. It seems like it should be easy.. >.> |
|
|
| Report Abuse |
|
|
|
| 15 Jun 2015 12:07 AM |
it's easy once you understand it. trust me. i remember getting confused with this return stuff, but do you understand parameters?
function test(a,b) print(a..b) end
you get that, right? |
|
|
| Report Abuse |
|
|
|
| 15 Jun 2015 12:08 AM |
if by parameters you mean...
a = "hello" b = " how are you"
print(a.. b) |
|
|
| Report Abuse |
|
|
|
| 15 Jun 2015 12:10 AM |
no nonono
like when you call a function
test("hello", " how are you")
would do the same thing |
|
|
| Report Abuse |
|
|
|
| 15 Jun 2015 12:13 AM |
so like
function test(num1, num2) -- they work as variables right now
return num1, num2 -- idek wot it was to do with anything haha
end
lol = test(2,2) print(lol)
i think i get that much, it's just I am confused about the return's role in the script |
|
|
| Report Abuse |
|
|
|
| 15 Jun 2015 12:16 AM |
check this out
http://wiki.roblox.com/index.php?title=Return#Using_Return
maybe it explains more than i do |
|
|
| Report Abuse |
|
|
|
| 15 Jun 2015 12:19 AM |
| i sorta understand, but couldn't you just use regular prints and stuff for basic stuff like that? |
|
|
| Report Abuse |
|
|
|
| 15 Jun 2015 12:25 AM |
The whole point of these examples is detailing how it works.
Later it could like like
function DestroyAllFrom(place)
local children = place:GetChildren()
for _, v in pairs(children)do v:Destroy() end
return #children --return number of children destroyed
end
local destroyed = DestroyAllFrom(game.Workspace)
print(destroyed)
|
|
|
| Report Abuse |
|
|