Rounding numbers not working properly

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I’m trying to make a placement system and every time I try to round to nearest 3. Like 3,6,9,12,15.

  2. What is the issue? Include screenshots / videos if possible!
    It works at first but when I try to round into negative numbers it doesn’t work.
    here is the script in the tool

local localplayer = game.Players.LocalPlayer
local replicatedstorage = game:GetService("ReplicatedStorage")
local event = replicatedstorage.Placed
local blocks = replicatedstorage.Blocks
local mouse = localplayer:GetMouse()
function round(n)
	return math.floor(n/3 + 0.5) * 3
end

script.Parent.Activated:Connect(function()
	local blockpl = blocks.WoodPlanks:Clone()
	blockpl.Position = Vector3.new(round(mouse.Hit.X),round(mouse.Hit.Y),round(mouse.Hit.Z))
	blockpl.Parent = game.Workspace
end)

Here is an example:
https://www.youtube.com/watch?v=FfLabu1cet4

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Yes I have looks on the devforum, but all the rounding methods just work on the addition and not negative.
2 Likes
local gridNumber(n)
   return n - (n%3)
end
1 Like

doesn’t work thanks for commenting though

I forgot to declare the function, if you haven’t done that, this should work.

local function gridNumber(n)
   return n - (n%3)
end
1 Like

yeah i added the function before and it still doesn’t work

Uhhh, I just tested it and it works perfectly for me, unless I missunderstood what you really want.

You want a number to be rounded into grid offset right? And the grid is 3 studs.

3 → 3
4 → 3
8 → 9
28 → 27

1 Like

Try this:

local function round(n)
    return math.floor(n + 0.5)
end
1 Like

Ok ill test it again, ill let you know how it goes

Did you read my post, I used that and it didn’t work

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