So I'm working on a build tool and this is what I have:
function cloneWood(hitTarget)
local cloneTemp = game.Workspace.WoodBlock1
local clonedWood = cloneTemp:Clone()
clonedWood.Position = hitTarget
clonedWood.Parent = game.Workspace
end
local tool = script.Parent
tool.Equipped:connect(function(Mouse)
Mouse.Button1Down:connect(function()
local hitTarget = Mouse.Hit
cloneWood(Mouse.Hit.p) --once again, mouse.Hit.p or just set the CFrame
end)
end)
So I would like to round the position it is placed to the nearest whole number and I tried implementing this:
function round(num,multiple)
if num%multiple==0 then
return num
elseif num%multiple>multiple/2 then
i = 0
repeat i = i+1 until (i*multiple>num)
return i*multiple
elseif num%multiple<((multiple/2)+.5) then
i = 0
repeat i = i+1 until (i*multiple>num)
return ((i*multiple)-multiple)
end
end
But it does nothing, is there a simpler way to round? Edit:
local rounded = math.floor(number + .5)
I tried math.floor into a position but it said thats a number value, not a vector3
I tried implementing the answer like so:
function RoundNumber(Input) -- Input is whatever number you want to round
local StringForm = tostring(Input)
local PlacementOfDecimal = string.find(StringForm, ".")
if tonumber(string.sub(StringForm, PlacementOfDecimal+1 ,PlacementOfDecimal+1)) >= 5 then
return (tonumber(string.sub(StringForm, 0, PlacementOfDecimal))) + 1
elseif tonumber(string.sub(StringForm, PlacementOfDecimal+1 ,PlacementOfDecimal+1))<5 then
return (tonumber(string.sub(StringForm, 0, PlacementOfDecimal)))
end
end
function cloneWood(hitTarget)
local cloneTemp = game.Workspace.WoodBlock1
local clonedWood = cloneTemp:Clone()
clonedWood.Position = hitTarget
clonedWood.Parent = game.Workspace
end
local tool = script.Parent
tool.Equipped:connect(function(Mouse)
Mouse.Button1Down:connect(function()
local hitTarget = Mouse.Hit
local newTarg = RoundNumber(hitTarget)
print(newTarg)
cloneWood(newTarg) --once again, mouse.Hit.p or just set the CFrame
end)
end)
I just get an error saying it expected a Vector3 and got a number

2 comments
It might be a little of a longer script, but on line 21, You need to make X, Y, and Z rounded separately. To do this, you will have to do: local newTarg = Vector3.new(RoundNumber(hitTarget.X), RoundNumber(hitTarget.Y), RoundNumber(hitTarget.Z))
Players.DrGigaByte.Backpack.Build Tool.LocalScript:6: attempt to perform arithmetic on a nil value // for line 6