I'm going to be reiterating Jeterhipobunbun's answer a little, but I found their answer could have been slightly condensed. Thanks, Jeter, you said it first!
A function in a script, in most basic cases, is just a container for code. When you call a function by its name, it's like pulling the box off the shelf and running a copy of the code inside it.
Return comes into play if you want your function to output something. When a function specifies a "return", it means anywhere the function appears in the code will be replaced with whatever you told it to return. Sort of like how a variable is.
The best example i've found is if you want to frequently re-use a math equation, but you don't want to copy and paste the equation into your script a bunch of times.
function quickMaths()
return 2 + 2 - 1
end
print(quickMaths())
Local number = quickMaths()
If you want to get a little more advanced, you can add inputs when you create your function, too.
function aplusb(a, b)
return(a+b)
end
number = aplusb(4, 6)
print(number)
print(aplusb(2, 2))
Once you tell the function to return something, the function will stop right away. This means you can manually tell a function to stop by telling it to return nothing.
Careful, though, as telling the script it has to do anything after the return usually results in an error. If you need your function to do something else after you've got the value you want it to return, it's better to keep track of the value with a variable, and return that variable when there's nothing else left to do.
Hope this helps!
1 comment
I gave up on scripting once like you did, but I have to say, don't give up! Reverse engineer, like Jeter said. Search for the "cookbook" on roblox wiki, and read it front-to-back. Explore the wiki's Class Reference, which is a list of how every object in Roblox works (clicking the button that said "API" at the top of the page takes you to the Class Reference). And take baby steps! a lot of tri