Algorithm for checking if a number is a multiple of another

  1. What do you want to achieve? A combo system, that detects every 3 times you click, then it prints “COMBO”

  2. What is the issue? I can only make it work for 3 punches.

  3. What solutions have you tried so far? I’ve been looking for about 30 minutes now throughout a-lot of websites, and either search algorithms are bad, or as it seems, that no one has posted about this.

Here is a snippet of my code (I don’t want to show it all because someone might steal)

local plr = game:GetService("Players").LocalPlayer
local punches = plr.Backpack.LocalScript.Punches

script.Parent.Text = ("Punches: ").. punches.Value
punches.Changed:Connect(function()
   script.Parent.Text = ("Punches: ").. punches.Value
	checkCombo()
end)

function checkCombo()
	if punches.Value == 3 then
	print("COMBO!")
	script.Parent.Text = ("COMBO!")
	wait(3)
	script.Parent.Text = ("Punches: ").. punches.Value
	end	
end

Basically, there is a NumberValue inside a localscript in the player’s backpack, and it detects if the mouse clicks, if so, increase the value.

SHORT: HELP ME MAKE A ALGORITHM OR SOMETHING TO CHECK IF THE NUMBER OF PUNCHES IS A MULTIPLE OF THREE

2 Likes

Use the modulus operator %. If a%b == 0 then you know a is a multiple of b. Also, the "Punches: " and "COMBO!" strings do not need to be in parentheses.

5 Likes

I don’t really understand that, if a’s percentage of b = 0 then? would a be the undefined number and b be 3??

No. % is the modulus operator. It returns the remainder of two numbers after division. When you divide, say, 6 by 3, you get 2 with no remainder. So, 6%3 == 0.

Ok… so do I do?

function checkCombo()
	if punches.Value == 3%12 == 0
	print("COMBO!")
	script.Parent.Text = ("COMBO!")
	wait(3)
	script.Parent.Text = ("Punches: ").. punches.Value
	end	
end

I really don’t understand how I incorporate this… I feel stupid but idk how I’m gonna make it know if the value is a multiple of 3… :sleepy:

No. You do if punches.Value%3 == 0 then

1 Like

Ok… let me test it out

30 characters

Let’s say you want to check if 22 is a multiple of 2, you do

if 22%2 == x then
      print(x)
end

x, in this code, is the remainder when you divide 22 by 2, and if there’s no remainder, (if remainder is equal to 0), then 22 is a multiple of 2.

1 Like

Ok, so it worked! Thanks, I really gotta get better at math for this stuff, and get my brain working again. All this quarantine ain’t doing the best for me lol. Your awesome, I will try understanding this! :wink:

1 Like