Alright so I'm trying to make a gui button that when clicked, it inserts a model that is moved to the mouse until the player clicks to place it. Everything works so far, but I'm trying to get it to align to a stud grid of 25 studs so that everything is even. The script:
InsertId = 184711756 Player = script.Parent.Parent.Parent.Parent Mouse = Player:GetMouse() local Spawn = game.Workspace.Start Clicked = false Placed = false function Insert() if Clicked == true then return end Clicked = true Placed = false local InsertedObject = game:GetService("InsertService"):LoadAsset(InsertId) InsertedObject.Parent = Player.PlayerGui local Search = InsertedObject:GetChildren() for i = 1, #Search do Search[i].Parent = game.Workspace.Camera InsertedObject:Remove() InsertedObject = Search[i] end InsertedObject.PrimaryPart = InsertedObject.Tile local SB = Instance.new("SelectionBox", Player.PlayerGui) SB.Color = BrickColor.new("Teal") SB.Name = "Place" SB.Adornee = InsertedObject.Tile while Placed == false do wait() if (Mouse.Hit.p - Player.Character.Torso.Position).magnitude <= 100 then if InsertedObject.Tile.Position.Y == Spawn.Position.Y then SB.Color = BrickColor.new("Lime green") else SB.Color = BrickColor.new("Really red") end InsertedObject:MoveTo(Vector3.new(Mouse.Hit.p.X+12.5, Spawn.Position.Y, Mouse.Hit.p.Z+12.5)) else SB.Color = BrickColor.new("Really red") end end end function Place() if Clicked == false then return end if Placed == true then return end if Player.PlayerGui:findFirstChild("Place") then if Player.PlayerGui.Place.ClassName == "SelectionBox" then if Player.PlayerGui.Place.Color == BrickColor.new("Lime green") then Placed = true Clicked = false local Search = game.Workspace.Camera:GetChildren() for i = 1, #Search do Search[i].Parent = game.Workspace end Player.PlayerGui.Place:Remove() end end end end Mouse.Button1Down:connect(Place) script.Parent.MouseButton1Click:connect(Insert)
In order to round any number so it's a factor of a particular value, 25 in your instance, you need to subtract off how far above the number you went. Roblox has a great operation that allows you to find the number you need to subtract. It's called the modulus operation and is represented by a %.
Example: 18%4 = 2 (Because 18/4 = 4 * 4 + 2)
To implement this, we can find the remainder and subtract it from the original number:
InsertedObject:MoveTo(Vector3.new(Mouse.Hit.p.X - Mouse.Hit.p.X%25, Spawn.Position.Y, Mouse.Hit.p.Z - Mouse.Hit.p.Z%25))
This will move it if it based on the centre of the brick, but in your case you want to move it based on the edges. To do this, we simply add half the rounding value to the position.
InsertedObject:MoveTo(Vector3.new((Mouse.Hit.p.X+12.5) - (Mouse.Hit.p.X+12.5)%25, Spawn.Position.Y, (Mouse.Hit.p.Z+12.5) - (Mouse.Hit.p.Z+12.5)%25))
Now just put this in your script, and you should be good to go!