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

Uses for Variables?

Asked by 8 years ago

What are useful things I can do for Variables? (Sorry, I am new to scripting.)

0
I might of made a mistake making this question as the Glossary will help me a bit dudeurkewlalso 5 — 8y

3 answers

Log in to vote
1
Answered by 8 years ago

Variables can hold different types of Values:

  • Object
  • Floats
  • Strings
  • Tables

Objects

These are mostly to shorten scripts or could be used with FindFirstChild/WaitForChild or, you can use them to change properties:

--Shorten; Which is more shorter? 1 or 2?
--1:
local x = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Button.Activate
x.Name = "Test"
wait(3)
x.Name = "Activate"
--2:
script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Button.Activate.Name = "Test"
wait(3)
script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Button.Test = "Activate"

Sure, 2 has less lines(3) but would you really write "script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Button.Activate" over and over again? No. Also, little bonus, most of the time to find the children of something you need to look for the name. If you use the variable, you don't need to worry if the name changes:

local a = script.Parent.Script
a.Name = "qwerty"
a.Disabled = false
--Name changed, we don't need to worry about that.
script.Parent.Script.Name = "qwerty"
script.Parent.qwerty.Disabled = false --We needed to say script.Parent.qwerty because the name changed.

FindFirstChild could be used because FindFirstChild could see if something exists. If it does, it returns the Object, if it doesn't exist, then it returns nil or nothing. This could be used to see if an object exists instead of making an error

local part = workspace:FindFirstChild("Part")

if part then --If this exists then...
    print("Exists")
end

Floats

Floats are numbers, they are better than Ints because they can use decimals and better than NumberValues since when NumberValues use Decimals it turns into: "1.100000341". The good thing about using floats with variables is you can use the number multiple times. If the number is something like32432985329075892357 you can shorten it to a simple word like "Number".

local num = 12
num = num+1
print(num)
num = num-1
print(num)

Strings

Strings are text:

print("I am a string",
'I am also a string',
[[I am still a string]])

Just like the Floats one, they're good because you can reuse it.

local text = "FOLLOW THE RULES"
print(text)
wait(1)
print(text)
wait(2)
print(text)

Tables

These are variables that fit OTHER variables inside. The bonus with this is it is also make scripts shorter.

local tAble = {"STRING2", 321, workspace.pArt}
local table = {"STRING", 123, workspace.Part, tAble} --Yes, you can put a table into a table.

table.insert(table,"Text")
table.remove(table,4)
--You can pretty much only put tables as variables.


Hope it helps!

Ad
Log in to vote
1
Answered by
woodengop 1134 Moderation Voter
8 years ago

Variables are very useful for holding certain values, some variables can be defined some undefined.

local UnDefined

local Defined="In a String format"

You can declare a variable in two ways, local, or a Global variable(withoutlocal), Global variables are in a Scope but can be accessible throughout the whole script,


local function Printer()
    Global=" adark, Bluethingy,Goulstem."
end

Printer()

print("Lists of people who overkill questions :"..Global) --> Lists of people who overkill questions : adark, Bluethingy,Goulstem.

The global variables can be accessed in or out of a function, statement, loop. local Variables on the other hand, have a scope but can not always be accessed throughout the script,


local function Printer()
    local ScopedVariable=" adark, Bluethingy,Goulstem."
end

Printer()

print("Lists of people who overkill questions :"..Global) --> Lists of people who overkill questions : nil.

Variables can hold objects such as, Tables, Strings, Values, etc.

local array={} -- A Array

local string=" A string" -- a String

local object=game.Workspace.Object -- a object.
Log in to vote
-1
Answered by 8 years ago

I would delete this before a bunch of jerks repeatedly downvote you. But anyway, they are basically used to define things. Instead of doing this...

game.Workspace.Part.Script.Disabled = true

You could do...

script = game.Workspace.Part.Script

script.Disabled = true

A couple other things... 1) A variable cannot start with a number. "Brick3" would work, but not "3Brick" 2) A "local" variable can only be used in that function/loop. A regular variable could be used anywhere. The good thing about local variables is that they can be called slightly faster, producing less wait time. IMO it isn't very noticable. That's about it. Good luck, and Happy Scripting!

0
lol you're really confusing him making a variable named script, KoreanBBQ 301 — 8y
0
wrong, variables define values, not "things". \s woodengop 1134 — 8y

Answer this question