Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

6 comments

1

I don't get it. What was it supposed to do and what is happening?

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 .-.

0

I want it to snap on the Y axis, but it doesn't do that

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.

0

How would I check that?

0

..wha

1 answer

✓ Accepted answer

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

2 comments

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

0

Thanks, it works!

This discussion is preserved as it was. New answers, comments, and votes are closed.