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
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
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)
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
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
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
(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.
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