Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What does a % do in a script?

Asked by
Discern 1007 Moderation Voter
9 years ago

So I was looking at the Snack Break problems created by Sukinahito. I looked at his solution for Snack Break #1, and his answer included % marks which I didn't even know what they did in Lua.

What do % marks even do in ROBLOX Lua?

Sukinahito's script in question:

local results = { }
for index = 1, 100 do
    if index == 0 then
        results[index] = "FizzBuzz"
    elseif index%3 == 0 then
        results[index] = "Fizz"
    elseif index%5 == 0 then
        results[index] = "Buzz"
    else
        results[index] = index
    end
end
print(table.concat(results, "\n"))

1 answer

Log in to vote
1
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
9 years ago

A "%" in Lua and many other languages is an operator that preforms the modulus operation. What it does it finds the remainder of the division of two values. The remainder is simply what is left over after completing division (see this image if this description wasn't clear enough).

0
So if I put in print(9%4) in a script, 1 is what appears in the output? Discern 1007 — 9y
0
Yes, because that's what would remain if you divided 9 by 4. ImageLabel 1541 — 9y
0
Correct as 4 goes into 9 twice with 1 unit left over BlackJPI 2658 — 9y
0
Awesome, thanks. Discern 1007 — 9y
Ad

Answer this question