scripting..?

Go down

scripting..?

Post  Blaze1500 on Mon Oct 27, 2008 3:01 am

how do u script?
avatar
Blaze1500

Posts : 59
Join date : 2008-10-27
Location : Pwnada, Japwn

View user profile http://combatarms.nexon.net

Back to top Go down

are you kidding

Post  Redrafter on Mon Oct 27, 2008 3:03 am

[content pwned]
avatar
Redrafter
Admin

Posts : 88
Join date : 2008-10-26
Location : fantasy dream,pwnada

View user profile http://TOP.igg.com

Back to top Go down

Re: scripting..?

Post  Blaze1500 on Mon Oct 27, 2008 3:06 am

lol
avatar
Blaze1500

Posts : 59
Join date : 2008-10-27
Location : Pwnada, Japwn

View user profile http://combatarms.nexon.net

Back to top Go down

Re: scripting..?

Post  Redrafter on Mon Oct 27, 2008 3:37 am

Introduction
This tutorial shows how to do some basic scripting. It explains how to script in the simplest terms possible, and is designed for people with little or no experience with scripting.

[edit]What is scripting?
Scripting is a way to tell the game what to do. However, computers can only understand commands if you tell them exactly what to do, in a specific code. ROBLOX Scripts are commands in a coding language called Lua. Lua is not-so-hard to understand, depending on the way you want to use it.

[edit]Your first script
Already you're about to make your very first script. We'll be executing the script from a place in ROBLOX Studio called the Command Bar, which allows you to execute a single line of script instantly.

[edit]Being specific
Let's make the most basic script: a suicide script. When it executes, it kills you. Scripting is just giving commands to the computer.

How do you kill a player? There are two ways to kill a character: disconnect their Head from their Torso, or have their Humanoid's health dropped to zero. Don't worry what a Humanoid is, we'll use the first method for right now.

The game doesn't know whom to kill, until you specify the username of the person whom you want to kill. Think of the structure of a ROBLOX game as a tree. The trunk is called "game". Then there is a branch, called "Workspace". Think of all the bricks as branches on that "Workspace" branch. (Bricks, by the way, are not stored by their position in the game. They are organized like leaves on the branches of this tree.) You are going to be a branch, called by your username, on the "Workspace" branch, on the "game" trunk.



Note that both the Players and the Workspace branch has a branch named after a specific user "jediknightkrazy". That's because there are two branches named after you, your player, and your character.

[edit]Translating to Lua
A script needs to be in a special coding language called Lua. This section will walk you through translating a basic English command to Lua.


Here's our starting command:

Delete jediknightkrazy's headNaturally, ROBLOX won't like that very much. Remember the tree from the last section? First we have to tell ROBLOX where to find "jediknightkrazy"

In the game, in the Workspace, there is jediknightkrazy's HeadIn Lua, you go down the tree like this by using a dot: (.)

delete game.Workspace.jediknightkrazy.HeadThis is almost a working statement. Now, to actually delete the head, a function must be used. To use a function, just type a colon (Smile after the object you want to execute it on (in this case, Head), and then the function you want to use, which in this case, is "Remove". Then, put a pair of parentheses after the function name.

game.Workspace.jediknightkrazy.Head:Remove()If your name isn't jediknightkrazy, substitute your name for mine:

game.Workspace.SomeGuy.Head:Remove()To experiment a bit, try deleting the Torso instead.

[edit]Testing the script
Open ROBLOX Studio and click File > New. Add a baseplate and a spawn.

To add a baseplate: Click Insert in the game window, NOT in the menu. Select "Free Models" in the drop down list at the right of the screen. Type "baseplate" and click Search. Add one that looks good.

To add a spawn: Select "Game Objects" in the drop-down list and click on a grey (neutral) spawn. + Now, in the menu, click Tools > Test > Visit Solo. This is where you can test your places without uploading them! + Once it loads, right click in the Toolbar and check Command if it isn't checked already. A small textbox bar labelled "Command" should appear at bottom of the screen. In there, type: +

game.Workspace.Player.Head:Remove()Don't replace Player with your username in Test mode, because your name is always "Player" in Test mode. Type it, don't just copy and paste! Hit enter. If your head vanishes and you die, you've done it! You've made your first script. Now, try your Torso. Maybe remove the Baseplate; it's probably called "Base". (HINT: It's not in Player)

