Introduction
Hi, This is just a tutorial that I am Arguing with myself whether if I should be putting in #resources:community-resources or not, But for now until proven otherwise, It will be here as a Tutorial for people to use or give to people for specific reasons, idk, its all up to you whether you want to look at this or not
This is just a combination of Data I have learned over time, I generally use these methods over others due to how simple they are to use.
Prelude
This mainly revolves around the Usage of if
, elseif
, and else
as those are generally very overused to the point where there is so many Possibilities for one statement that it’s unnecessary.
However, there maybe other examples I will Provide, I’ll Update if I find more you can do.
A Few things to Remember However:
-
Intended For Beginners
This Topic is intended to be for Beginners with Luau, There are plenty of well experienced people who can help with Better Methods than me. -
Some Meanings are Simplfied
Some of the meanings or Usages for some functions are Simplified to Help with Explanation. -
Correct Me if I’m Wrong
I could be wrong about some of the things I write, Feel free to Correct me if thats the case with this Tutorial.
I’m not perfect, and never expect me to be so, I tend to make a lot of mistakes when it comes to this so dont just reply saying “This is bad, blah blah blah”, I am fully Aware of my mistakes if I make any, Just some be an arse about it.
Anyway, This Prelude is getting very long so lets just get started.
Start
This generally isn’t a bad thing to do the methods I’ll cover, Its just that It can be simplified or Improved on with other methods, some Methods usually take a long time to write, or take a while to get working, which is normal when scripting, as it is just a Trial and Error Process to get the Expected Result.
Although there are plenty of methods to write code, there are plenty of bad or weird things we tend to do when writing them, we tend to add unnecessary things to them when you dont have to do them as they can be change with something else, usually doing these kinds of things results in weird looking code in which you are suprised Luau even allows it, or completely inefficient code that looks completely wrong, very slow, and very unnessacary, But Again, this tutorial is intended to be toward beginners so, I’ll help with fixing those mistakes.
When it comes to efficiency, you should always choose the best methods when it comes to that, many methods you try can be very inefficient compared to many other methods, some I’ll cover here.
I have Hid the Topics so it looks Better, However you decide that.
As if currently, there is 6 Examples, I may add more to this if I feel like it.
error() if false
error()
if false
We are able to check if the conditions we have are true or not with the usage of if
statements (if you forgot), In this case we can just use an if
statement to check if our condition is true
, Like with this code example to error()
our code if a statement is false:
local Active = false -- Active is false
if not Active then -- if false of nil
error("It is not Active.") -- errors code
end -- end of if statement
-- It will ignore the if statement and continue if Active is true.
However, We dont need to do this at all, while yes, this is the “Correct way” to do it, it isn’t the best way to do it, which is where the function assert()
comes in,
assert()
is used to check if a statement is true
or false
, and even nil
, we can use it to check for this, and if the statement is false
or nil
, it will error:
assert(Active == true, "It is not Active.")
-- Alternate Method:
assert(Active, "It is not Active.")
-- It's probably best to check if its true with "==" instead "if this then"
assert()
would be useful for this check, instead of having multiple if
statements for it.
assert(Something, "Something isnt there?")
assert(Data ~= nil , "Data is nil.")
assert(Imposr , "AMOGUS")
DoSomething(Something, Data, Imposr)
Setting Minimum and Maximum Values
Setting Minimum and Maximum Values
When Setting Minimum and Maximum values, you can use an if
statement to check for this like for Example:
if Number > Maximum then -- if number is greater than the Max Value
Number = Maximum
end
if Number < Minimum then -- if number is less than the Min Value
Number = Minimum
end
But again, we dont have to use this, you are able to use the math
functions math.min
and math.max
for this.
math.min
will get the minimum Number, while math.max
will get the maximum Number.
(You you didnt get that already, If you did, Good Job!, If not… Good Job!)
So for this, we can use these functions to set a limit for our number, like this:
local Maximum = 10
-- Limit for Higher values
Number = math.min(Maximum, Number)
This works as we are checking for the smallest value, if Number
is smaller, it will return Number
, however if 10
is smaller, it will return 10
as the new number, so it will automatically apply the Maximum Value, the same can be done with the Minimum Value with math.max
local Minimum = 0
-- Limit for lower values
Number = math.max(Minimum, Number) -- returns the Hightest value
However, it wouldn’t be great if we used both math
functions for this task, instead, we have math.clamp
for this where we can combine both of these functions together to limit the number.
- The First Argument is the Number we Currently have
- The Second Argument is the Minimum Number that the number can’t pass
- The Third Argument is the Maximum Number that the number can’t pass
local Minimum = 0
local Maximum = 10
Number = math.clamp(Number, Minimum, Maximum)
Resetting Numbers
Resetting Numbers
When reseting numbers, we can simply check when the number has reached a limit:
if Number >= Limit then
Number = 0
end
However, this can also be simplified by using the Modulo (or Modulus) Operator which is the % symbol.
Now, what does this do?
Modulo Simply returns the Remainder in Divison, we can use this to reset a number once it has reached a limit, so for Example:
10 % 11 = 10
11 % 11 = 0
We can use this to reset numbers when they reach a limit, and plus, if they dont, it wont affect your number, so it can be useful:
Number %= 10 -- if number is equal to 10, it will go back to 0
This can also be useful to 12 Hour Clocks when you want to set the hour from 12 to 1:
local Hour = 12
Hour = (Hour % 12) + 1
-- Number will reset to 0 but + 1 is added to the mix so it will be 1
Setting up Timers
Setting up Timers
When setting up a Digital Clock of some sorts, we would need to check if they number has reached a limit, like for example, if a number is less than 0, we should Add a 0 at the beginning to determine that, if a number has hit a limit, reset it, like with this:
local Sec = 0
local Min = 1
local SecFormat = Sec
local MinFormat = Min
if Sec < 9 then -- if number is less than 9
SecFormat = "0"..Sec -- formats Seconds
end
if Min < 9 then -- if Minutes are less than 9
MinFormat = "0"..Min -- formats Minutes
end
if Sec > 59 then -- if number is greater than 59
Sec = 0 -- resets
SecFormat = "0"..Sec -- also formats Seconds
end
local Time = MinFormat..":"..SecFormat -- concatenates our numbers
Although this is correct, it isnt efficient, we have to constantly check if the number is less or more than something, so instead, we will use string.format
for this, now what is this and how to we use it?
so string.format
is exactly what it sound like, it formats a string, we can use this to set up a format where we have a real Timer instead of adding on 0’s, For this we will use the Following Specifiers:
-
%d
or%c
to represent a number -
%02i
Adds Padding of 0’s
Ex:1 + format(%02i) = "01"
,10 + format(%03i) = "010"
Which you can basically find this information here.
So now, If we put these formats together, we now have this:
-- ... is a placeholder just to remind you.
string.format("%d:%02i", ...) -- our format
-- Alternate method:
("%d:%02i"):format(...) -- also our format
Wwith this, we can now have a accurate representation of a timer without the need to manually check it manually, but we still have to check out minutes and Seconds, We will use the Topic above (Ressetting Numbers) to help us with this:
-- finally, an actual use for an if statement
if Sec == 0 and Min == 0 then return end
if Sec > 59 then -- if Seconds is greater
Min += 1
elseif Sec < 0 then -- if Seconds is less than 0
Min -= 1
end
Sec %= 60 -- resets number to 0 or 59
-- you can add this for Minutes as well, Up to you tho :)
Timerformat = string.format("%d:%02i", Min, Sec) -- formats numbers
print(string.format("%d:%02i", 1, 1)) --> "1:01"
print(string.format("%d:%02i", 12, 59)) --> "12:59"
This will significantly simplify and improve your code
Absurd amount of elseifs
When using if
statements, you generally use them for one purpose, not multiple, but if you do, you have else
and elseif
to help with this.
However, It is never a good idea to place elseifs
everywhere within a statement, thats how you make your code look ugly and hard to read, Instead you should try using Arrays, or Create a function for the statements rather than placing elseif
left and right
What NOT to do;
if Statement then -- this statement is ugly
elseif Statement then
elseif Statement then
elseif Statement then
elseif Statement then
elseif Statement then
elseif Statement then
elseif Statement then
elseif Statement then
elseif Statement then
elseif Statement then
elseif Statement then
elseif Statement then
end
Typewriting Effects
Typewriting Effects
No, This isnt here because of Another Topic
And No, This isnt to hate the person who did this, but still, what you did was kind of stupid.
If you arent aware, I made a Tutorial about Typewriter Effects which will cover a lot of what I will say here.
2 Method's for a Typewriter Effect
It is also In my Featured Topics right now, if you want to, check it out.
When making Typewriter Effects, You usually do it will specific Methods like with a for
loop, but it is never a good idea to be doing this:
TextLabel.Text = "Hello"
task.wait(.1)
TextLabel.Text = "Hell"
task.wait(.1)
TextLabel.Text = "Hel"
task.wait(.1)
TextLabel.Text = "He"
task.wait(.1)
TextLabel.Text = "H"
task.wait(.1)
TextLabel.Text = ""
(But dont worry, I bet you got that too, if so, good job! you’re so smart!)
This would make code long and inefficient and would be great at all, instead we use a for
loop with string.sub
to have this same effect.
-
First Argument is our string
-
Second Argument is the Start of where the string is, starting on the character
Default is 1 meaning the first Character -
The Third Argument is the Target, basically the character the string will stop
Default is -1 meaning it starts at the very end, However it can be reversed to just the last character in out string, so if we haveHello
, we can put it as-1
or5
.
for i = #Text,0,-1 do -- iterates for the number of letters in the string
-- 0 is our Target Number to reach
-- the third argument for -1 is for the index to go down
task.wait(.1) -- waits .1 seconds
TextLabel.Text = string.sub(Text, 1, i) -- subtracts from string
end
This would be a way better method for this rather than the other!
Conclusion
There is no Really much to say, Just Hi.
I basically already have said what there is need to be said, These may help with with scripting, maybe you didnt know these methods, Maybe you didnt understand this, But thats fine. You can always learn more.
Thoughts
I would like to know what you think!
- Useful
- Good
- Ok
- Bad
- Terrible
0 voters
Whatever I put here?
This Section is just whatever, could be Off Topic, Could be useful.
Nothing to See here
Seriously, nothing to see here
Come on bro, not this again
I'm Serious
stop.
Stop.
Stop!
Stop!!!
STOP!!!
PLEASE!!
STOP IT!!!
YOU'RE KILLING ME!!!!
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
math.random() for Arrays
math.random() for Arrays
for some reason, people dont know how to do this with Arrays, where you can get the Random Index from math.random()
Food = {"Orange", "Apples", "Bannana"}
local RandomFood = Food[math.random(1, #Food]) -- Gets Random Index between 1 and the Number of Items in the Array
print(RandomFood) -- "Orange", "Apples", "Bannana"? (Result is Random)