I have a grid for placing furniture and each grid square is 2 studs by 2 studs how can I make it so when you place it stays in the 2 by 2 grid lines. This is what I have so far:
local Player = game.Players.LocalPlayer local Mouse = Player:GetMouse() local mTarget local Down function clickObject() if Player.BuildMode.Value == true and Mouse.Target ~= nil and not game.Workspace.StarterPlot then mTarget = Mouse.Target print(mTarget) Mouse.TargetFilter = mTarget print(Mouse.TargetFilter) Down = true end end Mouse.Button1Down:Connect(clickObject) function mouseMove() if Down and mTarget then local posX, posY, posZ = Mouse.Hit.X, Mouse.Hit.Y, Mouse.Hit.Z posX = posX/2 local function round(posX) return math.floor(posX + 0.5) end posX = posX * 2 posY = posY/2 local function round(posY) return math.floor(posY + 0.5) end posY = posY * 2 posZ = posZ/2 local function round(posZ) return math.floor(posZ + 0.5) end posZ = posZ * 2 mTarget.Position = Vector3.new(posX, posY, posZ) end end Mouse.Move:Connect(mouseMove) function mouseDown() Down = false Mouse.Target = nil Mouse.TargetFilter = nil end Mouse.Button1Up:Connect(mouseDown)
Divide the Mouse X Value by 2(Your Grid Size) then round that number and then finally multiply by 2(Your Grid Size). Then you can only get a value that is a multiple of 2(Your grid size).
Do this for the X, Y and the Z
Hope this works
Sorry for all the confusion, heres a modified version of your script that'll work
local Player = game.Players.LocalPlayer local Mouse = Player:GetMouse() local mTarget local Down function clickObject() if Player.BuildMode.Value == true and Mouse.Target ~= nil and not game.Workspace.StarterPlot then mTarget = Mouse.Target print(mTarget) Mouse.TargetFilter = mTarget print(Mouse.TargetFilter) Down = true end end Mouse.Button1Down:Connect(clickObject) local function round(number) return math.floor(number + 0.5) end function mouseMove() if Down and mTarget then local posX, posY, posZ = Mouse.Hit.X, Mouse.Hit.Y, Mouse.Hit.Z posX = posX/2 posX = round(posX) * 2 posY = posY/2 posY = round(posY) * 2 posZ = posZ/2 posZ = round(posZ) * 2 mTarget.Position = Vector3.new(posX, posY, posZ) end end Mouse.Move:Connect(mouseMove) function mouseDown() Down = false Mouse.Target = nil Mouse.TargetFilter = nil end Mouse.Button1Up:Connect(mouseDown)