Having problem visualizing part position

I am making a tool for building, and to see where the block is supposed to be, I use this half transparent holo of a block.
image
Tool works quite well, however, there seems to be a problem that sometimes the holo does not want to move to the position of mouse.
image
IDK why exactly this happens, but it is always the problem with new place parts, if you try doing the same on already existing one, like the structure that I set for testing, this issue never appears.
image
This made me thinking that issue is laying inside the tool code, but I can’t figure out why it does that.

I suspect that it is either issue with surfaces but could not find confirmaton.

Here is the code

local ContextActionService = game:GetService("ContextActionService")
local players = game.Players
local player = players.LocalPlayer 

local tool = script.Parent
local block = tool.Block
local holo = tool.BlockHolo
local mouse = player:GetMouse()

local holoOrientationFront = holo.CFrame.LookVector
local holoOrientationRight = holo.CFrame.RightVector
local holoOrientationUp = holo.CFrame.UpVector

local connection = mouse.Move:Connect(function()
	
	local worldSize = holo.CFrame:VectorToWorldSpace(holo.Size)
	local absoluteSize = Vector3.new(math.abs(worldSize.X), math.abs(worldSize.Y), math.abs(worldSize.Z))
	
	local mousePosition = Vector3.new(math.round(mouse.Hit.Position.X), math.round(mouse.Hit.Position.Y), math.round(mouse.Hit.Position.Z))

	local target = mouse.Target
	local targetSurface = mouse.TargetSurface
	
	local direction
	
	if targetSurface == Enum.NormalId.Top then
		direction = target.CFrame.UpVector
	elseif targetSurface == Enum.NormalId.Bottom then
		direction = -target.CFrame.UpVector
	elseif targetSurface == Enum.NormalId.Front then
		direction = target.CFrame.LookVector
	elseif targetSurface == Enum.NormalId.Back then
		direction = -target.CFrame.LookVector
	elseif targetSurface == Enum.NormalId.Left then
		direction = -target.CFrame.RightVector
	elseif targetSurface == Enum.NormalId.Right then
		direction = target.CFrame.RightVector
	end
	
	local step
	
	if direction == Vector3.new(1, 0, 0) then
		holo.Position = mousePosition + Vector3.new(direction.X * (absoluteSize.X / 2), 0, 0)
	elseif direction == Vector3.new(-1, 0, 0) then
		holo.Position = mousePosition + Vector3.new(direction.X * (absoluteSize.X / 2), 0, 0)
	elseif direction == Vector3.new(0, 1, 0) then
		holo.Position = mousePosition + Vector3.new(0, direction.Y * (absoluteSize.Y / 2), 0)
	elseif direction == Vector3.new(0, -1, 0) then
		holo.Position = mousePosition + Vector3.new(0, direction.Y * (absoluteSize.Y / 2), 0)
	elseif direction == Vector3.new(0, 0, 1) then
		holo.Position = mousePosition + Vector3.new(0, 0, direction.Z * (absoluteSize.Z / 2))
	elseif direction == Vector3.new(0, 0, -1) then
		holo.Position = mousePosition + Vector3.new(0, 0, direction.Z * (absoluteSize.Z / 2))
	end
	
end)

local function RotateAndPlace(actionName, inputState, inputObject) -- Rotate
	if inputObject.KeyCode == Enum.KeyCode.R and inputState == Enum.UserInputState.Begin then
		holo.CFrame = holo.CFrame * CFrame.Angles(0, math.rad(90), 0)
	elseif inputObject.KeyCode == Enum.KeyCode.T and inputState == Enum.UserInputState.Begin then
		holo.CFrame = holo.CFrame * CFrame.Angles(0, 0, math.rad(90))
	elseif inputObject.UserInputType == Enum.UserInputType.MouseButton1 and inputState == Enum.UserInputState.Begin then
		local clone = holo:Clone()
		clone.Transparency = 0
		clone.Parent = workspace.PlayerStructures
		clone.Name = "Block"
		clone.CanCollide = true
		clone.Anchored = false
		local weld = Instance.new("WeldConstraint")
		weld.Parent = clone
		weld.Part0 = clone
		weld.Part1 = mouse.Target
	end
end

tool.Equipped:Connect(function()
	ContextActionService:BindAction("Rotate and Place", RotateAndPlace, false, Enum.KeyCode.R, Enum.KeyCode.T, Enum.UserInputType.MouseButton1)
	holo.Transparency = 0.5
end)

tool.Unequipped:Connect(function()
	ContextActionService:UnbindAction("Rotate and Place")
	holo.Transparency = 1
end)

Have anybody faced similar issue in any way? I can also give a copy of place if more detailed inspection is needed.

1 Like
local tool = script.Parent
local block = tool.Block
local holo = tool.BlockHolo
local mouse = player:GetMouse()
mouse.TargetFilter = holo 

try adding the holo to the mouse filter

Sadly it did not work. Any other ideas?

I made it so the part is being placed on server side script. This completely removed the problem. But it makes me wonder how exactly that worked.