Last active
April 10, 2026 22:48
-
-
Save edutrul/454eba856debdc7bfc3c435f80d736e7 to your computer and use it in GitHub Desktop.
Script sample to learn luau
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local sayHello = function () | |
| print("Say something") | |
| end | |
| print (sayHello()) | |
| print((function () | |
| print('gosh') | |
| end | |
| )() ) | |
| local result = (function (a: number, b: number) | |
| return a + b | |
| end | |
| )(5, 3) | |
| print(result) | |
| -- [[ | |
| Hola soy Eduardo | |
| Evento manual: Saludo | |
| Eduardo | |
| Say something | |
| gosh | |
| 8 | |
| -- ]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local names: {string} = {'Eduardo', 'Amy', 'Ale'} | |
| print (names[1]) | |
| for i = 1, #names do | |
| print(i, names[i]) | |
| end | |
| --[[ | |
| Eduardo | |
| 1 Eduardo | |
| 2 Amy | |
| 3 Ale | |
| ]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local age = 20 | |
| local hasID = true | |
| if age >= 18 and hasID then | |
| print("Puede entrar") | |
| else | |
| print("No puede entrar") | |
| end | |
| -- Puede entrar -- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Person = { | |
| name: string, | |
| move: (self: Person, distance: number) -> (), | |
| jump: (self: Person, distance: number) -> (), | |
| swim: (distance: number) -> (), | |
| dance: (self: Person, distance: number) -> () | |
| } | |
| local Person = {} | |
| Person.__index = Person | |
| function Person.new(name: string): Person | |
| local self = setmetatable({}, Person) :: Person | |
| self.name = name | |
| return self | |
| end | |
| -- method -- | |
| -- which can or the same as Person.move(self: Person, distance: number) | |
| function Person:move(distance: number) | |
| print(self.name, " moved ", distance) | |
| end | |
| -- method -- | |
| -- which can or the same as Person.jump(self: Person, distance: number) | |
| function Person:jump(distance: number) | |
| print(self.name, "jumped", distance) | |
| end | |
| -- a function -- | |
| function Person.swim(distance: number) | |
| print('Swimmed', distance) | |
| end | |
| -- a function that expects self manually -- | |
| function Person.dance(self: Person, distance: number) | |
| print(self.name, "danced", distance) | |
| end | |
| local person = Person.new("Eduardo") | |
| person:move(10) | |
| person.move(person, 10) | |
| person:jump(20) | |
| person.swim(30) | |
| person.swim(30) | |
| person:dance(50) | |
| --[[ | |
| Eduardo moved 10 | |
| Eduardo moved 10 | |
| Eduardo jumped 20 | |
| Swimmed 30 | |
| Swimmed 30 | |
| Eduardo danced 50 | |
| --]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- In luau arrays [] are with {} omg -- | |
| local names: {string} = {'Eduardo', 'Amy', 'Ale'} | |
| print (names) | |
| print (names[1]) | |
| -- In luau arrays start with index 1 so this prints Eduardo -- | |
| for index, value in ipairs(names) do | |
| print (index, value) | |
| end | |
| --[[ | |
| Running main.luau... | |
| { | |
| "Eduardo", | |
| "Amy", | |
| "Ale", | |
| } | |
| Eduardo | |
| 1 Eduardo | |
| 2 Amy | |
| 3 Ale | |
| ]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Person = { | |
| name: string, | |
| age: number, | |
| greet: (self: Person) -> () | |
| } | |
| local Person = {} | |
| Person.__index = Person | |
| function Person.new(name: string, age: number): Person | |
| local self: Person = setmetatable({}, Person) :: Person | |
| self.name = name | |
| self.age = age | |
| return self | |
| end | |
| function Person.greet(self: Person) | |
| print ('Hola soy ' .. self.name) | |
| end | |
| local person = Person.new('Eduardo', 34) | |
| person:greet() | |
| -- Hola soy Eduardo -- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local t = {"Eduardo", "Amy"} | |
| t.role = "dev" | |
| print("ipairs:") | |
| for i, v in ipairs(t) do | |
| print(i, v) | |
| end | |
| print("pairs:") | |
| for k, v in pairs(t) do | |
| print(k, v) | |
| end | |
| --[[ | |
| ipairs: | |
| 1 Eduardo | |
| 2 Amy | |
| pairs: | |
| 1 Eduardo | |
| 2 Amy | |
| role dev | |
| --]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Listener<T...> = (T...) -> () | |
| type Signal<T...> = { | |
| listeners: {Listener<T...>}, | |
| Connect: (self: Signal<T...>, callback: Listener<T...>) -> (), | |
| Fire: (self: Signal<T...>, T...) -> () | |
| } | |
| local Signal = {} | |
| Signal.__index = Signal | |
| function Signal.new<T...>(): Signal<T...> | |
| local self = setmetatable({}, Signal) | |
| self.listeners = {} | |
| return self :: any | |
| end | |
| function Signal.Connect<T...>(self: Signal<T...>, callback: Listener<T...>) | |
| table.insert(self.listeners, callback) | |
| end | |
| function Signal.Fire<T...>(self: Signal<T...>, ...) | |
| for _, callback in ipairs(self.listeners) do | |
| callback(...) | |
| end | |
| end | |
| type Person = { | |
| name: string, | |
| age: number, | |
| Greeted: Signal<Person>, | |
| greet: (self: Person) -> () | |
| } | |
| local Person = {} | |
| Person.__index = Person | |
| function Person.new(name: string, age: number): Person | |
| local self = setmetatable({}, Person) :: any | |
| self.name = name | |
| self.age = age | |
| self.Greeted = Signal.new() | |
| return self | |
| end | |
| function Person.greet(self: Person) | |
| print("Hola soy " .. self.name) | |
| self.Greeted:Fire(self) | |
| end | |
| local person = Person.new("Eduardo", 34) | |
| print(person.age) | |
| print(person.Greeted) | |
| person.Greeted:Connect(function(p) | |
| print("Se disparó el evento para", p.name) | |
| end) | |
| local anonymous_function = function (person: Person) | |
| print("Se disparó el evento para", person.name) | |
| end | |
| person.Greeted.Connect(person.Greeted, anonymous_function) | |
| person:greet() | |
| --[[ | |
| 34 | |
| { | |
| listeners = {}, | |
| } | |
| Hola soy Eduardo | |
| Se disparó el evento para Eduardo | |
| Se disparó el evento para Eduardo | |
| --]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local firstname: string = 'Eduardo' | |
| print (firstname) | |
| -- Eduardo -- | |
| local function sayHello(name: string): string | |
| return 'Hello world' .. name | |
| end | |
| print(sayHello(firstname)) | |
| -- Hello world Eduardo -- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local t = {} | |
| table.insert(t, 10) | |
| table.insert(t, 20) | |
| print(t) | |
| print(#t) | |
| t[#t +1] = 30 | |
| print(t[#t]) | |
| --[[ | |
| Running main.luau... | |
| { | |
| 10, | |
| 20, | |
| } | |
| 2 | |
| 30 | |
| --]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Names = {string} | |
| local names: Names = {'Axel', 'Amy', 'Ambar'} | |
| print(names[1]) | |
| for _, name in ipairs(names) do | |
| print(name) | |
| end | |
| --[[ | |
| Axel | |
| Axel | |
| Amy | |
| Ambar | |
| --]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Person = { | |
| name: string, | |
| age: number | |
| } | |
| local person: Person = { | |
| name = "Eduardo", | |
| age = 34 | |
| } | |
| print (person.name) | |
| -- Eduardo -- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment