This (%) is a modulus. These return the decimal of an mathematical operation you did.
Say that i did 10 Ă· 2 usually. This would give 5. But if i did 10 % 2 it would give 0. It simply returns the decimal from said operation.
Here’s a little script to visualize :
If you need to round things, modulus isn’t for that.
There’s also a case where you see these in things like these :
local newStr = "This is an example. 12345!"
local str = string.gsub(newStr, "%d", "678910!"
print(str) --will print out "This is an example. 678910!"
If you aren’t familiar with this, simply focus on the modulus here : it can be used in a more complex topic (a.k.a string functions) in a string pattern, or even as a specifier for other more complicated topics (like os.date, for example)
I recommend you not to jump into the things I just mentioned, I only explained that other part for when you figure out all the rest of Lua until this point.
I know about the context, and i explained why i said the other part. The beginning part is for his general question. The other is in case he learns more about Studio (i even mentioned the things % can be used for, like string functions, os.date, etc.) so that they could come back to this post later, in case if they need to.
My last sentence said so, anyway.
Your first answer about it removing the decimal is a misconception. The correct answer is that it is modulo, and returns the remainder of the division:
It’s the modulo operator and it returns the remainder of an integer division.
An integer division is a division without decimal points. For example the integer division of 10/3 is 3(instead of 3.333...) and the remainder/modulo is 1(because num-divider*result = 10-3*3 = 1). Many languages support integer division by default(for example in python we do // and in C if two variables are of type integer the division is an integer division). However in lua we need to combine normal division with the math.floor function:
local integerDivision = math.floor(10/3)
print(integerDivision) --3
print(10%3 == 10-3*integerDivision) --true
Modulo is useful for a number of things, including converting things from a base to another(for example from base-10 to binary, base-8 or hex), for pseudorandom numbers, for certain things in cryptography and for running specific code on each x steps, for example if i%2 == 0 then task.wait() end should yield code inside a for loop once per two cycles, i%3 once per three, etc.
It’s also worth noting that the result of a modulo operation is always between 0 and modulo-1. So x%2 only returns 0 or 1, x%3 only 0, 1 or 2, etc.
You know how while dividing on paper you get a remainder? Something like:
(Pardon for toddler like handwriting LOL)
Where the 1 is the answer and 2 is the remainder? Basically the “%” or modulus operator does the same. It gets the remainder. For example:
print(5%3) -- Output: 2
Where 3 is the divider and 5 is the value.
It is usually used to limit numbers, for example, let’s say you have an angle of 400. You want to convert it into a range of 0-360. How would you do that? You use the modulus operator!
local angle = 400
local convertedAngle = angle%360
print(convertedAngle) -- Output: 40
Hope I clarified this in the easiest way possible!
Using some functions of the string library, you can use % to format a string in a way.
print(string.format("helloooooo %s!", "OnlyTwentyCharacters")) --> helloooooo OnlyTwentyCharacters!
--%s means a string.
--%i means an integer.
--%d means a number of any type.
--%% returns % itself.
--etc.
local perc = 57
print(string.format("%i%%", perc)) --> 57%