Main Search
Delta Corp Forum Index
 
 
FAQ Members Groups Profile Private Messages

Important Notice: We regret to inform you that our free phpBB forum hosting service will be discontinued by the end of June 30, 2024. If you wish to migrate to our paid hosting service, please contact [email protected].
Basic Scripting Tutorial by Kiddietron

 
Post new topic   Reply to topic    Delta Corp Forum Index -> Script Requests
View previous topic :: View next topic  

Did you think this was helpful?
Yes I learned a lot!
57%
 57%  [ 4 ]
Others will benefit from this.
42%
 42%  [ 3 ]
No it didn't help at all.
0%
 0%  [ 0 ]
Total Votes : 7

Author Message
kiddietron
Site Admin


Joined: 06 Feb 2008
Posts: 797
Location: On Roblox

PostPosted: Tue May 13, 2008 9:29 pm    Post subject: Basic Scripting Tutorial by Kiddietron Reply with quote

Hello everyone! Kiddietron Speaking/Typing! I see a lot of newbs nowadays on Roblox that don't know how to script. I was hoping with this tutorial, the Delta Corp members would learn scripting or become better scripters. Smile

Yes I did use some of Waffleboy's Tutorial because it filled in quite a few gaps my tutorial has.

--------------------[[What You Need!]]--------------------
-ROBLOX Studio
-Explorer Tab - This tells you exactly where everything is.
-Properties Tab - This tells you all the things editable in an object.
-Output - SCRIPTER'S BEST FRIEND. It tells you any mistakes found in a script, and where to find it.
-Command Prompt (Optional) - Not really needed, but is a great way to test out one-lined wonders.
-Any script; new or old (Optional) - If you want an example script to look at, you can always pick one out of the toolbox.
--------------------------------------------------------------------------

--------------------------------------------------------------------------
Table of Contents
--------------------------------------------------------------------------
1. Learning the basics of scripting
2. Basic syntax
3. If/Statements
4. Functions and Connections
5. Different methods
6. Harder Scripting
7. Conclusion
--------------------------------------------------------------------------



--------------------------------------------------------------------------
1. Learning the basics of Scripting
--------------------------------------------------------------------------
Now Scripting is a big part of Roblox. Roblox is a free environment designed to stimulate our young minds and give us a great creative freedom. It is difficult to script with no prior knowledge of scripting but you can learn from any level of programming experience.

Scripting is a bit different from programming. Scripting is merely modifying things within certain boundaries of Roblox while programming is actually creating/running your own programs.

Roblox was made with C++ I believe but we have only the power to script with Lua.

Now let's teach you the basics.
--------------------------------------------------------------------------

--------------------------------------------------------------------------
2.Basic Syntax
--------------------------------------------------------------------------
There are many different basic scripting methods in Roblox. Here I will list them.

To index an object in a script the start is usually like this.

To index the game you type "game"
To index the script you say "script" and to index the parent of the script is "script.Parent".
Then to access any of it's children you can use three different ways.

game.ObjectName --If it's one word and it exists
game["ObjectName"] --If it exists
game:findFirstChild("ObjectName")

From there you use game.Object.object or either of the three ways to get further in.

Some basic commands are
object:clone() --Creates a clone of the current object
object:remove() --Makes the object's parent nil
object:getChildren() --Makes a read-only table of all the children of the object
object:findFirstChild(str) --Finds a child of the object named str
object:MoveTo(Vector3.new(x,y,z)) --Moves the object to a certain Vector.new(x,y,z) --Creates a new Vector value that you can use
CFrame.new(x,y) --Position and offset I believe
object:getMass() --Finds the mass of the object
object:MakeJoints() --Makes a bond between all compatible parts
object:BreakJoints() --Breaks all the bonds of the object and other parts


