Paying bits for a script teacher
#11
Code:
do dis pls
hi u ok game // game should output current feelings
is game ok? if yes do dis
tell game: yh thats good me ok to
if no do dis
tell game: game y u no ok:(
done doing dis
[Image: KbMTk.png][Image: uC9A1.png]
Reply
#12
(09-04-2012, 09:26 PM)Duck Wrote: yes but if you define something you are going to use lots of times in the same script it cuts down on loads of writing
u rnt qwerty
@Qwerty
Oh, I see.
It was just the first thing I experienced, and I usually stick with that.
~Invader~
Reply
#13
Being able to change things with scripts is nice, but it's only half the fun of scripting. The second, somewhat more important half is changing things dynamically, with functions, events, and loops. I'll start with the most important.

Functions have two major uses: running the same bit of code multiple times, and running a bit of code at some other time than right at the first time the script is run.

Like variables, you need to give your functions a name. However, they're a bit more complicated to define.

A basic function will look something like this:

[lua]
function namehere()
--code
end
[/lua]

Here's an example of using a function to do something:

[lua]
local circle = create("Circle")
circle.Parent = game.World
circle.Position = Vec2D(50, 50)

function red()
circle.Color = Color(255, 0, 0)
end

function green()
circle.Color = Color(0, 255, 0)
end

function blue()
circle.Color = Color(0, 0, 255)
end

red()
sleep(2) --this important little function tells the script to pause X seconds before continuing; in this case, 2
green()
sleep(2)
blue()
[/lua]

Notice how similar the three functions are? We can make things more efficient by using something called "parameters". These are extra variables you put between the parenthesis.

[lua]
local circle = create("Circle")
circle.Parent = game.World
circle.Position = Vec2D(50, 50)
local box = create("Box")
box.Parent = game.World
box.Position = Vec2D(55, 55)

function colorize(part, thecolor) --"part" and "thecolor" are our parameters
part.Color = thecolor
end

colorize(box, Color(255, 0, 0))
--variable "box" gets sent to the function as parameter "part",
--while "Color(255, 0, 0)" gets sent to the function as parameter "thecolor"
colorize(circle, Color(100, 100, 100))
[/lua]
Reply
#14
I see.
~Invader~
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)