How to make a grid mechanism for my placement system?

so, i am making a building game. and i am getting trouble with making a grid system

i completely don,t know how to make a one

i tried to seek tutorials in forums and youtube videos, but it seems so complicated

they all uses roblox math (something that i have no clue to use), and is needed to group parts as model (wich will take a ertenety, since its a lot of parts)

there is my script part that needs the grid:

local EventsFolder = script.Parent.Parent.Events.BuildingEvents
local BlockChoosen = EventsFolder.BlockChoosen
local PlaceBlock = EventsFolder.PlaceBlock

local Tool = EventsFolder.Parent.Parent

local Grid = 4

local BlockPOS = Vector3.new(0,0,0)
local BlockROT = Vector3.new(0,0,0)
local Block = nil

local BlockG

local SelectionBox = Tool.SelectionBox

BlockChoosen.Event:Connect(function(BlockChosen, BlockGroup)
	BlockG = BlockGroup
	Block = BlockChosen:Clone()
	SelectionBox.Adornee = Block
	Block.Parent = workspace.BlockPreviewArea

	if Block then
		Block.CFrame = CFrame.new(Vector3.new(BlockPOS.X, BlockPOS.Y, BlockPOS.Z))
		Block.CanCollide = false
		Block.Anchored = true
	end

	--	Block.Transparency = 0.5
end)

game.Players.LocalPlayer.CharacterAdded:Wait()
local Char = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local BlockMagnitude


local MaxMagnitude = 200


game.Players.LocalPlayer:GetMouse().Move:Connect(function()
	local MousesUnitRay = game.Players.LocalPlayer:GetMouse().UnitRay
	local ray = Ray.new(MousesUnitRay.Origin, MousesUnitRay.Direction * 100000)
	local i, POS = workspace:FindPartOnRayWithIgnoreList(ray, {Block, Char}, true)

	BlockPOS = game.Players.LocalPlayer:GetMouse().Hit.p




	if Block then
		


		if Block:IsA("Part") then

			BlockMagnitude = (Block.Position - Char.Position).Magnitude

			Block.CFrame = CFrame.new(Vector3.new(BlockPOS.X, BlockPOS.Y, BlockPOS.Z))
		elseif Block:IsA("Model") then
			BlockMagnitude = (Block.Position - Char.Position).Magnitude

			Block:PivotTo(CFrame.new(Vector3.new(BlockPOS.X, BlockPOS.Y, BlockPOS.Z)))
		end
		game.Players.LocalPlayer:GetMouse().TargetFilter = Block



		if BlockMagnitude <= MaxMagnitude then
			SelectionBox.Color3 = Color3.fromRGB(13, 105, 172)
			SelectionBox.SurfaceColor3 = Color3.fromRGB(13, 105, 172)
		else
			SelectionBox.Color3 = Color3.fromRGB(172, 0, 3)
			SelectionBox.SurfaceColor3 = Color3.fromRGB(172, 0, 3)
		end

	end
end)

can someone help me with this situation? (making it simpler and clear)

1 Like

Hello! You could do this either by manually placing grid tiles or by just using more math. I would recommend placing grid tiles, getting the mouse target instance and placing the object on that tile’s top surface position + half of the object’s size on the Y-axis

1 Like

You can round by using a very small amount of math.

It works because division can show how many times a number can “fit” in another number. 10/2 is like saying how many times can 2 fit inside of 10. If we get a whole number when we do this division that means the imputed value is already on our grid system. But most of the time it will have a decimal trailing behind, and we don’t want that because only whole numbers align with our grid. So the solution is to round that number to the closet whole number. But the issue is our whole number is only a fraction of what it should be because we divided it, so we need to undo our first step and multiply by our gridSize to get the final number.

local function roundToNumber(mouseInput,gridSize)
	local result = math.round(mouseInput/gridSize)*gridSize
	return result
end
block.Position.X = roundToNumber(Mouse.X,4) -- This will snap the position on the X axis to a grid system of 4 units

that is a good soluction, the problem is: my baseplate is too big. my baseplate is 2048x and 2048z, if i need to the player build in a specific area, i might not discard this soluction.

yeah, that works. thank you. just another problem that the block is locating at the half of the baseplate, or even get inside of a block, but i am finding a solution.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.