Hello! I have a working grid based building system. The problem is, I want to be able to have multiple different block sizes with different grid sizes. I got grid sizes working, but when I place something with a different grid size on top of a block, it would float and not snap correctly.
Heres my code.
local xPos, zPos, yPos
local currentPos
local pos
local grid = 4
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local inputService = game:GetService("UserInputService")
local part = game.ReplicatedStorage:WaitForChild("Blocks").Cube
local hologram = part:Clone()
local hologramState = false
local world
for i, folder in pairs(game.Workspace:GetChildren()) do
if folder:IsA("Folder") and folder.Name == "World" then
world = folder
end
end
local surfaceVectors = {
Top = Vector3.new(0, 1, 0),
Bottom = Vector3.new(0, -1, 0),
Left = Vector3.new(-1, 0, 0),
Right = Vector3.new(1, 0, 0),
Front = Vector3.new(0, 0, -1),
Back = Vector3.new(0, 0, 1)
}
local function snap(normalID)
local surfaceVector = surfaceVectors[normalID]
xPos = mouse.Hit.Position.X + surfaceVector.X
yPos = mouse.Hit.Position.Y + surfaceVector.Y
zPos = mouse.Hit.Position.Z + surfaceVector.Z
xPos = math.round(xPos / grid) * grid
yPos = math.round(yPos / grid) * grid
zPos = math.round(zPos / grid) * grid
pos = CFrame.new(xPos, yPos, zPos)
end
local function move()
if mouse.Target then
if hologramState == false then
hologram = part:Clone()
grid = part.GridSize.Value
end
hologramState = true
local blockType = Instance.new("StringValue")
blockType.Name = "BlockType"
blockType.Parent = hologram
blockType.Value = part.Name
hologram.Parent = game.Workspace
hologram.Transparency = 0.5
hologram.CanCollide = false
hologram.Name = "Hologram"
mouse.TargetFilter = hologram
if mouse.Target then
currentPos = mouse.Target.CFrame
end
snap(mouse.TargetSurface.Name)
hologram.CFrame = pos
else
hologramState = false
hologram:Destroy()
end
end
mouse.Move:Connect(move)
mouse.Button1Down:Connect(function()
for i, block in pairs(world:GetChildren()) do if hologram.CFrame == block.CFrame and hologram.BlockType.Value == block.Name then return end end
local newPart = part:Clone()
newPart.Parent = world
newPart.CFrame = hologram.CFrame
wait()
move()
part = game.ReplicatedStorage:WaitForChild("Blocks").Pole
end)