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

The difference of "this" and "script"?

1 answer

✓ Accepted answer

this

this is a keyword in most Object-Oriented Programming (OOP) contexts. It refers to the individual instance object of a class. Lua actually has a version of this feature, called self which works in the exact same way (Lua isn't an OOP language, but it is designed to support OOP concepts). For example...

-- Dog class
local Dog = {}

-- Method of Dog class: bark
function Dog:bark()
	-- Increment number of barks within dog instance
	self.barks = self.barks + 1
	-- ...
end

In Lua, this is not a keyword. Rather, it's just an ordinary variable name. Someone may call a variable this if they're lazy, if the name of the variable doesn't matter, or perhaps if they're trying to emulate an OOP concept to make it look more familiar or conventional.

script

script is a keyword in ROBLOX's version of Lua. It refers to the actual script object in the game that the code is running inside of. For example, if you have your explorer window open with a script somewhere in your game, with this code inside of it:

script:Destroy()

...and you run this code, you will promptly see the script object in your game disappear. The script keyword is usually just used to access descendants or children that may exist in that directory.

6 comments

0

not a keyword. Keywords would be nil, for, break, etc etc

0

are you kidding me scriptguider i was writing a response before you even got to this page i hate you so much

1

It's debateable what the strict definition of "keyword" is, and by extension whether or not script is a keyword or just a built-in variable or whatever, but to argue this point is splitting hairs.

0

@MCAndRobloxUnited, that's just wrong. this exists in many other languages too, including JavaScript, C# and C++.

0

The definition of "keyword" is unambiguous; they are reserved words that cannot be used as identifiers. The keywords in Roblox's Lua are the same as those in Lua 5: and, break, do, else, elseif, end, false, for, function, if, in, local, nil, not, or, repeat, return, then, true, until, and while.

1

I personally just think about keywords as words that you shouldn't use as identifiers. The "true" definition may put emphasis on which you CAN'T use, but I find that it's good practice to group what you CAN'T do and what you SHOULDN'T do as the same thing.

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