Coding things you probably didn't know (part 1)

Aahhh this is soo cool tysm, i want more things like that

2 Likes

i might make a part two lol when i learn more uncommon stuff

1 Like

I wonder why does shared exist, if _G already did…

But hey, here’s more coding things that you probably didn’t know about, with good grammar as well!

Roblox Globals
Lua Globals (roblox.com)

2 Likes

sorry that my grammar sucks its not my best subject

2 Likes

next

Please don’t write loops this way, it’s unreadable. I know that pairs implements next (unless it doesn’t in Luau) but you should just use one of the proper generators when iterating; ipairs for contiguous arrays where ordering might be important and pairs for dictionaries.

Fake ternary (and & or)

Same as the above: be careful when you’re using this. There are some cases where developers may get “unexpected results” but that’s just a case of using this wrong. Luau doesn’t have a ternary operator so this is the closest that can be done without being actual ternary, this is just taking advantage of the way operators evaluate.

shared (or _G)

Good call: please never use either of these, opt for ModuleScripts. There are very exceptional cases where you need to use the global table otherwise try not to use it.

Dictionaries (“too long…”)

Each of these code samples has a minor problem.

  • Please use PascalCase when referring to properties. Team and Name should have a capital T and N respectively. The lowercase variants are deprecated aliases.

  • The second sample doesn’t work the way you think it does. You’ve created an array of teams but the indices of these are all numerical. You’re attempting to access this table via the indice of the player’s team name which will be nil. For this to work, the indices need to be strings and the value needs to be non-nil. See example [1] for a fixed version.

  • The third sample also will not work the way you intend because “White” is not a property of “Name”. You’d need to use the == operator (is equal to) for this instead. See example [2].

[1]:

local onlyTeams = {
    ["Green"] = true,
    -- same for the rest of the teams
}

if onlyTeams[player.Team.Name] then
    -- Commit action
end

* table.find is acceptable as well, but you need to be aware of what value types you’re including in the table. The original organisation of the table is invalid since a player cannot be on multiple teams at once.

[2]:

if not (player.Team.Name == "White") then
    -- Commit action
end

* The parenthesis are required to define what you’re checking which then the not operator flips the result of the evaluation in the parenthesis. Alternatively, you can use the ~= (not equal to) operator and avoid using not. A common mistake is not to include these in parenthesis; the not would only act on the left-hand operand not the whole evaluation.

if player.Team.Name ~= "White" then
    -- Commit action
end
9 Likes

whoops agree on the last one gonna fix it also thx for feedback and what is

never new that you needed to do that thank you

this is literally the best post here

local ThisIsPascalCase
local thisIsCamelCase
3 Likes

In roblox we never use it, but there is also

local this_is_snake_case
local this-is-kebab-case
3 Likes

I don’t think Lua likes kebab case. It sees it as subtraction.

3 Likes

oof, didn’t know that!
I mainly use kebab in other languages but I thought lua could handle that

That’s some pretty good stuff. Well done :+1:t2:

1 Like

Whoa, thank you so much. I was looking for the definition of continue for so long.

Also, number 5. isn’t really a thing people don’t know, rather a thing that beginner scripters can’t really utilize… (it’s fine though, they’re learning!)

1 Like

A SMALL RANT: avoid _G variables AS MUCH AS POSSIBLE and use shared instead as that might have conseuqences in ur games optimization

2 Likes

yup you gotta use them module scripts

1 Like

Why? They are technically the same thing under different names. They work exactly the same and even the API documentation tells you that:

Both of them are equally bad or good.

2 Likes

its in my my tutorial too. but either way you should use module scripts

You probably didn’t know, but when math.random has no arguments, it returns a number in the range of [0, 1], and if it has 1 argument number N, it returns a number in the range of [1, N]. You could shorten

local randomBool = math.random(1, 2) == 2 and true or false

to

local randomBool = math.random(2) == 2;

or

local randomBool = math.random() == 1;
1 Like

Here’s an interesting thing many people don’t know:

do
   --code
end

will execute the code inside (once).

1 Like