Consecutive scoring (SOLVED)

I have a function that add some points for my player. How would call a function for every 20 points the player gets? For example:

If his score is 20, 40, 60, 80 and so on then a function will be called.

Something like this?

local points = 0
local points_last = 0
local INCREMENT = 20

local function round_to_nearest(value, multiple)
  local half = multiple / 2
  return value + half - math.fmod((value + half), multiple)
end

local function do_the_thing()
  print("You got some points! " .. points)
end

local function check_points()
  if points < INCREMENT then return end
  local rounded = round_to_nearest(points, INCREMENT)
  print(rounded)
  if rounded > points_last then
    points_last = points
    do_the_thing()
  end
end

local function add_points(amount)
  points = points + amount
  check_points()
end

for i=1, 100 do
  add_points(math.random(10))
end
3 Likes