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

The difference of "this" and "script"?

Asked by
SCP774 191
5 years ago

The title explains all, I'm confused with the usage of "this".

1
While 'this' may mean something in OOP in other programming languages, 'this' doesn't mean anything in Lua. User#24403 69 — 5y
0
Oh so they are the same SCP774 191 — 5y
0
You can read this wikipedia article https://en.wikipedia.org/wiki/Object-oriented_programming User#24403 69 — 5y

1 answer

Log in to vote
3
Answered by 5 years ago

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.

0
not a keyword. Keywords would be nil, for, break, etc etc User#24403 69 — 5y
0
are you kidding me scriptguider i was writing a response before you even got to this page i hate you so much Fifkee 2017 — 5y
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. fredfishy 833 — 5y
0
@MCAndRobloxUnited, that's just wrong. `this` exists in many other languages too, including JavaScript, C# and C++. fredfishy 833 — 5y
View all comments (2 more)
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. BenSBk 781 — 5y
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. ScriptGuider 5640 — 5y
Ad

Answer this question