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

Why Does This Code Work When Its Not A Function Of The Variable?

local a = "hello"

print(a.upper("Sup")) -- Prints ("SUP")

My question is, how does this work, it puts the "upper" function to the variable "a" but how? and why does this work?

1 answer

✓ Accepted answer

a is an Instance of the String base-class. Thus, it will inherit the functions belonging to it (indexable by the dot operator). If you wish to call the methods of the String Object, you must use a colon prefix.

("").upper("Hello world!") --> HELLO WORLD

--// Interpreted as:

string.upper("Hello world!") --> HELLO WORLD

####If you wish to understand more about this underlying model, research OOP (Object-oriented Programming).

1 comment

0

Thank you this helped a lot

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