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

what is everything i can put in a variable?

Asked by 5 years ago

i have been seeing people put alot of things in varibles but what are all the things i can put in them?

1
what about when people use game.workspace.player in thier variables how can i learn more about that? EzireBlueFlame 14 — 5y
0
The player is really an object. More like a table than a variable. User#21908 42 — 5y
0
Also, you really should not be getting the player from workspace. Get the player from players. User#21908 42 — 5y
0
Could you tell me what is wrong with my answer so that I can correct it ezel? User#21908 42 — 5y

3 answers

Log in to vote
5
Answered by 5 years ago
Edited 5 years ago

While you might think they are putting a large amount of "things" into a variable, you must understand that not only variables can be defined with local. The local definition is there to define scope. An example of scope would be:

local a = 1
function findOutScope()
    local b = 2 
end
print(a, b)
-- output --> 1 nil 

Why is this? The reason b is nil is because we defined it locally inside of the function. This means that it is not visible outside of the function. Things defined locally are only visible in the block they were created in. Here is an example of local being used with a table and not a variable:

function scopeTrial()
    local a = {"a", "b", "c"} -- this is a table not a variable
end
print(a[1], a[2], a[3])
-- output --> nil nil nil
local a = {"a", "b", "c"} -- defining it outside of the block
print(a[1], a[2], a[3])
-- output --> a b c

local can also be used with functions and it is even an encouraged practice. The reason I did not use it in the explanations above was so that I could tackle each local individually and not make a really complex explanation involving both functions and tables (or something like that). Example of locally defined function:

local function hello()
    print("hi")
end

The only time that a function would be hidden by defining it locally would be if you had it inside of another block. Example:

local function hello() -- I will just use local all the time when defining now because it is good practice
    local function hi() -- nested function is what this is called
        print("hi")
    end
    hi()
end
hello() -- calling the function
-- output --> hi
hi() -- calling the inner function
-- output --> error because of calling a non-existent global function

In this final example using scope I will show you how not using local makes you able to use the inner function because it is not local to the block:

local function hello() 
    function hi() -- not defining it locally this time
        print("hi")
    end
    hi()
end
hello() -- have to call the function so that hi() is defined
-- the hello function works the same way it did before so we won't test that
hi() -- calling the no longer local function on the inside of the block
-- output --> hi hi

Another way to create a block would be using do end:

do
    local function hi()
        print("hi")
    end
    function hello()
        print("hello")
    end
end
hi()  -- would error
hello() -- would print hello

Ok, so that was a quick explanation of what local is for and why not everything defined by it is a variable. Now it is time to cover all the things you can put into a variable. There are three main things that variables are used for: strings, booleans, and numbers. Some examples:

local hello = "hi"
local five = 5
local notTrue = false
print(hello, five, notTrue)
-- output --> hi 5 false

I am pretty sure that you already know that stuff so I will move on to the more complex variable definitions. One thing that people do is define multiple variables at once. Here is an example of that:

local a, b = 1, 2 -- defining them both at once
print(a, b)
-- output --> 1 2

More info on that here.


You can also define a variable by calling a function that returns a value for that variable to hold. This can be done by the use of return. Example:

local function add(a, b)
    return a + b -- returning a value
end
local x = add(2, 3)
print(x)
-- output --> 5

Make sure that your function is really returning some value each time or your code will error.


Here is an example using pcall:

local success, message = pcall(function() -- defining to variables with what the function returns
    local x = 1 + nil
end
-- success is defined as a boolean determined to be true or false based on the whether or not the code in the pcall errored. message is defined as a string that is the error message if there was an error. If there was no error success would be true and message would be nil.
print(success, message)
-- output --> false (error message)

Sometimes people use ternary operators to define their variables. I will use an example from my own experience:

-- I wanted to load a player's data, but it was a table. The issue was that I needed to make sure the table existed before getting the value at that index or it would error. So, I used ternary operators:
local mainDataTable
local success, message = pcall(function()

    mainDataTable = mainDataStore:GetAsync(specialKey)

end)

if success then

    print("Player Data Loaded Successfully")

    plrInfo.Money.Value = type(mainDataTable) == "table" and mainDataTable[1] or 0
    plrInfo.BuildLevel.Value = type(mainDataTable) == "table" and mainDataTable[2]  or 1

end -- this may not be the most efficient way out there (I wrote this a while ago), but it is a perfect example

Take for example, the players money. If the mainDataTable is a table and there is a value stored at the index of one, then the player's money gets set to that value found at the index of one. If the mainDataTable is not a table the second part, mainDataTable[1] would not error because it would not be evaluated. If the first part of an and is false, it does not evaluate the second part. If either the table or a value at that index don't exist, the value of the player's money is set to 0.


Variables can also be set to mathematical expressions:

local x = 5*4+3-10/2
print(x)
-- output --> 18

I hope this answers all your questions. Have a great day scripting!

Links for reference:

0
The 'local function hi' would error with 'attempt to call a nil value (global 'hi'), not print nil User#19524 175 — 5y
0
Oh yeah. Forgot. Let me fix that. User#21908 42 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

Hello Eze, Variables are holders for a value and mostly used for Strings and Numbers. You can assign variable by using the operator "=". So it's gonna be like that VARIABLE = VALUE

Here are examples of different Variables:

x = 13 -- Assign the variable to the value
print(x) -- Printing the variable value

Output: 13

text = "Hello World"
print(text)

Output: Hello World

That's not all you can change the value any time buy using events, functions or timers

z = 5
print(z)

wait(1)

z = 8
print(z)

Output: 5 [After1Second] 8

As well as assigning a variable to another variable

b = 9
k = b
print(k)

Output: 9

Assign it to part

ballName = script.parent.Name -- Let's suppose the part's name is Ball
print(ballName)

Output: Ball

Assign it to Booleans

t = true
if t then
    print("True")
else
    print("False")
end

Output: True

I think that's all the values you can assign a variable to, If i forgot won i may edit this.

Here is a code for all the codes explained above and how would it function:

-- Defining Variables --
x = 13
text = "Hello World"
z = 5
b = 9
k = b
ballName = script.parent.Name -- Let's suppose the part's name is Ball
t = true


if t then
    print(x)
    print(text)
    print(z)
    z = 8
    print(z)
    print(k)
    print(ballName)
end

Output: 13 Hello World 5 8 9 Ball

Thanks.

1
You forgot float values. piRadians 297 — 5y
Log in to vote
-4
Answered by 5 years ago

liike definers u can put practically everything in existence into them, thanks mostly to stringvalue

0
*face palm* User#23365 30 — 5y
0
^ User#21908 42 — 5y

Answer this question