Building with Blocks (with grid lock)

G’evening, afternoon or morning!
Recently I started on a system for a building game, but as soon as I got to actually coding the building tool, I got completely clogged and stuck.

All the blocks are stored in ServerStorage, and I want the changes to be server-wide (trying not to use local scripts), plus all blocks are forced in a grid. Which direction should I head here? (examples would be appreciated, but aren’t needed)

2 Likes

This is about the simplest version of a block building tool possible:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local placeTool = script.Parent

local BLOCK_SIZE = 4
local RAY_MAX_LENGTH = 5000

placeTool.Activated:Connect(function()
	local raycastParams = RaycastParams.new()
	local raycastResult = game.Workspace:Raycast(
		mouse.UnitRay.Origin,
		mouse.UnitRay.Direction * RAY_MAX_LENGTH,
		raycastParams
	)
	
	local target = raycastResult and raycastResult.Instance
	if target and target.Parent == game.Workspace.Blocks then
		local normal = raycastResult.Normal
		local placedBlockPosition = target.Position + normal * 4
		game.ReplicatedStorage.PlaceBlock:FireServer(placedBlockPosition)
	end
end)

Put in a LocalScript in a Tool in StarterPack (set RequiresHandle to false).

Put the following in a server script in ServerScriptService:

game.ReplicatedStorage.PlaceBlock.OnServerEvent:Connect(function(player, position)
	local block = game.ServerStorage.Block:Clone()
	block.CFrame = CFrame.new(position)
	block.Parent = game.Workspace.Blocks
end)

This figures out where to place blocks when the player clicks, but doesn’t check if a given position is valid to place a block in, i.e. being on grid and there not being a block there already. Let me know if anything is hard to understand in the scripts, or if you would like help on making the improvements I suggested.

EDIT: Here’s a place file with the above working: Block Game.rbxl (23.2 KB)

2 Likes