[edit]Setting values and making objects
What about actually changing the value of an object, as opposed to removing an object? Earlier it was mentioned that there were two ways to kill someone. The more complicated way is setting one's Health to 0. The Health is contained in a Humanoid object, which is contained within the character (Player.Humanoid.Health).

Example:

game.Workspace.Player.Humanoid.HealthHowever, what about setting a value? In Lua, you set a value with the = sign. Put what you want to set on the left, and what you want to set it to on the right:

game.Workspace.Player.Humanoid.Health = 0Type it in the command bar like before, and you should be able to kill yourself without beheading yourself.

[edit]Getting a script in your place
To make a script in ROBLOX Studio, just click Insert>Object...>Script, then you've got a script you can edit in your own place. To learn how to make useful scripts, I recommend trying to read an existing script, like a Reset Tool or a deadly block. Try modifying it to do different things.

[edit]Adding a script
Remember to use ROBLOX Studio, NOT "build", or "solo" to edit your scripts. Just click on Insert > Object, then double-click on Script. [TODO: Add picture] This script will execute as soon as your place loads, so none of the killing scripts from last tutorial are going to work in your place.

IMPORTANT: When a script encounters an error, it stops running, or breaks. So, if only half of your script gets run, there's a good chance that there's a problem somewhere in the middle.

[edit]Editing a script in ROBLOX Studio
To edit a script, find it in the Explorer window:


and double-click on it. Now it should open up a text editor that says
print("Hello World")"What does 'print()' do?", you ask. Print() simply prints that line into the Output Window. To show it, click View > Output. Click the Play button in toolbar and look in the Output Window.

[edit]Testing your script
To test open the Test mode as described before. This time, don't type anything in the Command bar. They should run automatically.

[edit]Script Creation
There are 5 topics that will be answered here:

Creating the Script
Tagging Objects
The Listening Event
The Function
Modifying Objects/Tags
NOTE: All work, building and scripting, should be done in Edit Mode. All these lessons will be shown as if you are in Edit Mode. To open Edit Mode, on your desktop screen, hit Start>Programs>ROBLOX>ROBLOX Studio. Then go to your profile, then click "Edit".

[edit]Creating the Script
What work to be done when you don't have the project? To get a script go in Roblox Studio or Solo Mode then simply select Insert>Object. Now a window will show up. In the box click on the script object. You should find the script inside"Workspace" in the explorer tab. If you don't see any explorer window up, go to View>Explorer.

Now to open the script, just double-click it. If you did it right, a window will cover the whole ingame screen, and the browser should look a bit more like Microsoft Word. And you will find the line "print("Hello World!")" Before you start, just go ahead and delete that line.

[edit]Tagging Objects
You got a nice script up, but what good is a script if it doesn't have anything to play with? Assuming the script is still under Workspace, that is where your script will run under. Let's say you want a brick turning invisible/visible, back and forth when touched. The script needs to know where that brick is before modifying it.

Now, tagging objects are not necessary, but it can make scripting a lot less work. Here's an example of tagging objects:

brick = game.Workspace.BrickThat will tag the brick under the name you assigned. You can set the name to absolutely anything. You can have as many tags as you want.

If you didn't tag it, every time you try making the script modify the object (explained in lesson 5), you would have to put the line "game.Workspace.Brick" every single time. Tags are much simpler, since you'd only have to put the name you assigned. Name the brick desired to "Brick", and tag it in the script by typing the example above. Note that tagging is the same as assigning a variable.

[edit]The Listening Event
Now we're getting into the meat of scripting. Sure, the script knows where the brick is, but that's all. I can't do anything else. Now we're jumping into a listening event.

What is a listening event? It's the trigger of the script. This is going to tell the script to do something if the listener finds the trigger fired. This is one important part of the script, otherwise you couldn't really make scripts wait for anything.

You still should have the script with the tag in it. We're going to make the script listen for being touched. Here's an example:

brick.Touched:connect(onTouch)If the brick is Touched, It will connect the (function). The function is explained next lesson. Keep in mind that the name inside the parentheses is the name of the function. You can name this anything, however, I recommend doing the above for this lesson before you jump into any other names.

This is not the only listener type. There are many more to use, some of which require some familiarity with scripting. Here is a very well-done reference page set up by MrDoomBringer.

This is where you can find more help in the future, when you begin to understand scripting more. Not only does it show Events, but also shows other scripting references need for other aspects of scripting (some of which is explained in lesson 5).

So for this lesson, put the line "brick.Touched:connect(onTouch)" a line or two below the tag from last lesson.

