How to Script Anything

From Legacy Roblox Wiki
Jump to navigationJump to search

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!