Categories > Coding > Lua >

[turotial] bad lua habits and hwo 2 write good lua


New Reply

RealNickk

Nicholas (Nick)

vip

Posts: 3862

Threads: 172

Joined: Feb, 2020

Reputation: 47

  • 0

Posted

I wrote this off of sleep deprevation and four cups of instant coffee. Don't expect this to be a good tutorial.

1. namig convection

ok first of all learn ur freaking naming conventions im dead serious if ur following funny conventions ur code will look bad. theres no official lua naming convention but i made up my own by following roblox api's convention, with OOP (colon) functions as pascal case (like game:GetService) and static (period) functions as camel case (like Vector3.new), and for variables i stick with camel case (although i will use pascal case if it's either a global variable or if the variable is defined in the main chunk). for table members i generally do pascal case for public members and camel case sometimes prepending an underscore for private members but really all u have to do is come up with a convention that isnt aids or follow mine (i recommend u follow mine) then commit to it and ur code wont look bad

 

2. usign oop calls (":" calls) when unnecessary

some of you guys are idiots and make everything be called by ":" when not needed like bruh. YOU SHOULD ONLY EVER DEFINE AN OOP FUNCTION WHEN YOU INTEND ON USING "self" or you're literally just going to make your code have to run 2000 more cpu instructions for no reason. define it as a regular func and call it with "." if u dont intend on using the "self" variable.

 

3. not kleening up konnections

another thing is some of you idiots have a bad habit of cleaning up connections. when you connect to a rbxscriptsignal it allocates memory natively and when you dont disconnect it, it stays in memory, hence memory leak!!!1!1! BAD MEMORY LEAK!111!1 only time you dont disconnect is when you know that the object will be destroyed with ":Destroy()", NOT PART.PARENT = NIL THAT DOESNT MEAN IT DESTROYS IT! FOR EXAMPLE you should disconnect Players.PlayerAdded when you wont use it again but you dont need to disconnect Player.CharacterAdded because when the player leaves itlll get destroyed, inevitably disconnecting anyways

 

4. using deprecated crap

if you use wait i hate you, wait is slow and it can throttle (wait infinitely). use task.wait. same with spawn, delay, etc. the task library has functions that run on 60hz pipeline instead of 30hz meaning its more accurate and it wont throttle meaning no infinite yields on bad computers. also people like to use deprecated fields like "instance.className" WHICH IS DEPRECATED. IF IT IS DEPRECATED DONT USE THE FREAKING THING... INSTEAD OF className DO ClassName, SAME WITH GAME:SERVICE DO GAME:GETSERVICE

 

5. nested if statements

nested if statements are hell and you know that. you can easily write code that doesn't have them by simply reversing your expression, and with Luau's "continue" keyword you have no excuse to not do it. ALSO PLEASE INDENT YOUR CODE CORRECTLY ITS NOT HARD :CRY:

-- good
local x, y, z = true, 100, 200
if not x then return false; end
if y ~= 100 then return false; end
if z ~= 200 then return false; end
-- do stuff
return true

-- bad
local x, y, z = true, 100, 200
if x then -- indentation hell
    if y == 100 then
        if z == 200 then
            -- do stuff
        end
    end
end
return false

 

6. not discardig variables

smh if ur not gonna use a variable for example like in a pairs loop then REPLACE THE VARIABLE NAME WITH AN UNDERSCORE SO IT MAKES A LITTLE MORE SENSE!

 

7. not understanding how pemdas works

bro if you cant use pemdas you shouldnt ever program. i see so many examples of code with parentheses in a literal bucket of hell that i want to cry myself to sleep so please understand how pemdas works when doing math

 

8. adding to infinity

you're a true idiot if you add onto math.huge. infinite is infinite and you can't change that so stop doing math.huge * math.huge thinking that its bigger than math.huge because its EQUAL

 

9. not using metatabls

