Issue with placement grid letting blocks go inside of eachother

i am trying to make my grid building system not let you place blocks inside of each other

on 2 of the axis it lets me place blocks inside of each other
Video of what is going on

i have tried 2 tutorials

here is the local script for the placement grid that i made

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local tool = script.Parent
local block = game.ReplicatedStorage:WaitForChild("Blocks"):WaitForChild(script.Parent.Name)

local ghostBlock

local step = 3
local positions = {}
tool.Equipped:Connect(function()
	ghostBlock = block:Clone()
	ghostBlock.Parent = workspace
	ghostBlock.Anchored = true
	ghostBlock.CanCollide = false
	ghostBlock.Transparency = 0.5
	mouse.TargetFilter = ghostBlock
	repeat
		wait()
		
		local mousePos = mouse.hit.Position
		local xPos = math.round(mousePos.X / step) * step
		local yPos = math.round(mousePos.Y/ step) * step + 1.5
		local zPos = math.round(mousePos.Z / step) * step
		
		if not table.find(positions,Vector3.new(xPos,yPos,zPos)) then
			
			ghostBlock.Position = Vector3.new(xPos,yPos,zPos)
		else
			print(xPos,yPos,zPos)
			ghostBlock.Position = Vector3.new(xPos,yPos,math.abs(zPos))
		end
		
		
		
	until tool.Parent == player.Backpack
	ghostBlock:Destroy()
end)



tool.Activated:Connect(function()
	

	game.ReplicatedStorage.Place:FireServer(ghostBlock.Position,tool.Name)
	table.insert(positions,ghostBlock.Position)
end)

Rounding right on the edge of two numbers is what’s causing it. You should add a little bit of the surface normal to the position before rounding so its further away from the edge.

1 Like

sorry, i dont understand really what you mean by this. could you explain more?

If you add the direction of the surface to your position before rounding it, the number will be “deeper” inside the correct cube and thus wont be able to round in the wrong direction.

thanks for the help heres what i ended up doing to fix this issue:

1. i made a table of all surface vectors

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),
}

2. i got the target surface from the mouse and got the SurfaceVector

local surface = mouse.TargetSurface.Name
		local surfaceVector = SurfaceVectors[surface]

3. i added the surface vector axes

local adjustedX = mousePos.X + surfaceVector.X
local adjustedY = mousePos.Y + surfaceVector.Y
local adjustedZ = mousePos.Z + surfaceVector.Z

4. i did the grid placement math

local xPos = math.round(adjustedX / step) * step 
local yPos = math.round(adjustedY/ step) * step + 1.5
local zPos = math.round(adjustedZ / step) * step

5. i set the position of the ghostBlock to the new x,y,z

ghostBlock.Position = Vector3.new(xPos,yPos,zPos)

if anyone would like my completed selection script then here it is

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local tool = script.Parent
local block = game.ReplicatedStorage:WaitForChild("Blocks"):WaitForChild(script.Parent.Name)

local ghostBlock

local step = 3
local positions = {}

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),
}

tool.Equipped:Connect(function()
	ghostBlock = block:Clone()
	ghostBlock.Parent = workspace
	ghostBlock.Anchored = true
	ghostBlock.CanCollide = false
	ghostBlock.Transparency = 0.5
	mouse.TargetFilter = ghostBlock
	repeat
		wait()
		local surface = mouse.TargetSurface.Name
		local surfaceVector = SurfaceVectors[surface]
		local mousePos = mouse.hit.Position
		local adjustedX = mousePos.X + surfaceVector.X
		local adjustedY = mousePos.Y + surfaceVector.Y
		local adjustedZ = mousePos.Z + surfaceVector.Z
		local xPos = math.round(adjustedX / step) * step 
		local yPos = math.round(adjustedY/ step) * step
		local zPos = math.round(adjustedZ / step) * step
		ghostBlock.Position = Vector3.new(xPos,yPos,zPos)

		
		
		
	until tool.Parent == player.Backpack
	ghostBlock:Destroy()
end)



tool.Activated:Connect(function()
	

	game.ReplicatedStorage.Place:FireServer(ghostBlock.Position,tool.Name)
	table.insert(positions,ghostBlock.Position)
end)

Can you mark my answer as solution since you did what I said to do?

it did help me however i did get help from someone on discord but you did help contribute

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