How do i implement grid placement?

Can someone help me with grid placement. If you would like me to show a part of the current placement script, i can. But i would like to know how to change the object movement to a grid. The object is controlled from the mouse.

I have tried this but i get an error:

local ObjectAngles = CFrame.Angles(0, math.rad(RotatingAmount), 0)
local grid = 2
local ObjectCFrame = Vector3.new(math.round(mouse.Hit.Position.X / grid), math.round(mouse.Hit.Position.Y / grid + PrevieuwObject.PrimaryPart.Size.Y / 2), math.round(mouse.Hit.Position.Z / grid))* grid

PrevieuwObject:SetPrimaryPartCFrame(ObjectCFrame * ObjectAngles)

You get that error because your “ObjectCFrame” is a Vector3, not a CFrame. Use CFrame.new() instead of Vector3.new().

And to round a value to a certain grid, you need to round the value multiplied by your grid’s size, before dividing the result. You seem to be doing it the other way around.
Instead of

math.round(mouse.Hit.Position.X / grid)*grid

Try something like

math.round(mouse.Hit.Position.X*gridSize)/gridSize
1 Like

Hey, so i tried using it and i got an error in the output:


Here is the script incase if i did it incorrect:

local grid = 1
local ObjectCFrame = CFrame.new(math.round(mouse.Hit.Position.X*grid), math.round(mouse.Hit.Position.Y + PrevieuwObject.PrimaryPart.Size.Y / 2*grid), math.round(mouse.Hit.Position.Z*grid)) / grid
local ObjectAngles = CFrame.Angles(0, math.rad(RotatingAmount), 0)
PrevieuwObject:SetPrimaryPartCFrame(ObjectCFrame * ObjectAngles)

Maybe try this?

local grid = 1
local ObjectCFrame = CFrame.new(math.round(mouse.Hit.Position.X*grid)/grid, math.round(mouse.Hit.Position.Y + PreviewObject.PrimaryPart.Size.Y / 2*grid)/grid, math.round(mouse.Hit.Position.Z*grid) / grid)
local ObjectAngles = CFrame.Angles(0, math.rad(RotatingAmount), 0)
PreviewObject:SetPrimaryPartCFrame(ObjectCFrame * ObjectAngles)

Thank you. I didnt notice that i did it incorrect. Onc again thank you for the help!

1 Like

Actually, use this as it is better organized:

local grid = 1
local hit = mouse.Hit.Position
local size = PreviewObject.PrimaryPart.Size
local c = CFrame.new(
	math.round(hit.X*grid)/grid, 
	math.round(hit.Y + size.Y / 2*grid)/grid, 
	math.round(hit.Z*grid) / grid
) * CFrame.Angles(0, math.rad(RotatingAmount), 0)
PreviewObject:SetPrimaryPartCFrame(c)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.