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

Stamper tool works, but the Y position doesn't?

Asked by
Valatos 166
7 years ago

So I'm making a game inspired by Create Your Own Security Base, my stamper works fine, it has a 4x4 grid. But the problem is, the Y position doesn't work as it is meant for, this is what I use for my position

local grid = 4

function snap()
    local position = mouse.Hit.p
    return CFrame.new(
        position.X - position.X % grid,
        position.Y - position.Y % grid,
        position.Z - position.Z % grid
    )
end

It also doesn't position the Y position correct Image for what it does

1
I don't get it. What was it supposed to do and what is happening? Tesouro 407 — 7y
0
If you mean you don't want your part snapping on the y axis.. then just don't snap it on the y axis bruh .-. Goulstem 8144 — 7y
0
I want it to snap on the Y axis, but it doesn't do that Valatos 166 — 7y
0
But it does. The only way to check if it does is if you had your mouse on the side of another brick. mouse.Hit returns where the mouse landed on in world space. Goulstem 8144 — 7y
View all comments (2 more)
0
How would I check that? Valatos 166 — 7y
0
..wha Goulstem 8144 — 7y

1 answer

Log in to vote
0
Answered by
NubgIe 15
6 years ago

I suggest you don't use % for this. If your part is at position 3.9, on a 4x4 grid it would snap to 0, when it's closer to 4. A really easy to sort stuff like this is just to use math.floor

local Floor = math.floor
local function RoundToNearest(ToRound, Increment)
    return Floor(ToRound/Increment + 0.5)*Increment
end

RoundToNearest(1.2, 0.5) - Round 1.2 to the nearest 0.5 increment
RoundToNearest(5.4, 4) - Round 5.4 to the nearest 4 increment.

So for your code,

local Floor = math.floor
local function RoundToNearest(ToRound, Increment)
    return Floor(ToRound/Increment + 0.5)*Increment
end

local grid = 4

function snap()
    local position = mouse.Hit.p
    return CFrame.new(
        RoundToNearest(position.X, grid),
        RoundToNearest(position.Y, grid),
        RoundToNearest(position.Z, grid),
    )
end
0
As for the Y position messing up, it's probably because you're trying to set the position to the mouse.Y, which would generally be at the bottom, under everything. If you're going to use my code, I suggest changing the Y position of the cframe to RoundToNearest(position.Y, grid) + 4 NubgIe 15 — 6y
0
Thanks, it works! Valatos 166 — 6y
Ad

Answer this question