How would I add increments to my building system?

  1. How would I add increments to my building system? For example increment 1 is 1 stud of movement and 2 is 2 studs etc.
  2. What is the issue? Can’t figure out.
  3. What solutions have you tried so far? I’ve tried putting my increment value everywhere but it doesn’t seem to work, also looked on the devforum.
obj = transBlock
					local ignrlst = {Player.Character, obj.PrimaryPart}
					local pos,target,norm = FindPositonForPart(ignrlst)
					if pos ~= nil then
						local x = math.floor(pos.X + .5)
						local y = math.floor(pos.Y + .5)
						local z = math.floor(pos.Z + .5)
						local rpos = Vector3.new(x,y,z)
						if rpos and norm then
							local npos = rpos + norm
							obj:SetPrimaryPartCFrame(CFrame.new(npos))
						end
						mTarg = target
					end

To round you may use math.round() as it is the same as math.floor(x + .5).

I believe using modulo may be an appropriate approach, it will give you what is missing to make it evenly divisible.

If you have an increment of 2 then only numbers divisible by 2 would be allowed. I think you might be able to do something like this:

function f(inc, val)
	val = math.round(val)
	local diff = val % inc
	return math.round(val + diff/2)
end

print(f(2, 2.5)) -- Prints 4

This is not exactly what you wanted, but this is a perhaps a good starting point for you. Currently it will round anything from from .5 and up to the next interval, but you can probably fix this with some minor adjustments.

1 Like

Not exactly true. math.round() rounds up or down while math.floor() will only round down

Yes, but adding .5 to any value inside of it will round it up or down.

math.floor(x + .5) -- Expression
-- Examples:
math.floor(.3 + .5) -- = 0.8 => 0 [math.round(0.3) => 0]
math.floor(.4 + .5) -- = 0.9 => 0 [math.round(0.4) => 0]
math.floor(.5 + .5) -- = 1 => 1 [math.round(0.5) => 1]
math.floor(.9 + .5) -- = 1.4 => 0 [math.round(0.9) => 1]

Thank you so much, I’ve been trying to solve this for a week but it just wouldn’t work. You’ve really saved me here!

1 Like
local function RoundNumberToNearestMultipleOfN(Number, N)
	return math.floor(Number / N) * N
end

print(RoundNumberToNearestMultipleOfN(15, 7)) --14
print(RoundNumberToNearestMultipleOfN(10, 3)) --9

Shouldn’t 2.5 round down to 2 not up to 4? Assuming you want to snap a value to a grid of values.

Check my reply above.

Yes, it should.

However, since 4.5 will round to 6 it will still be 2 units of space, just offset in a dumb way.