Grid based building system only building on 3 sides

What do you want to achieve?

I am creating a grid-based building system.

What is the issue?

Everything works but I can only build in 3 directions.

What solutions have you tried so far?

I searched on the developer forum and got a few answers. They worked but the code was very messy so I didn’t understand it.

Code

BlockBuilding Module
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Camera = game:GetService("Workspace").CurrentCamera
local Modules = ReplicatedStorage:WaitForChild("Modules")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local BlockDatabase = require(Modules:WaitForChild("BlockDatabase"))
local PlayerSettings = require(Modules:WaitForChild("PlayerSettings"))
local mathplus = require(Modules:WaitForChild("mathplus"))
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()

local BlockBuilding = {}

function BlockBuilding:SetSelectionVisualizer()
	if Mouse.Target then
		if Camera:FindFirstChild("SelectionVisualizer") then
			game:GetService("Workspace").Camera:WaitForChild("SelectionVisualizer").Adornee = Mouse.Target
		else
			local SelectionVisualizer = Instance.new("SelectionBox", Camera)
			SelectionVisualizer.Name = "SelectionVisualizer"
			SelectionVisualizer.Color3 = Color3.new(0, 0, 0)
			SelectionVisualizer.LineThickness = 0.01
			SelectionVisualizer.Transparency = 0
			SelectionVisualizer.SurfaceTransparency = 1
			BlockBuilding:SetSelectionVisualizer()
		end
	end
end

function BlockBuilding:Place(BlockName)
	if BlockDatabase[BlockName] then
		local MouseHit = Mouse.Hit
		local Position = Vector3.new(
			mathplus.round(MouseHit.X, BlockDatabase["Size"]),
			mathplus.round(MouseHit.Y, BlockDatabase["Size"]),
			mathplus.round(MouseHit.Z, BlockDatabase["Size"]))
		Remotes:WaitForChild("PlaceBlock"):FireServer(BlockName, Position)
	else
		print(BlockName .. " is not a valid block!")
	end
end

return BlockBuilding
BlockBuilding LocalScript

(Forum decided to mess up the formatting on this one, I had to redo it here)

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Modules = ReplicatedStorage:WaitForChild("Modules")
local BlockBuilding = require(Modules:WaitForChild("BlockBuilding"))
local BlockDatabase = require(Modules:WaitForChild("BlockDatabase"))

RunService:BindToRenderStep("SelectionVisualizer", Enum.RenderPriority.Last.Value, function()
    BlockBuilding:SetSelectionVisualizer()
end)

UserInputService.InputBegan:Connect(function(Input)
    if Input.UserInputType == Enum.UserInputType.MouseButton2 then
        BlockBuilding:Place("Grass")
    end
end)
BlockBuilding Script

(Forum decided to mess up the formatting on this one, I had to redo it here)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Modules = ReplicatedStorage:WaitForChild("Modules")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local BlockDatabase = require(Modules:WaitForChild("BlockDatabase"))
local Blocks = game:GetService("Workspace"):WaitForChild("Blocks")

Remotes:WaitForChild("PlaceBlock").OnServerEvent:Connect(function(Player, BlockName, Position)
    local Block = BlockDatabase[BlockName]["Directory"]:Clone()
    Block.Position = Position
    Block.Parent = Blocks
end)
Rounding Module

(Forum decided to mess up the formatting on this one, I had to redo it here)

local mathplus = {}

function mathplus.round(num, mult)
    return math.floor(num / mult + 0.5) * mult
end

return mathplus
1 Like