Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What does a variable used for?

Asked by 9 years ago
1local.script.Parent.FindFirstChild

Im trying to add a teleportation in this script. Do I need a variable?

3 answers

Log in to vote
1
Answered by 9 years ago

local. is an error.

A variable is preety much a shorter way to define long lines.

Lets say

01script.Parent.Parent.Parent.Parent.Frame.TextLabel.Text = "variable" --The long way.
02 
03local 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 
07text = "answer"
08 
09for i = 0,8 do
10       print(text)
11--Will print the TextLabels text 8 times.
12 
13end

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.

Ad
Log in to vote
2
Answered by 9 years ago

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:

1local 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:

1do
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!
9end

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:

1function 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
6end
7script.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!

Log in to vote
1
Answered by 9 years ago

Variables are great! Here's a simple explanation:

1game.Workspace.Part.Velocity = Velocity3.new"0,0,5"

If you wanted to shorten that, you can use a variable!

1ping = game.Workspace.Part
2 
3ping.Velocity = Velocity3.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.

Hoped this helped! Variables are really nice!

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!

Answer this question