How to Script Anything/Chapter4

From Legacy Roblox Wiki
Jump to navigationJump to search

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.

The script Variable

There are very few properties of a script that are important to look at, and for the most part should be researched in the rest of the wiki. However, the basic ones are:
In a script, you can access the script object itself with the variable "script". Accessing its parent is therefore "script.Parent".

Events

Events are essential in almost every script. The most basic event can be used to make a door. The syntax of an event is:
object.EventName:connect(functionName)
Note that functionName can be an unnamed function. This is useful if you have extra parameters that you wish to pass to your function handler (usually this might be referencing the door itself). Each event will pass its own parameters; the onTouch event used for a door passes only what touches the object.
This example wil use onTouch for a door. The door script will look like this:

function onTouch(obj)
script.Parent.Transparency=0.8
script.Parent.CanCollide=false
wait(2)
script.Parent.Transparency=0
script.Parent.CanCollide=true
end
script.Parent.Touched:connect(onTouch)

If anything touches the door, it will turn mostly transparent for 2 seconds and allow anything to pass through it. After 2 seconds, it becomes solid and completely opaque (non-transparent).

Debounce

To expand on the above script, a debounce system could be implemented. A debounce system ensures that a function is not running on multiple coroutines. In some cases, a debounce system is essential to allow the script to operate properly. In the above script, imagine that the door is touched, and then 1.5 seconds later, is touched again, and then 1 second after the second touch, a 3rd touch to the door is applied. Here is what would happen:

t (time) = 0, 1st touch. Door is "open"
t = 1.5, 2nd touch. Door is re-opened.
t = 2, 1st touch closes the door. Door is "closed", even though the coroutine handling the 2nd touch event still thinks the door is open.
t = 2.5, 3rd touch. Door is opened.
t = 3.5, 2nd touch closes the door. Note that this cuts the time that the door is open for the 3rd touch by 0.5 seconds, an undesired event.
t = 4, 3rd touch re-closes the already closed door.

It is clear that the 2nd touch is undesired. The way to solve this problem is using debounce, a technique in which a variable is used to check if the function is already running. The script looks like:

db=false --short for 'DeBounce'
function onTouch(obj)
if db then return end --exits the function early if "db" is true.
db=true --sets "db" to true so that any future events will quit early.
script.Parent.Transparency=0.8
script.Parent.CanCollide=false
wait(2)
script.Parent.Transparency=0
script.Parent.CanCollide=true
db=false --because we are about to exit the function, "db" is set to false so that future events will not exit early.
end
script.Parent.Touched:connect(onTouch)

In this case, any time something touches the door while the door is open will not be registered. Note that a debounce system is useless if there are no wait() commands, and can get complicated when handling multiple objects at once. This will be examined in Chapter 5.

Checking Conditions before Running an Event

In the above example, the door will open for both players and any bricks that happen to fly and touch it. If the door was meant to only open for a player, the condition must be checked before running, just like the debounce system checked to make sure the door wasn't already open. The script will check to see if there is a player that exists whose character is the model that touched the door.

db=false --short for 'DeBounce'
function onTouch(obj)
if db then return end --exits the function early if "db" is true.
if obj.Parent==nil then return end --this is important. Without it, weapons will break the script since they may disappear when they touch something, causing their parent to be nil. Attempting to access a nil object's properties will result in an error.
if game.Players:playerFromCharacter(obj.Parent)==nil then return end --this is not a player touching the door
db=true --Note that the db statement is AFTER all checks to exit the function. If this was set to true and then it was found that the obj's parent was nil, db would never be reset to false.
script.Parent.Transparency=0.8
script.Parent.CanCollide=false
wait(2)
script.Parent.Transparency=0
script.Parent.CanCollide=true
db=false
end
script.Parent.Touched:connect(onTouch)

Now the script will only open the door if a player touches the door.

Instance.new()

Instance.new() is a very simple function that creates another object and returns the object. If, in a script, another brick was required, one would use:
variable=Instance.new("Part")
Note that instead of "Part", the name of any other class/object could be place there. For example, "BodyGyro" or "BodyPosition" are also useful. Occasionally you may not need to do anything with the object, such as if you want to place a ForceField somewhere and leave it there indefinitely. For this, you can use the object directly:
Instance.new("ForceField").Parent=location
An alternative method to making your own brick in a script is to make a backup brick with all the properties you desire and store it in game.Lighting. Name it something special and you can clone it instead of generating a new brick:
variable=game.Lighting.SpecialBrick:clone()
However, it is not suggested to use this method, since the script can no longer be put into any place and function properly until the special brick is cloned into the new place's Lighting.

Using the Object Browser

It is very helpful to use the object browser, located under "Help" in Roblox Studio. Here a list of all classes for Roblox exists, allowing you to find the names of properties and functions for any object including cameras, bricks, teams, players, sparkles, etc.

_G Variable

Roblox has provided scripters with a way of allowing all scripts in the Workspace to communicate with eachother using the _G variable. Note that scripts in hopperbins do not have access to this variable in online mode, although tools might (edit required here to confirm whether tools can or not; I think they can access it) .

The _G variable is used as a table. If you wish to add a global variable to it, simply use:
_G.variableName = value
Note that variableName can be absolutely anything, from classes to tables to single values (like strings and numbers). Aside from hopperbins, scripts will all access the same _G variable.

Alternate Method of Script Communication


Another way of allowing scripts to communicate is using objects such as NumberValue, BooleanValue, Vector3Value, StringValue, etc. Coming up with a system that all your scripts can understand will enable you to work around the _G hopperbin limitation, although lag may increase since each value will have at least 2 other variables attached to it (such as className and Parent). However, working the name and value may work to your advantage to make use of every variable. Example:
A script system might use an ObjectValue to represent a reference to a player. The value is of course, the player from game.Players, and the name might be a code for a different script that says what action must be taken on this person. It might be named "Bonus" to indicate that the action that should be taken on this person is a good thing, or possibly named "11a6" for a completely unique code that a script decodes (much like a save/load script does, only more simplistic).
As a child of the ObjectValue, there may be StringValues to represent more details, such as what type of bonuses to receive, or what other actions must take place. It depends on what you need and how you decide to get your scripts communicating.

Try it out!

  • Write a script that opens a door and changes its colours while its open, then returns to its original state after 5 seconds.
  • Write a script for a button that, when pressed, will open up a trap door that will stay open until either 30 seconds have passed or someone goes through the trap door.


Back to How to Script Anything