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
local.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

script.Parent.Parent.Parent.Parent.Frame.TextLabel.Text = "variable" --The long way.

local text = script.Parent.Parent.Parent.Parent.Frame.TextLabel.Text --text is now defined as the TextLabel's text. (the short way)

--then you can use it to change the text later on and print the text

text = "answer"

for i = 0,8 do
       print(text)
--Will print the TextLabels text 8 times.

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.

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:

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:

do
    do
        local potato=Workspace.Potato
        do
            print(potato) --Prints Workspace.Potato
        end
    end
    print(potato) --ERROR! This will cause the script to crash, because after the above "end" statement, the variable potato no longer exists!
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:

function touch(hit) --define the function
    local h=hit.Parent:FindFirstChild("Humanoid") --Check for the object's Humanoid. This will return nil if the touching object is not a character.
    if h then --In plain English: "if there is a humanoid, then do this:"
        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.
    end
end
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!

Log in to vote
1
Answered by 9 years ago

Variables are great! Here's a simple explanation:

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

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

ping = game.Workspace.Part

ping.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