metatables are really hot and no one uses them, for example when you set the metatable of a table to { __mode = "k" } that means when all references to the key in a table are gone, the garbage collector will remove itself from the table (sometimes, you'd have to study how the GC works), essentially meaning you dont have to wtite 90 more lines of code to handle cleanup because the garbage collector takes care of it for you. __mode is the same for things like values "v" or both keys and values "kv". Garbo collektor can collect things like strings, threads, lua function objects, numbers, booleans, etc. but not things like rbxscriptconnections

 

also __index is really hot for things like class hierarchy which is really important for good code structure. it's hell but when you understand how to use certain metamethods, you'll save yourself from writing hundreds of lines of code and maybe even optimizing your code

 

10. poop variable names

i use one letter var names sometimes but not all the time. i only use it when it's self-explainitory. i would always name ur variables like "someUsefulName" and not "fortniteBalls"

 

ok go write better code please

https://github.com/RealNickk

check out my github i make stuff

Posts: 26

Threads: 0

Joined: Jul, 2021

Reputation: 16

  • 0

Replied

yes.

but

you no

answer

my dms

;d

 

https://cdn.discordapp.com/attachments/1022610733520912436/1022957841214083214/sigg2.png

 

Thanks for the rep: Swiney, Byoke, Immune, Locust, Waves, Weeb, Nickk, darkn, Atari, CubeFaces, and Lux14.

RealNickk

Nicholas (Nick)

vip Mention

Posts: 3862

Threads: 172

Joined: Feb, 2020

Reputation: 47

  • 0

Replied

@Murz your dms are disabled

https://github.com/RealNickk

check out my github i make stuff

Posts: 911

Threads: 81

Joined: Apr, 2022

Reputation: 40

  • 0

Replied

vouch vouch vouch

Posts: 26

Threads: 0

Joined: Jul, 2021

Reputation: 16

  • 0

Replied

@RealNickk 0-0 mb i have fixed it

 

https://cdn.discordapp.com/attachments/1022610733520912436/1022957841214083214/sigg2.png

 

Thanks for the rep: Swiney, Byoke, Immune, Locust, Waves, Weeb, Nickk, darkn, Atari, CubeFaces, and Lux14.

ThatPhoenix

Dopemine Addict

Mention

Posts: 2843

Threads: 61

Joined: May, 2020

Reputation: 22

  • 0

Replied

no, all my varibles are still gonna be forniteBalls

https://cdn.discordapp.com/attachments/661621789591470090/1013924765892890774/Untitled_1366_768_px_1546_202_px_3.gif

Join Vector 

Phoenix#8419 | JavaScript & C# | Learning C/C++

eb_

Formally known as Shade.

vip Mention

Posts: 1591

Threads: 108

Joined: Jun, 2020

Reputation: 35

  • 0

Replied

https://cdn.discordapp.com/attachments/1013432227830435851/1013434658924535848/SwitchCases.jpg

RealNickk

Nicholas (Nick)

vip Mention

Posts: 3862

Threads: 172

Joined: Feb, 2020

Reputation: 47

  • 0

Replied

@eb_ haha

https://github.com/RealNickk

check out my github i make stuff

Posts: 31

Threads: 8

Joined: Apr, 2018

Reputation: 0

  • 0

Replied

I'm extremely guilty of #4 & #5

For #4, I didn't knew task.wait is a thing in LUA as well (I only knew about this in C#), guess I'll have to update some of my scripts for this, though I'll keep using Mouse Events, Lasso Instance & HandleAdornments unless Roblox decides to remove those deprecated things completely

For #5, it was the opposite for me in my opinion, it'll also depend on how or what method/context of the script you're making, the former is good and optimal (I use it in C# very often), but I use the latter where there are cases that you can't (or shouldn't) use return method in it and I use the former sparingly in LUA, and indentation wasn't that bad (for me, if that's the case) since it helps me organize whatever I want to sort and categorize some functions I made in the script

­­­

Posts: 358

Threads: 37

Joined: Feb, 2021

Reputation: 7

  • 0

Replied

* Use RunService.RenderStepped:Connect() instead of while true do for loops, Or If there is a better loop method that I don't know of.

https://media.discordapp.net/attachments/994643402949926956/1004560140252495960/uqJXQIda.gif

Read me.

Discord: Ad#1234 ; Don't hesitate to DM me if you need help/anything.

RealNickk

Nicholas (Nick)

vip Mention

Posts: 3862

Threads: 172

Joined: Feb, 2020

Reputation: 47

  • 0

Replied

@AlexSyndrome And use BindToRenderStepped for more managed connections

https://github.com/RealNickk

check out my github i make stuff

Posts: 358

Threads: 37

Joined: Feb, 2021

Reputation: 7

  • 0

Replied

https://media.discordapp.net/attachments/994643402949926956/1004560140252495960/uqJXQIda.gif

Read me.

Discord: Ad#1234 ; Don't hesitate to DM me if you need help/anything.

mwc94087

i write actual crap

Mention

Posts: 92

Threads: 4

Joined: Jun, 2022

Reputation: 2

  • 2

Replied

space_that_iWORKIN_XD = game:Service("Workspace")
MY_FUNCtionzz = {}

function MY_FUNCtionzz:DEleteHUOOMANOIDE()
     hooman = workspace
     for hhhhh, idklol in pairs(game.FindFirstChild(getfenv()["game"], "Players").LocalPlayer.Character:GetChildren()) do
          if idklol.className == "Humanoid" then
               if idklol:IsA("Humanoid") then
                    if idklol ~= nil then
                         hooman = idklol
                         hooman.Remove(hooman)
                         hooman = nil
                    end
          end
     end
end

function MY_FUNCtionzz:AUNTYRESPAWN()
     fhbryggergbekjbf = Instance["NEW":lower()]("Animation")
     fhbryggergbekjbf["AnIMAtioNid":lower()] = [[rbxassetid://148840371]]:lower()
     e = game.Players.LocalPlayer:FindFirstChildOfClass('Humanoid'):LoadAnimation(fhbryggergbekjbf)
     getfenv()["e"]:Play(.1, 1, 1)
     wait(0.7)
     yourmom = game.Players.LocalPlayer.Character
     game.Players.LocalPlayer.Character = nil
     wait(0.000000000000000000000000000000000000000000000000000000001)
     game.Players.LocalPlayer.Character = Instance[string.lower(string.upper(string.lower(string.upper("NEW"))))]("HopperBin", space_that_iWORKIN_XD)
     wait()
     game.Players.LocalPlayer.Character = nil
     wait(1)
     game.Players.LocalPlayer.Character = game
     wait(0.69)
     game.Players.LocalPLayer.Character = yourmom
     wait(0x98989967854387*0b01010101010011001010101101001011001010*763584263548356243284658634257632748234762365285628)
end

function SPAMINGTHECHAT()
     real_nick = "fortniteBalls:Destroy()"
     game:GetService("RunService").Heartbeat:Connect(function()
          for ljns = 0.59, (math.huge * (1010302 * 439)) / (345 ^ (732 % 32)) * math.huge * (((math.pow(math.huge, 42)))) do
               game.ReplicatedStorage.SayMessageRequest:FireServer(real_nick)
          end
     end)
end

-- 9 No b*t wait gotta keep it family friendly
-- 9 No metatables

possibly a

RealNickk

Nicholas (Nick)

vip Mention

Posts: 3862

Threads: 172

Joined: Feb, 2020

Reputation: 47

  • 0

Replied

@mwc94087 I'll kill you.

https://github.com/RealNickk

check out my github i make stuff

Kura

kuraise

Mention

Posts: 181

Threads: 3

Joined: Jun, 2021

Reputation: 5

  • 0

Replied

@mwc94087 i need bleach for my eyes

 

https://cdn.discordapp.com/attachments/877610317037858846/975469974405673041/Signature.png


New Reply

Users viewing this thread:


( Members: 0, Guests: 1, Total: 1 )