Rounding numbers not working properly

none of these solutions work(but thanks for responding), im gonna continue browsing the internet for some answers

You can round to the nearest number by adding .5 then flooring or by subtracting .5 then ceiling.

That is to round to the nearest 1.

If you want to round to the nearest power of three then you can add 1.5 then modulo operator can let you get the remainder. Then you can subtract the remainder.

function roundToNearest(number, multiple)
    number += multiple / 2
    number -= multiple % number -- I forgot which way this goes...
    return number
end

what should the multiple be? ch

Well it should be the numbers you want to round closest to. If you want to round closest to 5 then the multiple should be 5

nope not working, like literally not working

I believe this is just a minuscule position difference where mouse.Hit goes slightly inside the part (this causes the position to be closer to the block you’re clicking on rather than outside of it). Try offsetting mouse.Hit by it’s opposite unit vector multiplied by a small number like 0.01.

local hit = mouse.Hit - mouse.Hit.LookVector * 0.01
2 Likes

(n/3 + 0.5) * n is literally ((n*n) / 3) + 0.5 which doesn’t return the grid placement offset.

@qwertluk Try reading through other placeement systems to see how they round to the grid offset, not sure how would I go about it with the formula, other than counting with 1 stud displacement.

function round(number)

return (number-(number%3)) + 1 -- add the half length of the side of the cube for offset

end

You need to add the offset which will allow the block to be place properly.

I did a test the number and multiple are the wrong way around. This should work.

local testNumbers = {1,2,3,4.5,3.5,0}

local function RoundToNearestMultiple(number: number, multiple: number)
	number += multiple / 2
	number -= number % multiple
	return number
end

for _, Number in pairs(testNumbers) do
	print(RoundToNearestMultiple(Number,3))
end
-- Output
  16:30:42.112  0  -  Client - LocalScript:10
  16:30:42.113  3 (x2)  -  Client - LocalScript:10
  16:30:42.113  6  -  Client - LocalScript:10
  16:30:42.113  3  -  Client - LocalScript:10
  16:30:42.113  0  -  Client - LocalScript:10
1 Like

Thank you @koziahss and @VegetationBush for helping! :smiley:

1 Like