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

What does parent and functions and local do?

Asked by 8 years ago

what does parent do and what does functions do and local ?

3 answers

Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

Parent

The 'Parent' of an object represents what it's inside of. You can compare the literal meaning of the word to it's definition in this sense, because it means the same thing. If you have Part2 stored inside Part1, then Part1 is Part2's parent. You can even take it one step further and say that Part2 is Part1's child. Here's a more abstract example:

local Part1 = {
    Part2
}

In the above example, you can see that Part2 is stored inside Part1, making Part1, Part2's parent!

Functions

Functions are blocks of code that can be used to repeat the same instructions over and over again - without needing to re-type any of the code. This works by giving the functions a name, and then referencing that name and calling it whenever you want to use it's code. Just like a command. Here's an example:

-- This code won't execute until we call the function "func"
-- in this case, "func" is the name of the function
local function func()
    print("This function was called!")
end

func() -- This will run the code inside the function "func"
func() -- This runs it again! It runs the same code without us needing to re-type any of it.

For a more abstract example of creating a function, you could even write it with it's variable name first, and the actual function value after. To get a better idea:

-- This is the exact same thing
local func = function()
    print("This function was called!")
end

func() -- Still called normally

This is a very basic description of how functions work, and doesn't even scratch the surface of their true potential and capabilities. If you want to know more about them, I go more in depth here.

Locals

Before you know what a local variable is, you should know what a scope is. A scope is pretty much any block of code that requires an 'end' statement when it's done, to let the program know it's a separate environment for local variables.

That brings us to local variables! Locals only exist inside their current scope. If anything tries to access a local variable from a different scope, it's just going to return nil (or in other languages, it'll just break). We can use a function as an example, since it creates a new scope!

-- Everything out here is considered the "global scope"
-- and anything will be able to access it.

-- Function creating a new scope
local function func()
    local x = 1 -- Making the variable local to this scope
    print(x) -- this will print 1, becuase the print statement is inside the same scope as the local 'x' variable
end

func() -- Execute the function
print(x) -- This will print nil, because this is trying to access 'x' from a different scope.

This could even be better demonstrated with the 'do-end' statement. This statement is used as an extension for some loops, but used by itself it can be used just to create a new scope. Here's an example:

do
    local x = 1 -- Declare 1 in this scope
    print(x) -- print x (in the same scope)
end

print(x) -- trying to print 'x' outside of the scope above, so it will print nil.

There are a lot more sources out there that go a lot more in detail about these subjects, and I highly suggest you check them out, as this was probably a lot to take in all at once.

Personally, I strongly recommend you start from the absolute basics and learn the fundamental nature of programming. It will help you way more than trying to learn random specific details all at once. Just let me know if you have any questions, I'll be glad to help.

Ad
Log in to vote
0
Answered by
Async_io 908 Moderation Voter
8 years ago
Edited 8 years ago

I'm assuming you're new to scripting, and the first thing to know if you're new to scripting is it takes time. Just like everything else. So don't give up, complain, get impatient, etc, because scripting is a learning process. (Not saying you were being any of these, this is just for future usage.)

Parents

In Roblox's Simplified Lua, a parent means the thing above the current script/part/etc.

In the Explorer, there's a system that uses Parents and Children, which is an easier way of explaining a part and it's owner. It's important to note, if you didn't know already, when looking for a part in Roblox using a script, you'll need to use game.Workspace as this looks in the Workspace for what you're wanting to look at. There's other things such as ReplicatedStorage, StarterGui, and etc, and it would be the same for these. game.ReplicatedStorage would look for a part in ReplicatedStorage. game.StarterGui would look for something in the StarterGui, and etc.

For instance, say you were looking at a script located in a part, that was located in a model. You could use script.Parent and that would get the Part, you could also use script.Parent.Parent and get the model.

You could get a child by using the simple code, script.Parent.Script. This would look for the script we're typing in, assuming that you haven't renamed it.

When looking for children, you need to specify their exact name. Say I had a part named Part1 in the Workspace. I could not use game.Workspace.part1 because the part is named Part1, not part1, however using game.Workspace.Part1 would work, because Part1 = Part1.

Functions

Functions are extremely useful to every scripter. The easiest way to explain it, is a line of code that you can run on command.

Say I was trying to print a paragraph. Rather than typing out print("paragraphgoeshere") over and over again, I'm going to use a function so that I can just use the function over and over again.

