1 | local .script.Parent.FindFirstChild |
Im trying to add a teleportation in this script. Do I need a variable?
local. is an error.
A variable is preety much a shorter way to define long lines.
Lets say
01 | script.Parent.Parent.Parent.Parent.Frame.TextLabel.Text = "variable" --The long way. |
02 |
03 | local text = script.Parent.Parent.Parent.Parent.Frame.TextLabel.Text --text is now defined as the TextLabel's text. (the short way) |
04 |
05 | --then you can use it to change the text later on and print the text |
06 |
07 | text = "answer" |
08 |
09 | for i = 0 , 8 do |
10 | print (text) |
11 | --Will print the TextLabels text 8 times. |
12 |
13 | end |
btw this is a test script that won't work but just for you to get an idea for what a variable is used for.
A variable is really just a reference to an existing object. For instance, say I have a part in Workspace
named Potato
. If I was using a really long script, I could write Workspace.Potato
repeatedly, or I could simply define a variable for it:
1 | local potato = Workspace.Potato |
And then, what's with the local
keyword at the beginning?
The local
keyword keeps any other part of the script from using the variable. It is best used for functions and loops. Here is a better way to visualize that:
1 | do |
2 | do |
3 | local potato = Workspace.Potato |
4 | do |
5 | print (potato) --Prints Workspace.Potato |
6 | end |
7 | end |
8 | print (potato) --ERROR! This will cause the script to crash, because after the above "end" statement, the variable potato no longer exists! |
9 | end |
So that's what local
is used for.
There are all sorts of other uses for variables. If I were to make a lava script, I would do this:
1 | function touch(hit) --define the function |
2 | local h = hit.Parent:FindFirstChild( "Humanoid" ) --Check for the object's Humanoid. This will return nil if the touching object is not a character. |
3 | if h then --In plain English: "if there is a humanoid, then do this:" |
4 | h.Health = 0 --Kill the player by taking away all its health. Note that I did not have to write hit.Parent.Humanoid, I just had to write h. |
5 | end |
6 | end |
7 | script.Parent.Touched:connect(touch) --Connect the function to the event. Now, every time the brick is touched, the function will run. |
You can learn more about variables here. I hope this helped!
Variables are great! Here's a simple explanation:
1 | game.Workspace.Part.Velocity = Velocity 3. new "0,0,5" |
If you wanted to shorten that, you can use a variable!
1 | ping = game.Workspace.Part |
2 |
3 | ping.Velocity = Velocity 3. new "0,0,5" |
It's just saying to your script "Hey, Ping (or whatever you want to call it) is located in the Game, in the Workspace, and is called Part!" And so you can refer to it as Ping.
Like in real life!
You would say "Hey, the kid at the coffee shop on 34th street is Joe." You wouldn't refer to him as "the kid from the coffee shop on 34th street". You'd refer to him as Joe.
If this helped you, be sure to hit that up arrow! If you have a question, feel free to comment on here or PM me on ROBLOX. Thanks!