You can write your topic however you want, but you need to answer these questions:
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.
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)
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.
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