Absolute beginner's guide to scripting: Difference between revisions

Fixing
(Improving code and using SyntaxHighlight)
(Fixing)
 
(2 intermediate revisions by 2 users not shown)
Line 51: Line 51:
Here's our starting command. It's what we want to happen, and it has been written in plain English.
Here's our starting command. It's what we want to happen, and it has been written in plain English.


<code>Remove USERNAME's head.</code>
<pre>Remove USERNAME's head.</pre>


Naturally, ROBLOX will not like that very much because it's in English! You have to figure out how to express what you want to happen in Lua code.
Naturally, ROBLOX will not like that very much because it's in English! You have to figure out how to express what you want to happen in Lua code.


<code>In game, find Workspace, find USERNAME, find his Head, and remove it.</code>
<pre>In game, find Workspace, find USERNAME, find his Head, and remove it.</pre>


'''game''' is the root of all things in your game. We need to "go down" the family tree and find the right brick to remove. To get an object that is a child of another object this, you use a period ('''.''') after the parent object, then the child object's name. Going through the family tree, we get this:
'''game''' is the root of all things in your game. We need to "go down" the family tree and find the right brick to remove. To get an object that is a child of another object this, you use a period ('''.''') after the parent object, then the child object's name. Going through the family tree, we get this:


<code>Delete game.Workspace.USERNAME.Head</code>
<pre>Delete game.Workspace.USERNAME.Head</pre>


This is almost a working ROBLOX Lua statement! Now, to actually remove the head, a '''method''' must be used. Methods are functions that do specific things in ROBLOX for you by '''calling''' them. Methods are functions of objects in ROBLOX Lua. To call a method on an object in Lua, you use a colon (''':''') after the object. You then state the name of the method, then two parenthesis with nothing in them. Methods and functions take '''arguments''', which are values given to the function to change how it works. The "Destroy" method takes no arguments, so we place the parenthesis there to show that there's nothing else we have to give to the Destroy method.
This is almost a working ROBLOX Lua statement! Now, to actually remove the head, a '''method''' must be used. Methods are functions that do specific things in ROBLOX for you by '''calling''' them. Methods are functions of objects in ROBLOX Lua. To call a method on an object in Lua, you use a colon (''':''') after the object. You then state the name of the method, then two parenthesis with nothing in them. Methods and functions take '''arguments''', which are values given to the function to change how it works. The "Destroy" method takes no arguments, so we place the parenthesis there to show that there's nothing else we have to give to the Destroy method.
Line 209: Line 209:
While scripting, you may want to make your own function to call later. This is easy enough, here is the syntax (grammar) for doing so: (NOTE: These functions are called ''without'' a colon [:])
While scripting, you may want to make your own function to call later. This is easy enough, here is the syntax (grammar) for doing so: (NOTE: These functions are called ''without'' a colon [:])
<pre>
<pre>
function <functionname>(<parameter>)
local function <functionname>(<parameter>)
     <statements>
     <statements>
end
end
Line 222: Line 222:
Functions can also return a value, which means they can be used instead of a constant (like a number) or a variable (covered below):
Functions can also return a value, which means they can be used instead of a constant (like a number) or a variable (covered below):
<pre>
<pre>
function <functionname>(<parameters>)
local function <functionname>(<parameters>)
     <statements>
     <statements>