[edit]The Function
Your script is getting better and better, but where is the function? Your script will break if it doesn't have one of those for the listener to refer the script to.

What is a function? It is where all your modifying work will be done. It is also an important part to your scripting. Without it, you could not make the script modify objects from listeners. Another example:

function onTouch(part)

end
There is the function. As you can see, the function has "onTouch". The listener from last lesson is trying to refer to the function. The listener is going to tell the script to run through this function and do whatever is found inside (this will be explained next lesson). Notice after "onTouch". This is the tag of the object that the listener found that touched the brick. This is not always needed, especially for different listeners. Most times, with other listening types unlike touching listeners, you would just place this:

function onTouch()

end
But back to what we're looking at. The tag is the object that touched the brick. You can play with this object for fun later. Now notice also two lines below the function: "end". You will need one of these for every function and other aspect of scripting, such as "if" statements. Always remember this when scripting.

Now, make the lines from first example, except put it two lines under the tag, and one line above the listener.

[edit]Modifying Objects/Tags
The script knows the brick, will wait until it's touched, and has the function to use. But it doesn't know what to do to the brick.

This is where tagging objects saves you time. We wanted it to flicker invisible/visible, so here's an example:

brick.Transparency = 1
wait(1)
brick.Transparency = 0
These lines will alter the brick as we wanted. The brick's transparency is changed to "1", which is completely invisible. The "wait(1)" line will make the script wait for one second before continuing, then the brick's transparency will be put back at 0, which is completely visible. You can alter "wait(1)" to any number inside the parentheses. Whatever number you put inside the parenthesis will be the amount of time it will wait in seconds.

Put those 3 lines right under the line "function onTouch(part)" and above the line "end".

[edit]Complete script
brick = game.Workspace.Brick --tagging the brick

function onTouch(part) --the function

brick.Transparency = 1 --what the function is to do with the brick
wait(1)
brick.Transparency = 0
end

brick.Touched:connect(onTouch) --listening event



[edit]Advanced scripting techniques
Now that you can put scripts in your place, now they have to do something useful.

[edit]Make your own functions
While scripting, you may want to make your own function to call later. This is easy enough, here is the syntax (grammer) for doing so: (NOTE: These functions are called without a colon [:])

function <functionname>(<args>)
<statements>
end

<functionname>(<arguments>)

An argument is a way to give data to a function. An example of an argument: a Humanoid has a takeDamage() function. You have to tell it how much damage to take. You would type Humanoid:takeDamage(100) to take 100 damage. (NOTE: the takeDamage function will not take damage if the humanoid has a ForceField. Use this function for weapons instead of directly setting the health, to prevent spawnkillers)

Functions can also return a value, which means they can be used instead of a constant (like a number) or a variable (covered below):

function <functionname>(<args>)
<statements>
return <valueobtainedfromstatements>
end

<somevariable> = <functionname>(<args>)

Copy and paste these codes into a script for a better example:

Example 1

function sayHello(name)
print("Hello, " .. name .. "!")
end

sayHello("Bob")
Example 2

function addNumbers(a,b)
ans = a + b
return ans
end

answer = addNumbers(1,2)
print(answer) --> Prints in the output 3

Combining Strings Note the ".." in Example 1. That means to squish two pieces of text (also called strings) together. So "Hello" .. "World" would be "HelloWorld" (note the lack of a space), and "Hello " .. "World" would be "Hello World". Also note that a function must be written before it is called. If you wrote "sayHello("Bob")" before "function sayHello(name)", your script would break.


Functions that return values Also note the return statement in Example 2. The return statement automatically ends the function at that line, and then gives the value to the variable on the LEFT SIDE of the equals sign. Let's look at that again, this time with detailed comments:

--Define a function called addNumbers with the arguments "a" and "b"
function addNumbers(a,b)
--make a variable called "ans", and set it to the sum of a and b.
ans = a + b
--Return the variable called ans. This ends the function.
return ans
end

--Set a variable called "answer" to the return value (ans) of addNumbers, with the arguments 1 and 2.
answer = addNumbers(1,2)
print(answer) -- Prints in the output 3

[edit]Flow control
Flow control basically means doing different things depending on the situation. There are two main ways to do it in Lua, both involve conditions.

[edit]Conditions
A condition is a situation. A simple condition is this:

1 == 2(Note the "==". Always use that, and not "=". If you don't... well, it won't be pretty.) Of course 1 isn't 2! So that would be false.

1 < 2That would be true, because 1 is less than 2.

