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

Snapping parts to grid from external script?

Asked by
Marios2 360 Moderation Voter
9 years ago

What i want to do is have every part created snap to a grid of 2 studs for one, intergers only. Players themselves click on any spot to place a part, and the script snaps the part afterward. I know that i need to use the modulo operator for that, but it doesn't seem to help much.

I am able to snap the grid to intergers, but only to a grid of intergers. Here's my last out of many attempts with comments added:

workspace.ChildAdded:connect(function(child)
    print("New child added to Workspace")
    if child:IsA("Part") then
        print("Snapping child to grid")
        print("Original position: " .. child.Position.X .. " " .. child.Position.Y .. " " .. child.Position.Z)
        if child.Position.X > 0 then --If X is positive then...
            if child.Position.X % 2 ~= 0 then child.Position = Vector3.new(math.ceil(child.Position.X),child.Position.Y,child.Position.Z) end --Snap the part to an interger
            if child.Position.X % 2 ~= 0 then child.Position = Vector3.new(math.ceil(child.Position.X),child.Position.Y,child.Position.Z) end --Not divisible afterwards? Snap again.
        else --If it's negative then...
            if child.Position.X % -2 ~= 0 then child.Position = Vector3.new(math.floor(child.Position.X),child.Position.Y,child.Position.Z) end
            if child.Position.X % -2 ~= 0 then child.Position = Vector3.new(math.floor(child.Position.X),child.Position.Y,child.Position.Z) end
        end --If Z is negative then...
        --Ignore the lines about Y
        if child.Position.Y % 1 ~= 0 then child.Position = Vector3.new(child.Position.X,math.floor(child.Position.Y),child.Position.Z) end
        if child.Position.Y == 0 then child.Position = Vector3.new(child.Position.X,1,child.Position.Z) end
        if child.Position.Z > 0 then
            if child.Position.Z % 2 ~= 0 then child.Position = Vector3.new(child.Position.X,child.Position.Y,math.ceil(child.Position.Z)) end
            if child.Position.Z % 2 ~= 0 then child.Position = Vector3.new(child.Position.X,child.Position.Y,math.ceil(child.Position.Z)) end
        else
            if child.Position.Z % -2 ~= 0 then child.Position = Vector3.new(child.Position.X,child.Position.Y,math.floor(child.Position.Z)) end
            if child.Position.Z % -2 ~= 0 then child.Position = Vector3.new(child.Position.X,child.Position.Y,math.floor(child.Position.Z)) end
        end
        child.Position = Vector3.new(child.Position.X,child.Position.Y + 0.5,child.Position.Z)
        print("New position: " .. child.Position.X .. " " .. child.Position.Y .. " " .. child.Position.Z)
    else
        print("New child isn't a part - probably a player character")
    end
end)

1 answer

Log in to vote
3
Answered by
Rhyles 80
9 years ago

The fist solution that comes to mind is the following:

local function snapTo2StudGrid(vec)
    return Vector3.new(
        math.floor(vec.x * 0.5 + 0.5) * 2,
        math.floor(vec.y * 0.5 + 0.5) * 2,
        math.floor(vec.z * 0.5 + 0.5) * 2
    )
end

I'm not sure it's the most efficient solution, however it does the job! :P

To implement this into your code, do something similar to this:

workspace.ChildAdded:connect(function(child)
    if child:IsA("Part") then
        part.CFrame = snapTo2StudGrid(part.Position) * (part.CFrame - part.Position)
    end
end)

Good luck! :D

0
Does the job perfectly! Marios2 360 — 9y
0
Be sure to up-vote! :) Rhyles 80 — 9y
Ad

Answer this question