Operations of Comparison
== Is the left side equal to the right side? "hello" == "goodbye" false
~= Is the left side not equal to the right side? "hello" ~= "goodbye" true
< Is the left side smaller than the right side? 3 <4> Is the left side larger than the right side? 3 > 4 false
<= Is the left side smaller than or equal to the right side? 3 <4>= Is the left side greater than or equal to the right side? 3 >= 4 false

Order of Operations; Script Style (VFC)
There is an order of which you should add your operations, just like in math. A mneumonic device for this order is "VFC", for "Variables, Functions, and Connections". A good script usually has itself in a neat and tidy order so that almost anyone can understand what the script does just by looking at it.

then some basic methods.

1. An if statement. This is a basic usage of scripting and is an early scripter's best friend.

Code:
if (condition) then -- if the condition matches then it executes code
execute code
end


This can also be manipulated into an if/else statement or an if/elseif statement.

Code:
if (condition) then
execute code
else -- if the condition wasn't met then it runs this
execute code
end


or

Code:
if (condition) then
execute code
elseif (condition) then -- if the first condition wasn't met, the second condition is checked
execute code
else -- if the previous conditions weren't met this code is run
execute code
end


2. A For Loop. This runs a certain code a set amount of times.

Code:
for i=1,10 do --repeats the loop if i=1 10 times
execute code
end


3. Wait(Increment). This is just a simple command to wait a certain number of seconds.

wait(Number of seconds)

4. A While Loop. This runs repeatedly until the condition is met or the condition is false

Code:
while true do -- runs while true is equal to true (always)
execute code
end


*Important* If you have a never-ending loop remember to put a wait() in there otherwise the game will crash from a overload.

5. End, Return. End is just written to close loops, statements, and functions. Return is used to return variables, objects, etc. to the calling function or code.

6. Functions. This is the Bread and Butter of the Scripter/Programmer/Coder's arsenal. You can use functions to almost an infinite extent to do nearly anything possible. But I will explain this a little bit later as you get more advanced.

7. The 'local' Statement. Trust me on this. Just use it. Whenever you're scripting and you can't make something a variable, like making a variable out of a function variable, you should use the term 'local'.

Code:
bin = script.Parent

function onTouched(part)
local h = part.Parent:FindFirstChild("Humanoid")
if h~=nil then
print("Hi!")
end
end

bin.Touched:connect(onTouched)


'Local' is just what it is, local. It is not a global statement like variables are, so it can be used in a whole different function, as long as it isn't stated already in the same function.

7. If I missed anything pm me and I'll add more. For built in objects of Roblox go into Roblox Studio, hit the Help tab and open up the Object Browser window.

--------------------------------------------------------------------------

--------------------------------------------------------------------------
3. Working with If/Statements
--------------------------------------------------------------------------
Try it out and do anything with it.

"An if statement tests its condition and executes its then-part or its else-part accordingly. The else-part is optional."[2]

Example One:
Code:
brick = game.Workspace.Brick
if brick~=nil then
print("Brick there")
end

First is the "brick" variable, followed by the "if" statement. The "if" statement is asking whether the "brick" is not "nil". "Nil" meaning nothing. This means that if the brick is there, it will print in the output, "The brick there!" On the next line, there's the 'end'.

Example Two:
Code:
number = 0
while number <10> 5 then
print("Number Greater Than 5!")
end
end

On the first line, "number" is 0. The next step is a 'while true do' loop. It waits one second then "number" will increase by one. Then there is the 'if' statement. It is now asking if "number" is greater than 5. If "number" is greater than 5, it will print, "Number Greater Than 5!" If it isn't greater than 5, then it will just skip it and repeat the loop. Once "number" turns to 11, the loop will end. This also goes with connection functions.

TIP: When using 'if' statements, use "==" and "~=". Otherwise, just use ">" and "<". In other occasions, you can use these, too. ">=" and "<=".

The 'else' Statement
Say you have an 'if' function, but it doesn't meet the requirements. That's when the 'else' kicks in. It does the function of the else onwards.