[edit]If statements
The if statement does something only if a condition is true. Syntax:

if (<condition>) then
<statements>
end
Example:

if (2 + 2 == 4) then
print("All is right in the world")
end
That would always print "All is right in the world", because 2 + 2 is always equal to 4.
avatar
Redrafter
Admin

Posts : 88
Join date : 2008-10-26
Location : fantasy dream,pwnada

View user profile http://TOP.igg.com

Back to top Go down

O_O

Post  RoboFreak on Mon Oct 27, 2008 4:27 am

YOU CALL THAT SIMPLE??????????



affraid

_________________
.>@^%R0b0Fre@k%^@>.
avatar
RoboFreak

Posts : 30
Join date : 2008-10-26
Location : the black hole,dream land,pwnada

View user profile http://job889.sosblog.com/The-first-blog-b1/Welcome-b1-p1.htm#co

Back to top Go down

Re: scripting..?

Post  Blaze1500 on Mon Oct 27, 2008 5:51 am

AAAAAH! LONG WORDS! HELP!
PS I dont read long words

lol!
avatar
Blaze1500

Posts : 59
Join date : 2008-10-27
Location : Pwnada, Japwn

View user profile http://combatarms.nexon.net

Back to top Go down

AEEEEEEEEEEH

Post  RoboFreak on Mon Oct 27, 2008 7:23 am

LEONG WERDZ BIE EVEL

_________________
.>@^%R0b0Fre@k%^@>.
avatar
RoboFreak

Posts : 30
Join date : 2008-10-26
Location : the black hole,dream land,pwnada

View user profile http://job889.sosblog.com/The-first-blog-b1/Welcome-b1-p1.htm#co

Back to top Go down

Re: scripting..?

Post  Redrafter on Mon Oct 27, 2008 8:41 am

why dont you read? thats how you script!!!
Code:
local hole = script.Parent
local childList = {}

local massConstant = 5.8 -- Generally a good value

local mass = 32000 * massConstant

-- This is basically a function that finds all unanchored parts and adds them to childList.
-- Note: This should only be run once for each object
function checkObject(obj)
   if (obj ~= hole) and (obj.className == "Part") then
      if (obj.Anchored == false) then
         table.insert(childList, 1, obj)
      end
   elseif (obj.className == "Model") or (obj.className == "Hat") or (obj.className == "Tool") or (obj == workspace) then
      local child = obj:GetChildren()
      for x = 1, #child do
         checkObject(child[x])
      end
      obj.ChildAdded:connect(checkObject)
   end
end

checkObject(workspace)

print("Black Hole script loaded.")

local n = 0
while true do
   if n < #childList then
      n = n + 1
      if n % 800 == 0 then
         wait()
      end
   else
      n = 1
      wait()
   end

   local child = childList[n]
   if (child ~= hole) and (child.className == "Part") and (child.Anchored == false) then
      local relPos = hole.Position - child.Position
      local motivator = child:FindFirstChild("BlackHole Influence")
      if relPos.magnitude * 240 * massConstant < mass then
         child:BreakJoints()
         if (relPos.magnitude * 320 * massConstant < mass) and (child.Size.z + hole.Size.x >  relPos.magnitude * 2 - 4) then
            mass = mass + child:GetMass()
            child:Remove()
            table.remove(childList, n)
            n = n - 1 -- This is the reason I need a counter of my own design
         else
            child.CanCollide = false -- I Can assume that things won't escape the black hole.
            if motivator == nil then
               motivator = Instance.new("BodyPosition")
               motivator.Parent = child
               motivator.Name = "BlackHole Influence"
            end
            motivator.position = hole.Position
            motivator.maxForce = Vector3.new(1, 1, 1) * mass * child:GetMass() / (relPos.magnitude * massConstant)
         end
      elseif motivator ~= nil then
         motivator:Remove()
      end
   end
end
yeah this is how to make a black hole
avatar
Redrafter
Admin

Posts : 88
Join date : 2008-10-26
Location : fantasy dream,pwnada

View user profile http://TOP.igg.com

Back to top Go down

Re: scripting..?

Post  Redrafter on Mon Oct 27, 2008 8:42 am

copy it put a script in a brick and put this on it
Code:
 take out the hello world! (00345)
avatar
Redrafter
Admin

Posts : 88
Join date : 2008-10-26
Location : fantasy dream,pwnada

View user profile http://TOP.igg.com

Back to top Go down

Re: scripting..?

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum