generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Building Helpers
Home Search
 

Re: How to Script Anything

Previous Thread :: Next Thread 
stealthyspetguest is not online. stealthyspetguest
Joined: 17 Jun 2012
Total Posts: 1287
03 Jan 2013 05:36 PM
Introduction

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. I am writing this thread because a lot of people want to know how to script, but are confused by other threads (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, try to add in more advanced scripting techniques if you are experienced in them

Be warned...

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).
Chapters

Chapter 1: Scripting Basics
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
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
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.
[edit] Chapter 4: How to Interact with Roblox
Most of the basic features of Lua have been introduced by now. However, few of the scripts have done anything useful in Roblox.
[edit] Chapter 5: How to Script Anything
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.
[edit]Solutions to Practice Questions

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.
[edit]Chapter 1
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
etc.
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
true
[edit]Chapter 2
for i = 1, 20 do

if i % 2 == 0 then
print(i .. " even")
else
print(i .. " odd")
end
end
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.")
else
print("Two times var1 is less than var2.")
end
for i = 3, 13 do

if i~=5 and i~=8 then
print(i)
end
for i = 5, 1, -0.2 do print(i) end
min1=1

max1=1
max1=10
max2=20
trials=100
match=0
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
match=match+1
else
notMatch=notMatch+1
end
end
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)).
[edit]Chapter 3
function printOut()

for j = 1, 5 do
print(math.random(1,10))
wait(1)
end
end
--Alternate method; use: delay(0, printOut)
coroutine.resume(coroutine.create(printOut))
wait(0.5)
for i = 1, 4 do
print("Hello!")
end

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)
end
return num
end

print(recursionSum(10)) --will print out 30
TODO Finish classes
[edit]Chapter 4
db=false