Example:
Code:
if game.Workspace == game.Lighting then
print("NOWAI!!!")
else
print("Whew. That was close.")
end

Well, there's the 'if' statement, which is impossible. Now, if it can't fulfill the requirement, it goes to the 'else' statement. So it does its function and prints to the output the statement in quotes. Then the end.

The 'elseif' Statement
'Elseif' is not an 'if' function. So what is the difference between 'elseif' and 'else if'? 'Elseif' is counted as an 'else' with a requirement for it to be run, and doesn't need an end. On the other hand, 'else if' is comprised of two statements: an else, and an if. Therefore, it needs a 'end' for the 'if' statement.

waffleboy = true

Example 1:
Code:
if waffleboy == false then
print("NOWAI!!")
elseif waffleboy == true then
print("YAWAI!!")
end


Example 2A:
Code:
if waffleboy == false then
print("NOWAI!!")
else if waffleboy == true then
print("YAWAI!!")
end
end


Example 2B:
Code:
if waffleboy == false then
print("NOWAI!!")
else
if waffleboy == true then
print("YAWAI!!")
end
end

'Elseif' doesn't need an end, but 'else if' does, because Example 2A and Example 2B are the same.

The 'end' Statement
Always remember to put this to end an 'if' statement. This can also end the 'while' functions and 'for' functions.


Code:
value = true

if value == true then
print ("You are correct!")
end


--Yes I used this from Waffleboy's Tutorial and some other parts.
--------------------------------------------------------------------------

--------------------------------------------------------------------------
4. Functions, Variables, and Connection Lines
--------------------------------------------------------------------------
Variables
Usually in a script, there is a specific order in which you put your operations. First off, one of the most important things to put in the very beginning are variables. Variables are used to note objects that will be used a lot. If you like to be organized, you like to put things in models. Sometimes, that model chain can get pretty lengthy.

Example:

example = game.Workspace.Example
Notice how the variable has a name, which is "example." The name can be anything except numbers alone. If you have a name of just numbers and you use it in a script, you will end up with a syntax error. When defining a variable, you need to tell exactly where it is. The Explorer tab helps with this as there is always a little tab telling you that that object is a child of something. When typing the parental of the object you want, spell the parental(s) correctly. You do not need the function ":FindFirstChild()" when making variables.

Functions
Secondly, in many scripts, you will come across a function. A function is something that can be used repeatedly. Some functions are also used to shorten script length, and some are just there because someone didn't want to delete it. When I create functions, I create the ones without a connection first, and then I create the ones with a connection line afterwards. If I do it like this, I can use the non-connections in the ones with connections. After all the things inside, you always close a function with an "end" statement.

Example:

Code:
bin = script.Parent

function anchor(object)
object.Anchored = true
end

function onTouched(part)
anchor(part)
end

bin.Touched:connect(onTouched)

There is the one variable, not really needed but I need to stress its importance. In the first function, as in every function, the term "function" is first. It is correct if the term turns purple. After the term "function," the function needs to be named (in the example, there are two -- named "anchor" and "onTouched".) Do not use numbers as a name. It is better to spell out numbers in names, except in some occasions. Now, after the name, you'll see the word "object" in parentheses. You can rename it anything you want, except numbers, and you can use it for anything you want. It's like a function variable, you can use it to do other things and use it repeatedly with different terms. You can also add more variables to this by using commas. So, this example could have been made: "function anchor(object, statement)". In this function, though, it is anchoring the object no matter what. Finally, the "end" statement.

In the second function, the function has a connection line. It's the same as the first function, the "function" statement, the function name, and the variable. The difference is, the previous function anchors the so-called "part." The variable is defined as "part," which is in this case, the object being touched. Finally, the "end" statement as always.
--------------------------------------------------------------------------

--------------------------------------------------------------------------
5. Different Methods (Methodizing)
--------------------------------------------------------------------------
When you script it's advised to do different things to reach your goal. It twists your mind and helps you become a better thinker and scripter. For example,

