Im trying to make a basic snap to grid system for placing blocks however the blocks sometimes phase through eachother on certain axises
My code currently looks like this
local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local block = game.ReplicatedStorage.Block:Clone()
block.Parent = workspace
local mouse = player:GetMouse()
mouse.TargetFilter = block
local grid = 4
local function snap()
local pos = mouse.Hit
local target = mouse.Target
local X = math.floor(pos.X/grid+0.5)*grid
local Y = math.floor(pos.Y/grid + 0.5) * grid
local Z = math.floor(pos.Z/grid+0.5)*grid
return Vector3.new(X,Y,Z)
end
mouse.Move:Connect(function()
local newpos = snap()
print(newpos)
block.Position = newpos
end)
UIS.InputBegan:Connect(function(input,gameProcessed)
if not gameProcessed then
if input.UserInputType == Enum.UserInputType.MouseButton1 then
local newblock = block:Clone()
newblock.Parent = workspace
newblock.CanCollide = true
end
end
end)
This is what the snap to grid turns out to do and from what i see when it is used on the negative axis it phases through blocks
Does anyone know a fix?