generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Scripting questions

Previous Thread :: Next Thread 
Ojia is not online. Ojia
Joined: 08 Feb 2012
Total Posts: 650
16 Dec 2014 10:45 PM
Thank you for answering my earlier questions if you already have, here are some I'm still stuck with.

1. What does return mean and how would you use it?

2. What does declaring a local before a function mean, or is it the same as not declaring it before a function (Ex. local function onTouch())

3. How would you use a coroutine correctly?

4. What is a ModuleScript and what is the difference between a normal script? And when would you need to use one?

I'll come back again if I have more, thank you for reading.
Report Abuse
iceman72 is not online. iceman72
Joined: 08 Jun 2008
Total Posts: 786
16 Dec 2014 11:27 PM
I too am new, but I can answer a few! return is a function that will end a bracket (think that's what they are called) A bracket is any kind of loop, a if statement, or a function. I think there are some other, but them are the main. It also makes that bracket = whatever you specify after the return. example
x=1
while x<5 do
x=x+1
return "Hi"
end
print (x)
This would normal loop 5 times, but with the return it wont even make two loops. As soon as it hits the return it jumps out of the while loop and makes x=hi thus making it print out hi.There really popular in functions where they can stop it prematurely

All local means is it can only be accessed on that same bracket(again not sure if that's the real name) ex

local x=2
if x=2 then
local y=3
print (x)
end
print (y)

In this case the program will display 2 in the first print, but in the second print it will display nil because y only == 3 in that if statement and nowhere else.

Hope that helps, Im sorry I cant answer the other two :(
Report Abuse
ChiefDelta is not online. ChiefDelta
Joined: 05 Nov 2010
Total Posts: 13071
17 Dec 2014 04:02 AM
Let me try to explain.

1. What does return mean and how would you use it?
Return ends the function right there, and you can return a value to it. It's better to show an example.

function test()
return
print'Stuff.'
end

test()

Note how 'Stuff.' won't print to the output? That's because return ended the function before that code could execute. In this context, it's used like "break" is for loops.

It can also be used like so:

function test()
return 3
end

number = test()

Because test() returns the number 3, number is equal to 3.

2. What does declaring a local before a function mean, or is it the same as not declaring it before a function (Ex. local function onTouch())

local function is used to make it more "efficent". It's pretty silly. Basically, it's unnecessary. It makes a fraction of a second's difference for the most part. Usually used by "efficency freaks" or people who value the "beauty" of code.

3. How would you use a coroutine correctly?

Coroutines are used to "simulate" (is it simulating, is it real...) multi-threading (google).

Basically, you can run two things "at the same time" (while they're yielding but you get the point).

I don't even remember the coroutine anon function syntax so I'll give you an example with delay() which I use a lot.

delay(0, function()
while wait() do
print 'a'
end
end)

delay(0, function()
while wait() do
print 'b'
end
end)

As you can see, it will go like : a b a b a b etc.

Now, if you do this:

while wait() do
print 'a'
end

while wait() do
print 'b'
end

The script is going to be printing a forever because it will stay in the loop.

Basically you can run multiple things at the "same" time.

I'm not the best at explaining coroutines but maybe someone else can help.

4. What is a ModuleScript and what is the difference between a normal script? And when would you need to use one?

If you've ever heard of programming "libraries" it's sort of what it is. Modulescripts are used to have a bunch of functions and you can call them from other scripts. This means your scripting will be way cleaner and easier.

Perhaps you've used math.random() before.

You could make a custom module, and you could make your own functions, such as yourModuleName.moduleFunction() and use that in a script.

Modulescripts are a lot different from scripts and are used to benefit other scripts.

You have to "require" them in a script to use them. Check the Wiki for more details.


um hope I helped a bit
yeah
Report Abuse
Ojia is not online. Ojia
Joined: 08 Feb 2012
Total Posts: 650
17 Dec 2014 08:59 AM
So return is basically a break and a value holder, that makes lots of sense thank you.

And declaring a local before a script is just a "prefered" way but not needed.

Giving me coroutine in-depth also helps so thank you.

And module script is just basically a holder to keep things, that makes sense.
Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
17 Dec 2014 09:39 AM
1) Return gives a function a value when it's called. Makes way more sense in languages like C# and stuff.
2) Local just means that the variable only exists inside the scope it was declared in. Also totally awesome for speeding up resource intensive scripts with heavy caching.
3) You don't.
4) ModuleScripts are special scripts that only run once, return a value, and then return the same value every time. The special thing about them is that they can store functions that are used a lot, or can just be there as scripts designed to run once (when needed)
Report Abuse
Ojia is not online. Ojia
Joined: 08 Feb 2012
Total Posts: 650
21 Dec 2014 12:51 AM
Ok, was I right on what I said before or no?
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
21 Dec 2014 01:07 AM
I'll go into some depth, simple stuff first:

'4. What is a ModuleScript and what is the difference between a normal script? And when would you need to use one?'
A modulescript doesn't actually run until you require, then it runs in the context of whatever required it. You really never need one, but it's a great way to make your own APIs.

'1. What does return mean and how would you use it?'
Return returns the flow of the program back to right after the calling of the function. When you return a value, you are literally "giving" the value to whatever called you.
For example:
function name() return "cntkillme" end
local myName = name();
Here, myName would actually equal "cntkillme".

'2. What does declaring a local before a function mean, or is it the same as not declaring it before a function (Ex. local function onTouch())'
Well, in Lua all scopes have their own stack that holds the local variables (as opposed to other languages that only have a stack by functions). What this means is if you declare a local variable, only that scope can access it (also, the "local stack" of a scope can be accessed by sub-scopes, meaning if you have a scope in a scope, that inner-scope can access the local variables of the scope it is in).

'3. How would you use a coroutine correctly?'
Try to avoid coroutines at all cost, but if you must: setup some sort of system to control the flow between your coroutines (using coroutine.yield). Of course the wait function does that for you but in many cases, you can be more efficient by giving a coroutine control only right when it needs it.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
21 Dec 2014 01:08 AM
Think of module-scripts as copy-paste code, although it's not exactly this as they have their own environment and stuff.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image