How do I make it so a part moves with mouse position but with 2 studs

Hi, I’m making so that a part moves with the mouse position, but with a grid of 2 studs, and I have tried many things but it didn’t work. Here is the code:

local Part = game.Workspace.Part

local Grid = 2;

local RunService = game:GetService("RunService")

local Mouse = game.Players.LocalPlayer:GetMouse()

RunService.RenderStepped:Connect(function()
	Part.CFrame = CFrame.new(Mouse.Hit.X, Mouse.Hit.Y + 0.5, Mouse.Hit.Z)
	Mouse.TargetFilter = Part;
end)

Replace your Part.CFrame with this and it should work

Part.CFrame = CFrame.new(math.floor(Mouse.Hit.X/Grid)*Grid, Mouse.Hit.Y + 0.5, math.floor(Mouse.Hit.Z/Grid)*Grid)

I got that solution from this Reddit post.

5 Likes

change Mouse.Hit.X to Mouse.Hit.X + (2 - Mouse.Hit.X%2) and then do the same for the other two. I am not 100% sure this will work since it’s hard to visualize the math, so try it out and see if it works

EDIT: They guy from above is probably correct

No way! Thank you so much!!! I can’t thank you enough!

1 Like

It’s working, but, the part is not on the mouse.

Part.CFrame = CFrame.new(math.floor((Mouse.Hit.X+Grid/2)/Grid)*Grid, Mouse.Hit.Y + 0.5, math.floor((Mouse.Hit.Z+Grid/2)/Grid)*Grid)

Try this?

Okay, I will try that thank you

It works but that’s how it is with longer grid

hold on I think I can fix it probably.