How to Script Anything


Welcome to How to Script Anything! This miniature scripting "book" will teach you:
-Scripting in general (variables, syntax, etc.)
-The basic commands and details
-Advanced commands
-Logical thinking (and planning)
-How to script almost anything you want
Preliminary knowledge: How to navigate through Roblox; the basics of playing in Roblox.
You will need to do independent research on the links provided.
NOTE: Feel free to request that more be added to this page by PMing chess123mate. I am writing this page because a lot of people want to know how to script, but are confused by other wiki articles (there are many!). Hopefully this will help by laying down the basics and getting more advanced slowly. Much of the information can be found on other websites, this isn't exactly new.
Also, feel free to add in more advanced scripting techniques if you are experienced in them, or format this page better, or clarify something that doesn't make sense (or request clarification).

Be warned...[edit]

You must be aware that scripting is not easy to get good at. Read as much in the wiki as you can, test it out in the Command bar and output, and try simple scripts to start with. Believe it or not, but tools that allow cars and airplanes to move are very advanced.
It may take anywhere from weeks to years to get really good at scripting. However, this skill is very valuable in making pretty much anything you dream. That's what Roblox is all about.
It is a good idea to try editing advanced scripts and editing parts of it. Edit one piece at a time, cut and paste the script from the Explorer Window in Roblox Studio (this restarts the script and is an important debugging technique). After reading this entire page and other wiki pages, you should have a general idea of what it does anyway, but sometimes it is difficult to figure out what another scripter has done (or is trying to do).


Chapter 1: Scripting Basics[edit]

On Roblox, scripting can help you make things happen. Without it, nothing would occur. Scripts allow your character to move and blox others, regen things, making things fly (potentially on their own), and much more. However, it takes many precise commands for it to all work. Without precise commands, Roblox may end up doing something you didn't want it to do.

However, the skill of choosing the correct commands and putting them in the correct order is not easily learned. It will take hard work, time, and a lot of practice. If you don't get something the first time, you will need to continue to search for errors, bugs, syntax errors, and continue trying. However, the reward of this is great, for you will be able to script almost anything.

Chapter 2: Flow Control[edit]

Especially in Roblox, if there was no way of controlling what happens, there would be very little that could be done. One way of controlling the "flow", or what the script executes next, is by using events (in Chapter 3).

Chapter 3: Functions, Data Types, and Coroutines[edit]

A closer look at making your own functions is necessary before any significant scripting can be done. Events, data types, coroutines all require functions, and very few scripts can do anything on Roblox unless they have at least one function.

Chapter 4: How to Interact with Roblox[edit]

Most of the basic features of Lua have been introduced by now. However, few of the scripts have done anything useful in Roblox.

Chapter 5: How to Script Anything[edit]

In this chapter, an example will be used on how you might apply all of the above concepts in a variety of contexts. Two examples will be looked at simultaneously. The first is if you had ten doors and wanted them to open or close randomly every time someone touches the door. Instead of using ten separate scripts, one script will be used for all of them to save time if the script needs to be edited and processing time, since less scripts will need to be transmitted to each player using the place on load-up. The second thing is much more complicated: Make a brick fly and attack players.

Solutions to Practice Questions[edit]

Remember that your solutions to these questions is likely to be slightly different than the ones presented here, and that's perfectly okay. However, even if they are very different, your answer may be just fine or even better than the ones here.

Chapter 1[edit]

  • myVar = 10
  • myVar2 = 5

myVar = myVar * myVar2
print("myVar's value: " .. myVar)

  • print(1==3) --will print false

print(8<8) --will print false
print(8<9) --will print true
print(8<8 or 0>-5) --will print true
print(8<8 and 0>-5) --will print false
print(true and true) --will print true

  • 1+1*2 == 3 and 3*3==9

Simplifies to: 1+2 == 3 and 3*3==9
3 == 3 and 9==9
true and true

Chapter 2[edit]

  • for i = 1, 20 do

if i % 2 == 0 then
print(i .. " even")
print(i .. " odd")

  • var1 = 5 --editable

var2=11 --editable
if var1*2>var2 then
print("Two times var1 is greater than var2.")
elseif var1*2==var2 then
print("Two times var1 equals var2.")
print("Two times var1 is less than var2.")

  • for i = 3, 13 do

if i~=5 and i~=8 then

  • for i = 5, 1, -0.2 do print(i) end
  • min1=1