Code:
damage = 10
debounce = 0
function touched(part)
if debounce == 1 then return end
debounce = 1
local figure = part.Parent
if figure:findFirstChild("Humanoid") ~= nil then
  figure["Humanoid"]:TakeDamage(damage)
end
wait(2)
debounce = 0
end

script.Parent.Touched:connect(touched)


Now as an alternative you could do something different.

Code:
debounce = false
script.Parent.Touched:connect(function(part) if debounce == false then debounce = false if part.Parent:findFirstChild("Humanoid") ~= nil then part.Parent["Humanoid"]:TakeDamage(10) end end wait(2) debounce = true end)


Virtually the same thing but it's a bit more advanced running a script directly from the connection. Going about looking for several methods to accomplish one thing can be very helpful when looking for a way to solve a problem.
--------------------------------------------------------------------------

--------------------------------------------------------------------------
6. Harder Scripting
--------------------------------------------------------------------------
Assuming you're a master at the basics, you should be ready to take on some of the big stuff. Try new projects and don't worry about failing. Work on scripts that may exist but script it on your own.

Once you get really good do some original scripting.
--------------------------------------------------------------------------

--------------------------------------------------------------------------
7. Conclusion
--------------------------------------------------------------------------
That's all I have to say. I started from the beginning too. I'm a pretty good scripter and I'm passing most of the basics I know. If you follow most of this stuff that I came up with and Waffleboy as well you should get pretty good with a bunch of practice. No matter what you do you won't be a master over night so work hard to become better.

Remember if you ever need help on some other things, just post on this forum. I mean after all it's a scripting requests forum. Smile

Thanks for reading.
_________________
�Kiddietron
�Phailing at making a signature

Free Money here just sign up and click away.
http://bux.to/?r=kiddietron

Spam is for noobs.



Last edited by kiddietron on Sun Jun 01, 2008 9:59 pm; edited 5 times in total
Back to top
View user's profile Send private message Send e-mail AIM Address
Nasadaws
Delta Corp Moderator


Joined: 27 Mar 2008
Posts: 484
Location: Under you bed Where else?

PostPosted: Wed May 14, 2008 7:58 am    Post subject: Reply with quote

You should add things like (hit.Parent.Name) and Mouse.Move.


I want to learn them, Plus they are really useful.


Plus a section on how to read Output.
_________________
Nasadaws Attack Spammer: Hits 100 Damage with his Ban Hama.
Spammer: Flees.


I R NOE WELDIN ME BAN HAMAR.
Back to top
View user's profile Send private message Send e-mail
LuigiFan
Beginner Poster


Joined: 26 May 2008
Posts: 11

PostPosted: Sun Jun 01, 2008 9:34 pm    Post subject: Reply with quote

He's teaching the basics. If they want to know stuff like that they can ask through a forum. Or someone could create a HopperBin tutorial of some sorts...
_________________


http://www.roblox.com/User.aspx?id=31132
Back to top
View user's profile Send private message Send e-mail
Rhadamanthus
Newbie


Joined: 08 Jul 2008
Posts: 4
Location: In Hades judging the Asian souls

PostPosted: Tue Jul 08, 2008 11:02 pm    Post subject: Reply with quote

That's a huge tutorial, I haven't read it all but I think I'll get really good at Lua by learning the basics first. Thanks.
_________________
Foul cretins, I banish thee to the depths of Tartarus.

Good souls, rest in peace in the Elysian Fields.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Delta Corp Forum Index -> Script Requests All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Back to Top              
Jenova Template © digital-delusion.com
Powered by phpBB © 2001, 2002 phpBB Group



For Support - http://forums.BizHat.com

Free Web Hosting | Free Forum Hosting | FlashWebHost.com | Image Hosting | Photo Gallery | FreeMarriage.com

Powered by PhpBBweb.com, setup your forum now!