Is it possible to check if a number is a multiple of another number? (Needs to account for all numbers)

Hello developers, I’m making a grid system and need to check if the position of an object on that grid is a multiple of 3 to verify that the object is on the grid properly.

Any help with this is massively appreciated as it is a crucial part of the system.
Thanks, MBroNetwork :slight_smile:

Edit: I am very stuck, I have no idea how modulus works and can’t get it to stay as a multiple of 3, it keeps giving me false detections of off-grid objects.

1 Like

Take the number and divide it by 3. Then take this new number and math.floor() it. Compare the number from math.floor() and the number from dividing by 3. If the two numbers are equal, then the number is a multiple of 3.

Yes, it’s called modulus. The operator is the %. Take number % 3 == 0 to see if number is a multiple of 3.

3 Likes

How would I verify if it was correct with this?
Would I have to compare the original with the current?

I’ve never really thought of this approach, Do you mean to compare the original number and then the one divided by 3?

So what I mean is get a new number by dividing your number by 3.

Take this new number, and keep it in a variable or something.

And then do a thing like…

if math.floor(new number) == new number then

…to see if it’s a multiple of three.

Also, I have never known of Modulus (%) before this and I have a feeling it’s better than what I have here.

1 Like

I’ll probably look into modulus as well as this approach.
Due to the way the grid system works currently, an exploiter can just put a piece off grid by calling FireServer to the event.

I just did a check and apparently the modulus operator doesn’t support floating-point numbers which an exploiter could specify in their FireServer event.

Should I just round the int directly and then use that parameter?

How did you check this, the operator works with all numbers. Do you have any example code to share? .

(The manual defines it as a % b == a - math.floor(a/b)*b)

I don’t get it at all, I’m genuinely getting a headache from this.
Modulus is annoying.

Try it out in the Studio command bar maybe to get a feel for it?

print(15.6 % 3 == 0)
→ false

print(15.0 % 3 == 0)
→ true

1 Like

Thank you so much, the way that it was being wrote everywhere I looked kept returning the wrong value.

Thanks for clearing up the misinformation and now that I know how it works, I can finally get the grid fixed so that exploiters can’t place objects off of the grid by calling FireServer.

:smiley:

1 Like

if a number is not a multiple, how would I change it so it is one