How to Script Anything: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>JulienDethurens
No edit summary
Improving page
 
Line 1: Line 1:
==Introduction==
== Introduction ==
Welcome to '''How to Script Anything'''! This miniature scripting "book" will teach you:
Welcome to '''How to Script Anything'''! This miniature scripting "book" will teach you:
<br>-Scripting in general (variables, syntax, etc.)
* Scripting in general (variables, syntax, etc.)
<br>-The basic commands and details
* The basic commands and details
<br>-Advanced commands
* Advanced commands
<br>-Logical thinking (and planning)
* Logical thinking (and planning)
<br>-How to script almost anything you want
* How to script almost anything you want
<br>Preliminary knowledge: How to navigate through Roblox; the basics of playing in Roblox.
Preliminary knowledge: How to navigate through Roblox; the basics of playing in Roblox.
<br>You will need to do independent research on the links provided.
== Be warned ==
<br>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.
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.
<br>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).
<br />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.
<br />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 ==
=== [[How to Script Anything/Chapter1|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 kill 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.
=== [[How to Script Anything/Chapter2|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).  
=== [[How to Script Anything/Chapter3|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.
=== [[How to Script Anything/Chapter4|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.
=== [[How to Script Anything/Chapter5|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.
== 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.
=== Chapter 1 ===
<syntaxhighlight lang="lua" line>
local myVariable = 10
local myVariable2 = 5
 
myVariable = myVariable * myVariable2
 
print("myVariable's value: " .. myVariable)
 
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
</syntaxhighlight>
=== Chapter 2 ===
<syntaxhighlight lang="lua" line>
local var1 = 5 --editable
local var2 = 11 -- editable
 
for i = 1, 20 do
if i % 2 == 0 then
print(i .. " even")
else
print(i .. " odd")
end
end
 
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
 
local min1 = 1
local max1 = 1
local max1 = 10
local max2 = 20
local trials = 100
local match = 0
local 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
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)).
</syntaxhighlight>
=== Chapter 3 ===
<syntaxhighlight lang="lua" line>
local 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.
local 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


==Be warned...==
print(recursionSum(10)) -- will print out 30
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.
-- TODO Finish classes
<br>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.
</syntaxhighlight>
<br>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 4 ===
<syntaxhighlight lang="lua" line>
local debounce = false
local part = script.Parent


== Chapters ==
local function onTouch(otherPart) -- assumes that the door starts out with CanCollide == true
if debounce then
return
end


=== [[How to Script Anything/Chapter1|Chapter 1: Scripting Basics]] ===
debounce = true


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.  
local transparency = d.Transparency


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.  
part.Transparency=0.5
part.CanCollide = false


=== [[How to Script Anything/Chapter2|Chapter 2: Flow Control]] ===
local color = part.BrickColor


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).
for index = 1, 5, 0.1 do
part.BrickColor = BrickColor.Random()


=== [[How to Script Anything/Chapter3|Chapter 3: Functions, Data Types, and Coroutines]] ===
wait(0.1)
end


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.  
part.BrickColor = color
part.Transparency = transparency
part.CanCollide = true


=== [[How to Script Anything/Chapter4|Chapter 4: How to Interact with Roblox]] ===
debounce = false
end


Most of the basic features of Lua have been introduced by now. However, few of the scripts have done anything useful in Roblox.  
part.Touched:connect(onTouch)
</syntaxhighlight>
Note: There are many ways of writing this script.
<syntaxhighlight lang="lua" line>
local Players = game:GetService("Players")


=== [[How to Script Anything/Chapter5|Chapter 5: How to Script Anything]] ===
local door = workspace.TrapDoor
local button = workspace.Button
local open = false -- actual state of trapDoor
local 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.
local debounce = false


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.
local function closeDoor()
The second thing is much more complicated: Make a brick fly and attack players.  
button.BrickColor = BrickColor.new("Bright green")
door.CanCollide = true
door.Transparency = 0


==Solutions to Practice Questions==
debounce = false -- only make debounce false here to ensure that 'closeDoor' isn't called with a 1 second delay and the button is touched within that second.
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===
* myVar = 10
* myVar2 = 5
<br>myVar = myVar * myVar2
<br>print("myVar's value: " .. myVar)
* print(1==3) --will print false
<br>print(8<8) --will print false
<br>print(8<9) --will print true
<br>print(8<8 or 0>-5) --will print true
<br>print(8<8 and 0>-5) --will print false
<br>print(true and true) --will print true
<br>etc.
* 1+1*2 == 3 and 3*3==9
<br>Simplifies to: 1+1 == 3 and 3*3==9
<br>2 == 3 and 9==9
<br>false and true
<br>'''false'''
===Chapter 2===
* for i = 1, 20 do
<br>if i % 2 == 0 then
<br>print(i .. " even")
<br>else
<br>print(i .. " odd")
<br>end
<br>end
* var1 = 5 --editable
<br>var2=11 --editable
<br>if var1*2>var2 then
<br>print("Two times var1 is greater than var2.")
<br>elseif var1*2==var2 then
<br>print("Two times var1 equals var2.")
<br>else
<br>print("Two times var1 is less than var2.")
<br>end
* for i = 3, 13 do
<br>if i~=5 and i~=8 then
<br>print(i)
<br>end
* for i = 5, 1, -0.2 do print(i) end
* min1=1
<br>max1=1
<br>max1=10
<br>max2=20
<br>trials=100
<br>match=0
<br>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.
<br>for i = 1, trials do
<br>if math.random(min1, max1)==math.random(min2,max2) then
<br>match=match+1
<br>else
<br>notMatch=notMatch+1
<br>end
<br>end
<br>print("Number of times they matched: " .. match)
<br>print("Number of times they didn't match: " .. notMatch) --or, if notMatch was eliminated, it would be ..."didn't match: " .. (trials-match)).
===Chapter 3===
* function printOut()
<br>for j = 1, 5 do
<br>print(math.random(1,10))
<br>wait(1)
<br>end
<br>end
<br>--Alternate method; use: delay(0, printOut)
<br>coroutine.resume(coroutine.create(printOut))
<br>wait(0.5)
<br>for i = 1, 4 do
<br>print("Hello!")
<br>end
<br><br>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)
<br>if num%2==1 then max=max-1 end --make "num" an even number
<br>if num>1 then
<br>return num+recursionSum(num-2)
<br>end
<br>return num
<br>end
<br><br>print(recursionSum(10)) --will print out 30
* TODO Finish classes
===Chapter 4===
* db=false
<br>d=script.Parent
<br>function onTouch(obj) --assumes that the door starts out with CanCollide==true
<br>if db then return end
<br>db=true
<br>trans=d.Transparency
<br>d.Transparency=0.5
<br>d.CanCollide=false
<br>colour=d.BrickColor
<br>for i = 1, 5, 0.1 do
<br>d.BrickColor=BrickColor.Random()
<br>wait(0.1)
<br>end
<br>d.BrickColor=colour
<br>d.Transparency=trans
<br>d.CanCollide=true
<br>db=false
<br>end
<br>d.Touched:connect(onTouch)
* Note: There are many ways of writing this script.
<pre>
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
local function check(value)
if value == id and open then
closeDoor()
open = false
end
end
end
function closeDoor()
 
button.BrickColor=BrickColor.new("Bright green")
local function onDoorTouch(otherPart)
door.Transparency=0
if otherPart.Parent then
door.CanCollide=true
if not open then
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.
-- already closed
 
return
end
 
if Players:GetPlayerFromCharacter(otherPart.Parent) then
open = true
 
delay(1, closeDoor) -- provide time for the person to get through
end
end
end
end
function onDoorTouch(obj)
 
if not open then return end --already closed
local function onButtonTouch(otherPart)
if obj.Parent==nil then return end --a weapon touched the door
if otherPart.Parent and not open then
if game.Players:playerFromCharacter(obj.Parent)==nil then return end --not a player
if Players:GetPlayerFromCharacter(otherPart.Parent) then
open=false
open = true
delay(1, closeDoor) --provide time for the person to get through
 
debounce = true
 
id = id + 1
 
button.BrickColor = BrickColor.new("Bright red")
 
door.CanCollide = false
door.Transparency = 0.6
 
delay(30, function()
check(id)
end)
end
end
end
end
function onButtonTouch(obj)
</syntaxhighlight>
if open then return end --trapdoor already open
=== Chapter 5 ===
if obj.Parent==nil then return end --a weapon touched the button
<syntaxhighlight lang="lua" line>
if game.Players:playerFromCharacter(obj.Parent)==nil then return end --not a player
-- Sample 1
open=true
local function fadeBrick(object)
db=true
for index = 0, 1, 0.1 do
id=id+1
object.Transparency = index
button.BrickColor=BrickColor.new("Bright red")
 
door.Transparency=0.6
wait(0.1) -- otherwise there will be no pause before the transparency goes to 1
door.CanCollide=false
end -- to end the for loop
delay(30, function() check(id) end)
 
object:Destroy()
end
end
</pre>
 
===Chapter 5===
fadeBrick(workspace.FadeThisBrick) -- assume that this brick does exist
The modifications for each script is shown with arrows, -->.
</syntaxhighlight>
The first script was almost perfect, but had a few serious flaws:
<syntaxhighlight lang="lua" line>
<pre>
-- Sample 2; a little harder...
--Sample 1
local function flicker(brick)
function fadeBrick(obj)
local original = brick.Parent
for i = 100, 0: --> "for i = 0, 1, 0.1 do"
 
obj.Transparency=i
brick.Parent = nil
-->wait(0.1) --otherwise there will be no pause before the transparency goes to 1
 
-->end --to end the for loop
wait(0.03)
obj.Remove()
 
brick.Parent = original
end
end
fadeBrick(game.Workspace.FadeThisBrick) --assume that this brick does exist
 
</pre>
for index = 1, 10 do
The next script is also a mess, but the main error is a logic error:
-- flickers everything 10 times, one brick right after another. A different script would be to have them all flicker simultaneously, using coroutines.
<pre>
for index, value in ipairs(workspace.CoolModel:GetChildren()) do
--Sample 2; a little harder...
flicker(value)
function flicker(brick)
end
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
end
while i not 10 do flicker(game.Workspace.CoolModel.Children) --assume "CoolModel" is a model in the Workspace that contains several bricks
</syntaxhighlight>
-->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
</pre>
And the 3rd script, with a lot of minor errors and problems all in one location:
And the 3rd script, with a lot of minor errors and problems all in one location:
<pre>
<syntaxhighlight lang="lua" line>
--Sample 3. Whoever wrote this must be really tired... or maybe just didn't read "How to Script Anything"
-- Sample 3.
function explode(model)
local function explode(model)
ch=model:GetChildren()
for index, value in ipairs(model:GetChildren()) do
for i = 1, ch: Explosion.new()=ch(i) ch(i).Remove
if value.className == "Part" or value.className == "SpawnLocation" or value.className == "Seat" then
-->Change the above to:
-- otherwise, this script will try and add explosions to models and cameras, which do not have the ".Position" property.
-->for i = 1, #ch do
local explosion = Instance.new("Explosion", nil)
-->if ch[i].className=="Part" or ch[i].className=="SpawnLocation" or ch[i].className=="Seat" then
explosion.Parent = workspace
-->--otherwise, this script will try and add explosions to models and cameras, which do not have the ".Position" property.
explosion.Position = value.Position
-->e=Instance.new("Explosion")
 
-->e.Parent=Workspace
value:Destroy()
-->e.Position=ch[i].Position
end
-->ch[i]:Destroy()
end
-->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
end
wait(60)
wait(60)
explode(game.Workpace)
 
</pre>
explode(workpace)
</syntaxhighlight>
Remember, there will usually be more than one correct way to fix a script.
Remember, there will usually be more than one correct way to fix a script.
<br>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!
<br />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!
[[Category:Scripting Tutorials]]

Latest revision as of 01:13, 18 July 2023

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.

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 kill 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.

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.

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.

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.

Chapter 1

local myVariable = 10
local myVariable2 = 5

myVariable = myVariable * myVariable2

print("myVariable's value: " .. myVariable)

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

Chapter 2

local var1 = 5 --editable
local var2 = 11 -- editable

for i = 1, 20 do
	if i % 2 == 0 then
		print(i .. " even")
	else
		print(i .. " odd")
	end
end

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

	local min1 = 1
	local max1 = 1
	local max1 = 10
	local max2 = 20
	local trials = 100
	local match = 0
	local 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
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)).

Chapter 3

local 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.
local 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

Chapter 4

local debounce = false
local part = script.Parent

local function onTouch(otherPart) -- assumes that the door starts out with CanCollide == true
	if debounce then
		return
	end

	debounce = true

	local transparency = d.Transparency

	part.Transparency=0.5
	part.CanCollide = false

	local color = part.BrickColor

	for index = 1, 5, 0.1 do
		part.BrickColor = BrickColor.Random()

		wait(0.1)
	end

	part.BrickColor = color
	part.Transparency = transparency
	part.CanCollide = true

	debounce = false
end

part.Touched:connect(onTouch)

Note: There are many ways of writing this script.

local Players = game:GetService("Players")

local door = workspace.TrapDoor
local button = workspace.Button
local open = false -- actual state of trapDoor
local 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.
local debounce = false

local function closeDoor()
	button.BrickColor = BrickColor.new("Bright green")
	door.CanCollide = true
	door.Transparency = 0

	debounce = false -- only make debounce false here to ensure that 'closeDoor' isn't called with a 1 second delay and the button is touched within that second.
end

local function check(value)
	if value == id and open then
		closeDoor()

		open = false
	end
end

local function onDoorTouch(otherPart)
	if otherPart.Parent then
		if not open then
			-- already closed

			return
		end 

		if Players:GetPlayerFromCharacter(otherPart.Parent) then
			open = true

			delay(1, closeDoor) -- provide time for the person to get through
		end
	end
end

local function onButtonTouch(otherPart)
	if otherPart.Parent and not open then
		if Players:GetPlayerFromCharacter(otherPart.Parent) then
			open = true

			debounce = true

			id = id + 1

			button.BrickColor = BrickColor.new("Bright red")

			door.CanCollide = false
			door.Transparency = 0.6

			delay(30, function()
				check(id)
			end)
		end
	end
end

Chapter 5

-- Sample 1
local function fadeBrick(object)
	for index = 0, 1, 0.1 do
		object.Transparency = index

		wait(0.1) -- otherwise there will be no pause before the transparency goes to 1
	end -- to end the for loop

	object:Destroy()
end

fadeBrick(workspace.FadeThisBrick) -- assume that this brick does exist
-- Sample 2; a little harder...
local function flicker(brick)
	local original = brick.Parent

	brick.Parent = nil

	wait(0.03)

	brick.Parent = original
end

for index = 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.
	for index, value in ipairs(workspace.CoolModel:GetChildren()) do
		flicker(value)
	end
end

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

-- Sample 3.
local function explode(model)
	for index, value in ipairs(model:GetChildren()) do
		if value.className == "Part" or value.className == "SpawnLocation" or value.className == "Seat" then
			-- otherwise, this script will try and add explosions to models and cameras, which do not have the ".Position" property.
			local explosion = Instance.new("Explosion", nil)
			explosion.Parent = workspace
			explosion.Position = value.Position

			value:Destroy()
		end
	end
end

wait(60)

explode(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!