Rounding numbers not working properly

Of course, but notice that putting negative values will not give the same output as using math.round.
So here in case to make it works with negative values:

function round(x)
	if x < 0 then
		x = math.floor(x - 0.5)
	else
		x = math.floor(x + 0.5)
	end
	return x
end
2 Likes

What do you mean? That’s not what you used.

1 Like

just letting you know this is also meant to be a placement system

I see but I think there needs to be math.ceil if x<0 like this:
function round(x)
	if x < 0 then
		x = math.ceil(x - 0.5)
	else
		x = math.floor(x + 0.5)
	end
	return x
end
2 Likes

Try with this (You might organize your code better and don’t make everything lowercase).

local replicatedstorage = game:GetService("ReplicatedStorage")

local localplayer = game:GetService("Players").LocalPlayer
local mouse = localplayer:GetMouse()

local event = replicatedstorage:WaitForChild("Placed")
local blocks = replicatedstorage:WaitForChild("Blocks")

local function round(x)
	if x < 0 then
		x = math.ceil(x - 0.5)
	else
		x = math.floor(x + 0.5)
	end
	return x
end

script.Parent.Activated:Connect(function()
	local woodplanks = blocks:WaitForChild("WoodPlanks")
	local blockpl = woodplanks:Clone()
	blockpl.Position = Vector3.new(round(mouse.Hit.X), round(mouse.Hit.Y), round(mouse.Hit.Z))
	blockpl.Parent = workspace
end)
2 Likes

Yeah i just noticed that i did something wrong, but i saw that floor were almost doing the same job and i wrote it quick. Thanks correcting me.

2 Likes

is this the right video now? - YouTube
ok this is the right video, its just that it places the blocks inside the block. this is a 3x3 grid not a 1x1 or 2x2 grid

1 Like

I can’t test it but maybe try changing the return x inside the ‘round’ function by return x * 3.

1 Like

nope just places it far away from the mouse

Sorry to intervene, but why you didn’t just use math.round, it seems to work fine.

2 Likes

math.round rounds to the nearest whole

1 Like

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
3 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