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

How would I make a part following the mouse snap to 1 stud?

I have this script right here that makes a part follow the mouse (not the full script)

render:connect(function()
   if moving then
       currentPart.CFrame = CFrame.new(mouse.Hit.p)
   end
end)

But the problem is the part:

  1. Doesn't nap to the nearest whole stud
  2. It keeps moving closer to the camera, like floating in the air. I want it to stay snapped to the Y.

This is for a building system in a new game I'm making. Thanks!

1 comment

0

Not sure if this will work but try this. CFrame.new(mouse.Hit.p.X, PosY, mouse.Hit.p.Z) PosY would be where you want the block on the Y axis.

1 answer

You can prevent the floating effect by ignoring the part, by setting mouse.TargetFilter = currentPart (and resetting this to nil after the part is placed). To snap to the grid, you can do currentPart.CFrame = CFrame.new(math.floor(mouse.Hit.p.x+0.5), mouse.Hit.p.y, math.floor(mouse.Hit.p.z+0.5)) to round the x and y coordinates to the nearest whole number. If you want a different gridsize, do math.floor(mouse.Hit.p.x/gridSize+0.5)*gridSize for the x and z.

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