Put part infront of block with grid

This is going to be a bit hard to explain but I’ll try my best.

Basically, I’m working on a Minecraft building system, I wanted to make a prediction block that gets put in front of a block that you are hovering, if there is an empty space in front then the script puts a prediction block there, the only problem is that I’m using LookVector of Player’s HumanoidRootPart and it puts it depending on where the player is looking at, and if I use the block’s LookVector, it might face the wrong way.

I want the part to be put in front of the block according to the grid!


here is the code: (it is put in a RenderStepped by the way, the currenthoverpart variable is the block that the player is looking at, the mouseTargetSurface variable is the face of the block the player is looking at.)

 if mode	== "Placing" then
	if currenthoverpart and char and char.PrimaryPart then
		if currenthoverpart:IsA("BasePart") and (mouseTargetSurface == Enum.NormalId.Top) and currenthoverpart.Name ~= "PredictionPart" then

			local front = false

			for _, v in pairs(workspace.PlacedBlocks:GetChildren()) do
				if v.Position == currenthoverpart.Position + char.PrimaryPart.CFrame.LookVector * 4 + Vector3.new(0,currenthoverpart.Position.Y,0) then
					front = true
				end
			end

			local function makePrediction(newcf)
				local newPrediction = Instance.new("Part", workspace.PlacedBlocks)
				newPrediction.Name = "PredictionPart"
				newPrediction.Size = Vector3.new(4,4,4)
				newPrediction.Anchored = true
				newPrediction.CanCollide = false
				newPrediction.Transparency = .65
				newPrediction.Color = Color3.fromRGB(112, 74, 0)
				newPrediction.Material = Enum.Material.Concrete
				newPrediction.Position = newcf
			end

			if not front then
				makePrediction(currenthoverpart.Position + char.PrimaryPart.CFrame.LookVector * 4)
			end
		end
	end

I’ll try explaining better if this doesn’t make sense!

1 Like

You’ve got the mouseTargetSurface, just use that to determine where to place the block:

local dir = Vector3.FromNormalId(mouseTargetSurface)
makePrediction(currenthoverpart.Position + dir * 4)
2 Likes

My goal here is to put a prediction block in front of the block that you’re hovering depending on the direction the player is looking at.

I can’t use LookVector of HumanoidRootPart since it doesn’t snap to the grid.

Ah. I wrote a thing for snapping to axes here:

1 Like

Minutes after I posted my reply, I did some searching around and found a snap to grid function, applied it to my script and it managed to work!

if mode == “Placing” then
if currenthoverpart and char and char.PrimaryPart then
if currenthoverpart:IsA(“BasePart”) and (mouseTargetSurface == Enum.NormalId.Top) and currenthoverpart.Name ~= “PredictionPart” then

			local front = false
			
			local function snap(newcf)
				local posx = math.floor(newcf.X/4 + 0.5) * 4
				local posy = math.floor(newcf.Y/4 + 0.5) * 4
				local posz = math.floor(newcf.Z/4 + 0.5) * 4

				return Vector3.new(posx,posy,posz)
			end

			for _, v in pairs(workspace.PlacedBlocks:GetChildren()) do
				if v.Position == snap(currenthoverpart.Position + char.PrimaryPart.CFrame.LookVector * 4) then
					front = true
				end
			end

			local function makePrediction(newcf)
				local newPrediction = Instance.new("Part", workspace.PlacedBlocks)
				newPrediction.Name = "PredictionPart"
				newPrediction.Size = Vector3.new(4,4,4)
				newPrediction.Anchored = true
				newPrediction.CanCollide = false
				newPrediction.Transparency = .65
				newPrediction.Color = Color3.fromRGB(112, 74, 0)
				newPrediction.Material = Enum.Material.Concrete
				newPrediction.Position = snap(newcf)
			end

			if not front then
				makePrediction(currenthoverpart.Position + char.PrimaryPart.CFrame.LookVector * 4)
			end
		end
	end
end

I’m putting this here in case someone else faces the same problem but thank you for the help!

1 Like