function par() --Functions can be named whatever, except for a few words like wait(), tick(), etc.
print("As a describer of life and manners, he must be allowed to stand perhaps the first of the first rank. His humour, which, as Steele observes, is peculiar to himself, is so happily diffused as to give the grace of novelty to domestic scenes and daily occurrences. He never o'ersteps the modesty of nature, nor raises merriment or wonder by the violation of truth. His figures neither divert by distortion nor amaze by aggravation. He copies life with so much fidelity that he can be hardly said to invent; yet his exhibitions have an air so much original, that it is difficult to suppose them not merely the product of imagination.")
end --Every function needs to end with an end.

par() -- Runs the function par

It's better to use functions whenever you can, just so that it's less work on you, and scripting becomes more enjoyable.

There are different types of functions that will be called to action whenever something happens. For instance, say whenever someone died, you wanted to give them +10 WalkSpeed.

local player = game.Players.LocalPlayer
repeat wait() until player.Character
local character = player.Character
local humanoid = character:WaitForChild('Humanoid')

humanoid.Died:connect(function() --This will run the chunk of code below when the person dies.
humanoid.WalkSpeed = humanoid.WalkSpeed + 10
end) --It's important to end using an end) when using functions like this.

This chunk of code would run every time the person died.

Locals

Locals are extremely important when it comes to Lua. In Lua, there are multiple types of Locals, but for today, I'm only going to explain variables.

When scripting, it's easier to go ahead and tell the script, I'm going to use this over and over again, so here's a simplified way of reading it so I don't have to type it over and over again. This is basically what variables are. Variables can be anything, strings, parents, children, parts, people, you name it, it'll be variable'd. Variables can have any name to them, so long as they aren't named important words like wait, end, etc.

local var = "Variable" --This is a variable known as 'var'. 
print(var) --We are going to print what the script knows as var, which is 'Variable'.
local var = script.Parent --This is making var whatever the script's parent is.
print(var.Name) --This will print the name of the part attached to Var.

Variables are probably one of the most important things for scripters, as variables help out a lot. They save time for both your fingers and yourself, prevent scripts from running over and over again, and could save the lives of dozens of players. All with one little variable.

Helpful Links

There is an amazing blogpost from a scripter known as Perci1. This will teach you the basics of scripting.

There are dozens of tutorials made by an amazing scripter known as ScriptGuider, which can help you out with the more advanced things.

The wiki is probably the best place to learn things about Roblox Scripting.

This is probably one of the best problem-solving websites you could find. If you ever need help there's a live chat going on with other scripters that can either become your friends, or help you learn dozens of new things.

This one blog post alone, could save you hours of time, hairpulling, and screaming, all thanks to M39a9am3R.

This is an extremely important post made by evaera, which again, could save you from wasted time, hairpulling, and screaming.

A vital blogpost that will help you in the longrun, posted by evaera.

Few more things

Local scripts cannot work in Workspace.

ScreenGuis do not work in Workspace.

Scripting takes time, practice, and patience.

LocalPlayer is only usable in LocalScripts.

ExampleScript

All of the elements we just talked about, are being used in this script.

local mo = 'MoHealth'
local player = game.Players
repeat wait() until player.Character
local char = player.Character
local humanoid = character:WaitForChild('Humanoid')

humanoid.Died:connect(function()
print(mo)
humanoid.MaxHealth = humanoid.MaxHealth + 10
humanoid.Health = humanoid.Health + 10
print(player.Name..' now has '..humanoid.MaxHealth..' health.')
end)
Log in to vote
-2
Answered by 8 years ago
Edited 8 years ago

Well...

To give you a brief explanation, Parent can be anything in Roblox Lua, if it has something inside of it. So if a ScreenGui had a Frame inside of it, then the ScreenGui would be considered a Parent. Functions is a whole giant of its own, I'll give you a link at the end. Local allows you to call an object by a different name, such as ScreenGui being called 'Gui', such as below

local Gui = game.StarterGui.ScreenGui--This changes the name of ScreenGui to Gui, and is easier to access rather than writing a whole line of code to access it.

Based On Your Question...

You seem new to scripting, and there are multiple tutorials out there that can help you learn more about it, I'll link you what works for me and allowed me to get to scripting to even this day!

Links

http://wiki.roblox.com/index.php?title=Intro_to_Scripting

https://www.youtube.com/playlist?list=PLA4609EA2E5307C94

http://wiki.roblox.com/index.php?title=Function

There are so many out there, but these helped me the most, so good luck, and welcome to the world of scripting!

Greek

Answer this question