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
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!)
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
When the faster Lua VM was released, they discouraged use of next because using pairs() would return a ‘faster’ next. I believe this was updated with time as now they are now using the same ‘next’.
I know loleris uses next in some weird way on ProfileService but I don’t know how to use it. He should have shown probably how to use next in different ways.