Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What does "Returns" in scripting mean??? I'm so confused with it

Asked by 7 years ago

Could somebody explain to returns and the use of it because im so confused,

im still a begginer scripter, i tried learning scripting in the past, but i gave up because i couldnt

understand some points, so could u help me now, i dont want to give up again

0
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 DropshipPilot 148 — 7y

2 answers

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

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())
-- will print what you told it to return (3) in the output window.

Local number = quickMaths()
-- the variable "number" now equals what you told quickMaths() to return: 3.

If you want to get a little more advanced, you can add inputs when you create your function, too.

function aplusb(a, b) -- "a" and "b" are placeholders for any number we'll be able to put in later
  return(a+b)
end

number = aplusb(4, 6) -- run the function with all "a"s replaced with 4 and all "b"s replaced with 6

print(number) -- will print 4+6, so 10
print(aplusb(2, 2)) -- will print 4 (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!

Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

Scripting is not about learning from Roblox wiki all that reading and not getting no where will make you give up. But the pro make you think that they sat down and read roblox wiki. That is not the case, you find scripts that make things happen, like a fan script. You look at the fan blades and see how they rotate, then you look at the script and see what inside the script makes the blades rotate. When i read that wiki page by page, i am reading it but my imagination is going wild i am still asking the same question "How do i make this." You learn by making a project that you want to make, and use pre-made scripts, and if a script does not work try to find out whats causing it to not function. So when people sit there and say learn from roblox wiki that is a way of them saying they do not want to help. Wiki sometimes help but not for the main plate. Remember, find something you want to make first, then do some side research, watch video's. If the script does not work don't throw it away, play around it, see what happens to the script if you switch true to false. However the value of "Return" means a return statement returns occasional results from a function or simply finishes a function. There is an implicit return at the end of any function, so you do not need to use one if your function ends naturally, without returning any value

0
I agree with your point about reverse engineering, but I think you might be giving the wiki less credit than is due. Most of the stuff I learned through reverse engineering I could have checked quickly and easily in the Class Reference (aka API) which generally does a good job of explaining what every object in Roblox does. DropshipPilot 148 — 7y
0
Yea i guess, i didnt give RBXWiki enough credit. But sitting there just reading does not help at all. I also would be greatful if you would remove that negetive rep point. I already had -8 not i have -10. I honestly dont see why because everybody in here agreed to what i said. User#19513 5 — 7y

Answer this question