How to Script Anything/Chapter5

From Legacy Roblox Wiki
Jump to navigationJump to search

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.

Logical Thinking

It is important to think logically. To improve your logical thinking skills, it is useful to solve logic problems. This skill is also improved as you continue to practice scripting. This is why it is not suggested to try very large scripting projects until you've done a few minor ones. However, feel free to challenge yourself; if you find scripting something boring, that is a clear sign that you have mastered it, even if you make a few syntax errors.

Logical thinking simply allows you to think about what the computer is doing when it reads your commands. With no logic, one might think that scripting in "Do what I tell you to" as a command might work, meanwhile someone else with a lot of logic will know which commands must be used and how to use them. That's why all good scripters are able to script logically and will push themselves to think outside the box.

Get an Idea

The first step in scripting anything you want is to first come up with an idea. In this example, we already have two ideas, but you need to come up with your own when you script. Rescripting the same ideas does not accomplish much, with the exception of if you are trying to make the script more efficient (reducing lag). Creativity is a gift and should be used here. If you are finding it difficult to come up with something, get a piece of paper and write out a few ideas.

Our ideas here are:
1. Controlling 10 doors simultaneously: each door may or may not open when you touch it
2. Controlling a brick that will fly around and dive on people, reducing their health.

After you get your main idea, you may find more features that you wish to include. The only danger of these features is coming up with too many; be careful to ensure that all your ideas will work together. Also be aware of your own skill, if you can barely script a dance floor, it is unwise to try and script a plane.

Write it Out on Paper

The next step is to write out your ideas in English (instead of Lua). This is called an algorithm. If you are very skilled in Lua, you may not need to write out each step, and your algorithm may look like this (for idea #1):

  1. Connect all 10 doors to the onTouch function
  2. Check the debounce for the specific door
  3. Randomly open/close the door
  4. Undo the debounce for the door

However, for someone with less practice in scripting, it is important to write out the specifics:

  1. Let "door" be the table of doors.
  2. Let "db" be the table of debounce, each index working with the door number.
  3. Get all the 10 doors from the children of game.Workspace.Doors:
    • Let ch be the children
    • Use a for loop
    • Use the table.insert command for the "door" table AND the "db" variable.
  4. The function onTouch:
    1. It needs the parameters "obj" and "doorNumber"
    2. If db for the door is true, exit the function
    3. Make db for the doorNumber true
    4. If a random number between 1 and 100 is less than or equal to 25, open the door:
      • Set transparency to 0.6
      • Set CanCollide to false
    5. Otherwise, close the Door
      • Set transparency to 0
      • Set CanCollide to true
    6. After 1 second, set db for this door to false.
  5. Connect all 10 doors to function:
    • For loop
    • Unnamed function to pass both parameters

Do note that everyone's algorithm will be slightly different. It will depend on your idea and how you decide to write out the function. There is usually more than one way to script something well.

Script it

Now, script it.

local doors = game.Workspace.Doors:GetChildren()
local active = {}

for _, door in ipairs(doors) do
    door.Touched:connect(function()
        if active[door] then return end
        active[door] = true

        local rand = math.random(100)
        if rand <= 25 then
            door.Tranparency = 0.6
            door.CanCollide = false
        else
            door.Tranparency = 0
            door.CanCollide = true
        end

        wait(1)

        active[door] = false
    end)
end

Debugging and Testing

The next stage of writing your script is to debug it and test it. If you are in edit mode, open the place in solo mode and test the script this way. Ensure your output is open, since it will likely have syntax errors. Continue editing the script until there are no more syntax errors (and no more red text appears in the output). Now test its functionality. This may take several tests to ensure that it will work no matter what; another thing to try and do is if the script uses functions like 'onTouch', try hitting the trigger with weapons, other bricks, etc. to ensure that no matter what happens, the script will work the way you want it to.

If Lua continues to not do what you desire, there are a few possibilities:

  • You have made a logic error, in which case you should use Method A (below)
  • Roblox is bugged, in which case you should use Method B (below)

Method A Write out all the variables that are relevant to the code that is not working. Now, at each relevant event or stage where the variables change, write out their values and, step by step, follow the path that your script is supposed to be following. If you encounter a line in your script such as:
if a*b == c then doA() else doB() end
Sub in the values that you've recorded for a, b, and c, and see if they are equal. If they are, examine the function doA(), otherwise look at function doB() to see what Lua would do next. Eventually, you will have either found the error and fixed it, or missed it. If you are unable to find the error, you have a few choices: 1. Repeat the process, 2. Look for other factors you aren't considering, 3. Look for common variable names when you don't declare them local to a function 4. Post it on Scripting Help, 5. Consider the possibility that Lua is not working properly (this should be a last resort), and proceed to Method B.

Method B You should first ensure that you have checked all ways to solve your problem from Method A before contacting the admin. Especially posting it on Scripting Help to see if anyone else is having the problem. However, administrators cannot always solve everything, and sometimes a "bug" is not really a bug but a limitation of functionality (such as Hopperbins having scripts that are local only, or re-positioning bricks has a different effect than changing the CFrame of a brick, etc.). Occasionally you will find a bug with a new update, in which contacting the administrators is highly suggested.

Try it out!

Now you have the ability to script almost anything you want. With a lot of practice and a few questions, what you can script will increase dramatically.



Here are a few short sample scripts with many bugs in them. See if you can find where they are, determine what the code should have been, and fix the errors.

--Sample 1
function fadeBrick(obj)
for i = 100, 0:
obj.Transparency=i
obj.Remove()
end
fadeBrick(game.Workspace.FadeThisBrick) --assume that this brick does exist
--Sample 2; a little harder...
function flicker(brick)
original=brick
brick.Parent=nil
wait()
brick.Parent=original.Parent
end
while i not 10 do flicker(game.Workspace.CoolModel.Children) --assume "CoolModel" is a model in the Workspace that contains several bricks
--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
end
wait(60)
explode(game.Workpace)


Back to How to Script Anything