I've been trying to make a Grid placement script, this is what I have:
Player = game.Players.LocalPlayer Part = game.Workspace.TestPart Mouse = Player:GetMouse() MouseDown = false function SnapToGrid() return CFrame.new( math.floor(Mouse.Hit.X * 0.5 + 0.5) * 2, math.floor(Part.CFrame.Y), math.floor(Mouse.Hit.Z * 0.5 + 0.5) * 2 ) end Mouse.Button1Down:connect(function() if Mouse.Target.Name == "TestPart" then MouseDown = true repeat wait() Part.CFrame = SnapToGrid() until MouseDown == false end end) Mouse.Button1Up:connect(function() MouseDown = false end)
But it doesn't exactly work... I've been trying to mess around with the numbers, but it doesn't seem to fix it. Anyone know what i'm doing wrong?
Hello there! I also once had this question, and my friend had to tell me the logic: Imagine that your cursor is on 2.62 and you have a grid of 5.
Let's divide it by your grid size (5): 0.524 Now, round that number. We'll get 1. Do the rounded number times the grid size and TADAA! This will give 5.
Let's try it with another random number. 634.245
634.245 / 5 = 126.849
126.849 rounded = 127
127 * 5 = 635. Boom :)
I don't think I have to put this in a script for you, since you look pretty experienced. However, if you would like me to, feel free to leave a reaction to this answer!
Enjoy and good luck, Jonas
P.s., don't use math.floor only for the rounding! Also use math.ceil, and let the script check which one is the closest to the mouse position. (If on 0.5, it has to be rounded to 1!)