I tried making placement grid to whole world ( minecraft-like )

So I tried making a placement grid but i don’t have many experience with roblox coding so i figured out something on my own instead of youtube videos etc bc its too complicated for me yet. I created this code which works but not very well. The issue here is that when i place a block on some surface, it places it inside the block but this doesnt happen all the time (block on block worked for some sides and when i built another structure it worked in the opposite directions but didnt on the same ones) so i dont really know what is going on.

Sorry for my explanation, im not good at explaining things and thanks for your time. :slight_smile:

Here is the local script( server script just places the block so its not really needed to put here ):

local mouse = game:GetService("Players").LocalPlayer:GetMouse()
local RS = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local preview
local block

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.R then
		if preview == nil then
			block = RS:WaitForChild("Block")
			preview = block:Clone()
			preview.Transparency = 0.5
			preview.CanCollide = false
			preview.CanQuery = false
			preview.CastShadow = false
			preview.Parent = game:GetService("Workspace")
		else
			preview:Destroy()
			preview = nil
		end
	end
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 and mouse.Target and preview then
		RS:WaitForChild("RemoteFunction"):InvokeServer(block, preview.Position)
	end
end)

spawn(function()
	while wait() do
		if preview then
-- this is the part of code that causes the issues, most likely..
			preview.Position = Vector3.new(math.round(mouse.Hit.X/preview.Size.X)*preview.Size.X,math.round(mouse.Hit.Y/preview.Size.Y)*preview.Size.Y,math.round(mouse.Hit.Z/preview.Size.Z)*preview.Size.Z)
----------------------------------------------------------------
		end
	end
end)

Edit:

I just realised that the placement depends on the 6 coordinates, whenever they are positive or negative. So basically if the y coordinate is higher than 0 it will place blocks up and if it is lower then 0 it will place blocks down…

Any idea how to fix this without some complicated stuff

1 Like

Do you have issues if the mouse Position isn’t acutally on the Part? I’m wondering if the transparent Part is aligned on the part below it but the mouse is moving slightly off the edge of the Part and ‘hitting’ the surface behind.
Does it get better when the camera is closer to the Part and worse the farther the camera is from the surface?

i just realised that the placement depends on the 6 coordinates, whenever they are positive or negative. So basically if the y coordinate is higher than 0 it will place blocks up and if it is lower then 0 it will place blocks down…

Any idea how to fix this without some complicated stuff

thanks but thats not the issue, i just found out what it is but i dont know how to fix it. Could you look into it (its reply above this one) ?

If the placement block Y Position > the 1st block you are placing it on then make the placement block on top.
If it’s = place it on the side the mouse hits.
If it’s < the 1st block Y Position then put the placement block under the 1st block.

yeah it also came to my mind, ill just need to come up with the code, it will take some time but i think i can do it