Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there a way to round positions on roblox? *Edited*

Asked by 6 years ago
Edited 6 years ago

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

0
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)) zyrun 168 — 6y
0
Players.DrGigaByte.Backpack.Build Tool.LocalScript:6: attempt to perform arithmetic on a nil value // for line 6 ExtremeNyanCat123 32 — 6y

1 answer

Log in to vote
0
Answered by
zyrun 168
6 years ago

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

0
Now how would I convert that number into a Vector3, trying to round positions ExtremeNyanCat123 32 — 6y
0
Players.DrGigaByte.Backpack.Build Tool.LocalScript:6: attempt to perform arithmetic on a nil value // for line 6 ExtremeNyanCat123 32 — 6y
0
Ah, after line 3, put this: if PlacementOfDecimal ~= nil then Make sure to include another end after line 8 zyrun 168 — 6y
Ad

Answer this question