d=script.Parent
function onTouch(obj) --assumes that the door starts out with CanCollide==true
if db then return end
db=true
trans=d.Transparency
d.Transparency=0.5
d.CanCollide=false
colour=d.BrickColor
for i = 1, 5, 0.1 do
d.BrickColor=BrickColor.Random()
wait(0.1)
end
d.BrickColor=colour
d.Transparency=trans
d.CanCollide=true
db=false
end
d.Touched:connect(onTouch)
Note: There are many ways of writing this script.
door=game.Workspace.TrapDoor
button=game.Workspace.Button
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.
db=false
function check(value)
if value==id and open then
closeDoor()
open=false
end
end
function closeDoor()
button.BrickColor=BrickColor.new("Bright green")
door.Transparency=0
door.CanCollide=true
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.
end
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
open=false
delay(1, closeDoor) --provide time for the person to get through
end
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
open=true
db=true
id=id+1
button.BrickColor=BrickColor.new("Bright red")
door.Transparency=0.6
door.CanCollide=false
delay(30, function() check(id) end)
end
[edit]Chapter 5
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"
obj.Transparency=i
-->wait(0.1) --otherwise there will be no pause before the transparency goes to 1
-->end --to end the for loop
obj.Remove()
end
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=nil
wait()
brick.Parent=original.Parent -->"= original", because original now contains the parent.
end
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
-->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)
ch=model:GetChildren()
for i = 1, ch: Explosion.new()=ch(i) 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.
-->e=Instance.new("Explosion")
-->e.Parent=Workspace
-->e.Position=ch[i].Position
-->ch[i]:Destroy()
-->end
--->The reason for these changes are: 1. #ch returns a number, important for a "for loop", but "ch" just returns a table. The Instance.new() 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.
end
wait(60)
explode(game.Workpace)
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!
Report Abuse
fearlesstutor is not online. fearlesstutor
Joined: 10 Aug 2012
Total Posts: 176
03 Jan 2013 05:37 PM
You typed this thread by hand your amazing.
Report Abuse
V111anz is not online. V111anz
Joined: 02 Aug 2012
Total Posts: 193
03 Jan 2013 05:38 PM
Yeah dude you got talent.
Report Abuse
V111anz is not online. V111anz
Joined: 02 Aug 2012
Total Posts: 193
03 Jan 2013 05:38 PM
Stealthyspetguest im your biggest fan.
Report Abuse
V111anz is not online. V111anz
Joined: 02 Aug 2012
Total Posts: 193
03 Jan 2013 05:39 PM
What do you owe us for such amazing talent?
Report Abuse
stealthyspetguest is not online. stealthyspetguest
Joined: 17 Jun 2012
Total Posts: 1287
03 Jan 2013 05:40 PM
Feel free to buy my self portrait decal for free.
Report Abuse
fearlesstutor is not online. fearlesstutor
Joined: 10 Aug 2012
Total Posts: 176
03 Jan 2013 05:40 PM
AWESOME!!!!!!
Report Abuse
stealthygirl123 is not online. stealthygirl123
Joined: 13 Mar 2012
Total Posts: 131
03 Jan 2013 05:41 PM
AMAZING!!!!!!!!!!!!!!!
Report Abuse
V111anz is not online. V111anz
Joined: 02 Aug 2012
Total Posts: 193
03 Jan 2013 05:42 PM
Stealthyspetguest your sooooooo AWESOME :D
Report Abuse
stealthygirl12345 is not online. stealthygirl12345
Joined: 16 Mar 2012
Total Posts: 31
03 Jan 2013 05:43 PM
Thats so cool stealthyspetguest.
Report Abuse
stealthygirl12345 is not online. stealthygirl12345
Joined: 16 Mar 2012
Total Posts: 31
03 Jan 2013 05:44 PM
You know what stealthyspetguest your epic.
Report Abuse
aseefa is not online. aseefa
Joined: 05 Nov 2012
Total Posts: 23
03 Jan 2013 05:46 PM
Stealthyspetguest your soo awesome :D
Report Abuse
stealthyspetnoob is not online. stealthyspetnoob
Joined: 02 Nov 2012
Total Posts: 4
03 Jan 2013 05:47 PM
Yeah your awesome.
Report Abuse
stealthyspetnoob is not online. stealthyspetnoob
Joined: 02 Nov 2012
Total Posts: 4
03 Jan 2013 05:48 PM
Every robloxian keep stealthyspetguest's thread popular because its so awesome.
Report Abuse
stealthyspetnoob is not online. stealthyspetnoob
Joined: 02 Nov 2012
Total Posts: 4
03 Jan 2013 05:58 PM
ITS 2013
Report Abuse
stealthyspetnoob is not online. stealthyspetnoob
Joined: 02 Nov 2012
Total Posts: 4
03 Jan 2013 06:00 PM
Oh and thanks stealthyspetguest we needed this
Report Abuse
buffrobloxian is not online. buffrobloxian
Joined: 07 Apr 2012
Total Posts: 323
03 Jan 2013 06:01 PM
LOLOLOLOLOLOLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
Report Abuse
buffrobloxian is not online. buffrobloxian
Joined: 07 Apr 2012
Total Posts: 323
03 Jan 2013 06:02 PM
trollololololololol
Report Abuse
fearlesstutor is not online. fearlesstutor
Joined: 10 Aug 2012
Total Posts: 176
03 Jan 2013 06:04 PM
Thanks we needed this.
Report Abuse
mjnecraft is not online. mjnecraft
Joined: 18 Oct 2012
Total Posts: 14
03 Jan 2013 06:08 PM
am i cool yet
Report Abuse
mjnecraft is not online. mjnecraft
Joined: 18 Oct 2012
Total Posts: 14
03 Jan 2013 06:10 PM
am i cool yet?
Report Abuse
mjnecraft is not online. mjnecraft
Joined: 18 Oct 2012
Total Posts: 14
03 Jan 2013 06:12 PM
Umm stealthyspetguest.
Report Abuse
mjnecraft is not online. mjnecraft
Joined: 18 Oct 2012
Total Posts: 14
03 Jan 2013 06:12 PM
YOU ARE AWESOME :3
Report Abuse
mjnecraft is not online. mjnecraft
Joined: 18 Oct 2012
Total Posts: 14
03 Jan 2013 06:13 PM
TROLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOL
Report Abuse
mjnecraft is not online. mjnecraft
Joined: 18 Oct 2012
Total Posts: 14
03 Jan 2013 06:14 PM
Listen up buffrobloxian i bloxxed your noobs in teh robloxia :3
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Building Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image