notMatch=0 --Note that you could also calculate this by subtracting "match" from "trials", making the script more efficient since the script wouldn't have to run as many lines.
for i = 1, trials do
if math.random(min1, max1)==math.random(min2,max2) then
print("Number of times they matched: " .. match)
print("Number of times they didn't match: " .. notMatch) --or, if notMatch was eliminated, it would be ..."didn't match: " .. (trials-match)).

Chapter 3[edit]

  • function printOut()

for j = 1, 5 do
--Alternate method; use: delay(0, printOut)
for i = 1, 4 do

Alternatively, the printOut function could be run indefinitely and then the main coroutine would stop it using coroutine.yield(co), but it would have had to declare co earlier on.

  • function recursionSum(num)

if num%2==1 then max=max-1 end --make "num" an even number
if num>1 then
return num+recursionSum(num-2)
return num

print(recursionSum(10)) --will print out 30

  • TODO Finish classes

Chapter 4[edit]

  • db=false

function onTouch(obj) --assumes that the door starts out with CanCollide==true
if db then return end
for i = 1, 5, 0.1 do

  • Note: There are many ways of writing this script.
open=false --actual state of trapDoor
id=0 --touch id. Incremented every time the trapdoor is opened to ensure that the "check" function does not close the door if the door was closed and opened within the 30 seconds.
function check(value)
    if value==id and open then
function closeDoor()"Bright green")
    db=false --only make db false here to ensure that 'closeDoor' isn't called with a 1 second delay and the button is touched within that second.
function onDoorTouch(obj)
    if not open then return end --already closed
    if obj.Parent==nil then return end --a weapon touched the door
    if game.Players:playerFromCharacter(obj.Parent)==nil then return end --not a player
    delay(1, closeDoor) --provide time for the person to get through
function onButtonTouch(obj)
    if open then return end --trapdoor already open
    if obj.Parent==nil then return end --a weapon touched the button
    if game.Players:playerFromCharacter(obj.Parent)==nil then return end --not a player
    id=id+1"Bright red")
    delay(30, function() check(id) end)

Chapter 5[edit]

The modifications for each script is shown with arrows, -->. The first script was almost perfect, but had a few serious flaws:

--Sample 1
function fadeBrick(obj)
for i = 100, 0: --> "for i = 0, 1, 0.1 do"
-->wait(0.1) --otherwise there will be no pause before the transparency goes to 1
-->end --to end the for loop
fadeBrick(game.Workspace.FadeThisBrick) --assume that this brick does exist

The next script is also a mess, but the main error is a logic error:

--Sample 2; a little harder...
function flicker(brick)
original=brick --this does not work because "original" will contain a *reference* instead of a duplicate. Either the line should read "original=brick:clone()", or better, "original=brick.Parent", since that's the whole point of the variable in the script.
brick.Parent=original.Parent -->"= original", because original now contains the parent.
while i not 10 do flicker(game.Workspace.CoolModel.Children) --assume "CoolModel" is a model in the Workspace that contains several bricks
-->Delete the above mess and replace it with:
-->for i = 1, 10 do --flickers everything 10 times, one brick right after another. A different script would be to have them all flicker simultaneously, using coroutines.
-->ch=game.Workspace.CoolModel:GetChildren() --note the ":" instead of the ".", and the assignment to a variable
-->for j = 1, #ch do flicker(ch[j]) end

And the 3rd script, with a lot of minor errors and problems all in one location:

--Sample 3. Whoever wrote this must be really tired... or maybe just didn't read "How to Script Anything"
function explode(model)
for i = 1, ch: ch(i).Remove
-->Change the above to:
-->for i = 1, #ch do
-->if ch[i].className=="Part" or ch[i].className=="SpawnLocation" or ch[i].className=="Seat" then
-->--otherwise, this script will try and add explosions to models and cameras, which do not have the ".Position" property.
--->The reason for these changes are: 1. #ch returns a number, important for a "for loop", but "ch" just returns a table. The is basic syntax, and you cannot assign a variable to an object (ExplosionObject = ch[i] is not legal, ExplosionObject.Parent = ch[i] is). Of course, there are multiple properties that need to be assigned, so it is better to assign the ExplosionObject to a variable, which we can manipulate. Next, the ch[i] requires square brackets, [], not curled, (). Finally, functions of objects need a colon, not a period.

Remember, there will usually be more than one correct way to fix a script.
If you caught most, if not all, of these mistakes, congratulations! Otherwise, it would be useful to continue studying lua syntax and getting familiar with all the ways of scripting. Practice, ask questions, and then let your imagination fly with your new scripting skills!