Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Can someone explain how variable storage really works?

So, I've been going back to the basics in an attempt to really learn Lua, and I'm confused on something. The following code stores a variable for myPart:

myPart = Instance.new("Part", workspace)

However,r it also creates the part in the workspace. Can someone explain why this is? I'm confused, because I thought that the variable was simply supposed to store the part, not create it. Thanks.

2 answers

✓ Accepted answer

Well here we go again. So I totally get what your thinking here, "that how can a variable stand for things like functions, when on the wiki, I learned that they can only stand for numbers, strings, UserData, etc?"

And I have the answer! First off, the wiki page gives a great description of how variables work (variables, but it can be confusing sometimes).

--Here's a great description for variables to start off with: "Variables are a set of symbols (such as numbers and letters) that stand for a string, number/set of numbers, or userdata.

Ok, so we know that, but how do things like functions and the Instance.new() statement?

--If you don't already know what functions are, here's a great link --> http://wiki.roblox.com/index.php?title=Function.

--Specifically, a pre - difened can do two things, one a it will change certain objects properties (e.x. Destroy() will set that objects properties to nil), or two will return a value (e.x. GetChildren() returns all the children of an object). So basically, this type of function will always output the data from the outcome (if the outcome has added, destroyed, changed, or specified a piece of data)

The only way we can grab the data that has been outputted from that function, is from a variable (e.x. Part = Instance.new("Part") will add a new part, because the outcome of that function is a new part).

I you need any more clarification, feel free to ask questions!

Yes you're right, but the second argument of the function state the parent.

local Part = Instance.new('Part') --> Just store the part

local Part = Instance.new('Part', Workspace) --> Store the part, plus parent it to workspace

Aka what ever the second argument is, will be the parent.

This discussion is preserved as it was. New answers, comments, and votes are closed.