Absolute beginner's guide to scripting: Difference between revisions

Gordonrox24 recommended to change "jediknightkrazy" to "USERNAME" or something like that. So I did it.
>JulienDethurens
>JulienDethurens
(Gordonrox24 recommended to change "jediknightkrazy" to "USERNAME" or something like that. So I did it.)
Line 39: Line 39:
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 jediknightkrazy's head.</code>
<code>Remove USERNAME's head.</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.
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 jediknightkrazy, find his Head, and remove it.</code>
<code>In game, find Workspace, find USERNAME, find his Head, and remove it.</code>


'''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.jediknightkrazy.Head</code>
<code>Delete game.Workspace.USERNAME.Head</code>


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.


<code lua>game.Workspace.jediknightkrazy.Head:Destroy()</code>
<code lua>game.Workspace.USERNAME.Head:Destroy()</code>


The above statement is '''syntactically correct''' Lua code. However, your name probably isn't jediknightkrazy, so substitute your name in. If your name was "AwesomeSocks", then you would use the following:
The above statement is '''syntactically correct''' Lua code. However, your name isn't USERNAME, so substitute your name in. If your name was "AwesomeSocks", then you would use the following:


<code lua>game.Workspace.AwesomeSocks.Head:Destroy()</code>
<code lua>game.Workspace.AwesomeSocks.Head:Destroy()</code>
Line 72: Line 72:
What about changing values of objects in ROBLOX Lua? There is a way to do that, and it's more of the same. To set your health to zero (rather than going "''Off with your head!''") you need to set the Health property of your humanoid to zero. To set properties of objects, you use a period ('''.'''), the property's name, an equals sign and the value.
What about changing values of objects in ROBLOX Lua? There is a way to do that, and it's more of the same. To set your health to zero (rather than going "''Off with your head!''") you need to set the Health property of your humanoid to zero. To set properties of objects, you use a period ('''.'''), the property's name, an equals sign and the value.


<code lua>game.Workspace.jediknightkrazy.Humanoid.Health = 0</code>
<code lua>game.Workspace.USERNAME.Humanoid.Health = 0</code>


You will learn specifics about what is what in ROBLOX Lua later (such as "''What does the equals sign really do?''"). For now, you can consider yourself a really basic scripter that can call methods and change properties's values.
You will learn specifics about what is what in ROBLOX Lua later (such as "''What does the equals sign really do?''"). For now, you can consider yourself a really basic scripter that can call methods and change properties's values.
Line 78: Line 78:
Protip: The Workspace is a special game service. Since it is used a lot in scripting, ROBLOX Lua has a built-in constant (just like '''game''') called '''Workspace''' rather than '''game.Workspace'''. You'll learn about constants and variables later, but know that the Workspace is the only object in ROBLOX Lua that has it's own built in constant. You can therefore do this:
Protip: The Workspace is a special game service. Since it is used a lot in scripting, ROBLOX Lua has a built-in constant (just like '''game''') called '''Workspace''' rather than '''game.Workspace'''. You'll learn about constants and variables later, but know that the Workspace is the only object in ROBLOX Lua that has it's own built in constant. You can therefore do this:


<code lua>Workspace.jediknightkrazy.Humanoid.Health = 0</code>
<code lua>Workspace.USERNAME.Humanoid.Health = 0</code>


==Inserting a Script object==
==Inserting a Script object==
Anonymous user