User:Garretcat/Basic Scripting

From Legacy Roblox Wiki
Revision as of 19:30, 6 May 2012 by >Garretcat
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

All roblox scripts use the language Lua. Lua is a very basic language, designed to be learned easily. We will learn some of the basics now.

A Character Killing Loop

Inserting a Script

First of all, we will want to make a script, to write our code in. To insert a script, Insert>Object>Script

Opening the Script

To open a script, you should have the Studio explorer open. Under workspace, double click the new script. It will open up a new window where you can type.

Making a script

Lets create a basic code to kill your player every minute. "YOURPLAYER" will represent whatever your player's name is.

Defining

First, we need to tell the code where your player is, and define it's location. Lua uses a .(Period) to separate the 'tree' of the ROBLOX game. in all codes, 'game' is the top of the 'tree'. In explorer, you will see workspace, Lighting, Players, and other folders. Workspace houses all the physical aspects of the game, Including your character. So if we want to get to your character inside workspace, It would look like this:

Example
Defining your character
game.Workspace.YOURPLAYER 

Now we have your character. So if we wanted to kill your character, the simplest way is to remove your character's head. When your character's head is removed, your character dies. To remove something, we use the basic :Remove().

Example
Now we need to get to your head and remove it
 
game.Workspace.YOURPLAYER.Head:Remove() 

The Head:Remove() will remove 'Head" from the game, thus killing your character. Now that we have learned how to kill your character, we need to loop it.

Making a Loop

There are many types of loops, but for this particular code we will use a infinite loop; which is a 'while true do' to create a while true do, we need to simply declare it.

Example
While True Do
 
while true do 

that will begin a loop. Then it is advisable to put a wait, or a pause inbetween each loop, so it does not slow down the game. Since we wanted to kill the player every minute, we will use wait(60), which will wait for 60 seconds, or a minute.

Example
while true do
while true do
wait(60) 

now to finish the loop up, we need a 'end', to tell the code that we are done with our loop.

Example
while true do
 
while true do
wait(60)
end 
Putting it all Together

Now we need to put both of these little mini codes together. We need to have the code kill the player every minute. It would look something like this:

Example
Putting it all Together
 
while true do -- Declares the loop
game.Workspace.YOURPLAYER.Head:Remove() -- kills your player
wait(60) -- waits 60 seconds
end -- ends the code 

Wrapping up

That finishes the code, and inserted, will kill your player every minute, if you replace "YOURPLAYER" with your character's actual name.