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

Confused About the Spawn Function?

Asked by 8 years ago

I am confused about the spawn function.

spawn(function()
end)

Can someone please explain what it is/does? Thanks!

1 answer

Log in to vote
0
Answered by 8 years ago

There are two kinds of functions.

1) The first are predetermined ones, that are part of an object and do edit that object. You can't change, add, or delete pre-difined functions, but there the basis for triggering certain events in the game (not to be mixed up with connection lines --> http://wiki.roblox.com/index.php?title=Scripting_Book/Chapter_2#Calling_a_function).

Example:

Part = game.Workspace.Part
Part:Destroy() --this deletes the part from the workspace (aka setts everything to nil)

2) The 2nd type are the ones you can make yourself. Now these functions don't already do specific things, but they are instead like a folder that holds specific lines of code. A function has three parts to it, one the code function, then the name you want to name that function (e.x. testing), and finally the function variable (optional ; more on these here ---> http://wiki.roblox.com/index.php?title=Function#Using_Arguments_and_Parameters)

Example:

function testing()
The cool things about this is that you can call the function (run the function ; activate it) any time and anywhere in the script, and when it's called it also calls runs all the lines of code inside of that function. The process of calling a function, is called a connection line. There are two ways a connection can be activated, one by typing first typing the the function name, then putting to enclosing parentheses next to it (e.x. `testing()` ). 

The second way of calling a function is to add in events. Events are certain actions that happen to an object (e.x. a part gets Touched by another part). So there are three things we must include in the connection line for it to work. One, the object that you want analize, two the event of that object that your watching for, and three the function that you are calling.

Example:

function testing() --this is the function
    --Do stuff
end --you must add an end to close off that function
game.Workspace.Part.Touched:connect(testing) --the Part is the object, .Touched is the event, and testing is the function were calling
0
BTW the first "example" I gave is not supposed to include the next paragraph in code block form. In fact, it doesn't even appear in code block form on the editor. If encounter the same issue, please contact an admin so they can fix the bug. LateralLace 297 — 8y
0
Question, what you said, what does it have to do with the Spawn function? Thundermaker300 554 — 8y
Ad

Answer this question