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

HI DEVS,

first sorry if the tittle sucks couldn’t think of a better one also this is my first tutorial so have mercy also in this tutorial i will tell you things very little coders know about.

1. continue

do you know what breaks are right they break the loop. well continue almost does the same thing except instead of breaking the loop it skips an iteration for example:

for i, v in pairs({1,2,3,4,5,6,7,8,9,0}) do
 if v == 7 then
  continue -- skips iteration
 end
 print(v)-- prints v or value of table
end
--the output would look like:
1
2
3
4
5
6-- as you can see it skips six to 8
8
9
0

note. that continue does not have parentheses or ().

2. next

we all know what for loops are but did you know you could write them in a different way? here is the more used version:

for i, v in pairs({"lol", true}) do

end

here’s the less used version:

for i,v next, {"give","me","feed","back"}, nil do-- after the `next,` you put your table
 -- your stuff here
end

note that next does not need parentheses or () also the nil is not needed.

3.and & or

yes i know you probably know what and & or are but did you know you could put them in variables. you probably seen it but probably never understood it. here’s an example on what a variable looks like with ors & ands:

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

the random bool will be equal true if math.random(1, 2) == 2 that’s why there’s an and. if you wanted randomBool to be false when math.random(1, 2) == 2, you would add false after the and.

But wait… how about the or what’s that for well if math.random(1, 2) == 1 we need randomBool to be equal to something that’s wen or comes in, if the first part isn’t true then randomBool will be equal what ever value comes after the or.

4. shared

ever heard sharing is caring well we aren’t learning about that were learning about shared. here is what shared does that I didn’t steal from the devhub.
A table that is shared across all scripts that share This serves the exact same purpose as _G . here is an example how you would use it:

-- some script
shared.RandomTable = {"i", "have",-1,"brain","cell"}-- very random table i made
-- some other script
for i, v in next, shared.RandomTable, nil do
 print(v)-- i have -1 brain cell
end

note. use module scripts please

5 too long…

Have you ever have that long if statement for example:

if player.team.name=="Green" or player.team.name=="Blue" or player.team.name=="Red" or player.team.name=="Pink" or player.team.name=="black" then
 -- see how long it is
end
local onlyTeams = {
["Green"] = true,
["Blue"] = true,
["Red"] = true,
["Pink"] = true,
["black"] = true
}
if onlyTeam[player.Team.name] then
 see how short it is
end
if not (player.Team.name == "white") do
 -- you can do this too
end
-- note this is just an example
156 Likes

Now that’s a post worth reading
Thank you so much
I kinda understand what we use _G for and another way to use if statement

11 Likes

can you imagine that i was going to almost give up in the middle

3 Likes

what is this for???

2 Likes

but what is this for???

1 Like

bro i am just asking i am not mad plz dont be sad

hehe no worries i meant no offence
my words don’t contain any sarcasm
sorry it confused you
i am not sad no worries

1 Like

The things that helped me were continue.

_G is especially for global functions, where you can access things from other scripts if you set them.

and & or can be used like this:

animator = script.Parent:FindFirstChild("Humanoid") or script.Parent:FindFirstChild("AnimationController")
3 Likes

and & or can also be used to make a type of if statements

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

Fun fact there is also the global table “shared” that even less people know of. Also I reccomend you fix some spelling errors. Also don’t use for I,v in next, table, don’t do that.

3 Likes

i didnt know that mind if i can use it in my tutorial and sorry for my spelling

1 Like

Great tutorial but I have some critiques:

  • Your code uses properties that don’t exist, such as Player.team, where it should be Player.Team.
  • You don’t really explain how next operates and what’s happening in the code sample.
  • You’re not using proper English that people can understand. (You’re using slang like “peps”).
  • In the “to long” section, for your second code example, you didn’t explain why what you did was better other than it was “short.”
  • Mainly, there’s a lack of explanations.

Majority of my critiques are just nitpicks so don’t take them personally.

6 Likes

This is not true. The table does not replicate from client-server, but can be used on client.

3 Likes

guess my resources are wrong woops gonna fix that

i removed the _G what do you think now @HugeCoolboy2007

Please use commas and grammar, it is extremely difficult to read this.

5 Likes

wow thank you, never knew shared existed :sweat_smile:

1 Like

i tried my best to fix it but i am no grammar dude its my least favorite subject

1 Like

me either i didn’t know till today but please… use modules

1 Like

it was actually was the original tittle for number