Any opinions on how I'd keep a 20x20 model on a 5x5 grid?
I've already tried some other code, but it didn't work, well it did, but it was inaccurate.
My code:
Item:SetPrimaryPartCFrame(CFrame.new(Mouse.Hit.p.X - Mouse.Hit.p.X%5, yAxis, Mouse.Hit.p.Z - Mouse.Hit.p.Z%5))
It's pretty easy, if your question is "How do I snap it to a grid".
local function round(i) --You make a function to round to the nearest number, it would have a single parameter. return math.floor(i+.5) --math.floor + .5 will round to the nearest. end local function snap(p,gW,gD) --The snap function, it should have three parameters, the position, the Grid width and Grid depth return Vector3.new( round(p.x / gW)*gW, --Simple equation, shouldn't need to be explained as it is really that simple. 1, --You could also use the rounded number of p.y /a grid height * a grid height for this. round(p.z / gD)*gD --repeat the equation. ) end
Next you would write a function that snaps it's position when you want it to.
I.E.
button.MouseButton1Clicked:connect(function() game.Workspace.SnapPart.CFrame=snap(game.Workspace.SnapPart.CFrame,5,5) end)
Sources: Math functions, Using return, Gui click, CFrame, Vector3