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
I'm sure roblox has rounding function (math.floor, possibly), But I've never liked it. I've always made my own to make it more customizable. Note: This only works if you're rounding by the thenths place
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
For more infomation on why this works, go here: http://wiki.roblox.com/index.php?title=Global_namespace/String_manipulation