mouse.TargetSurface not functioning as expected?

I’m just scripting but all of a sudden I discover a glitch/bug. (I’m creating a building tool, again.) Whenever I move my mouse, the part following starts glitching. I have a video to show, too:

robloxapp-20200702-1926000.wmv (1.1 MB)

The code:


tool.Equipped:Connect(function()
	addBlock:FireServer()
	local block = mouseBlocks:WaitForChild(player.UserId)
	mouse.TargetFilter = block
	mouse.Move:Connect(function()
		-- Right, Front, Top
		local x1 = math.floor(mouse.Hit.x / gridsnap + 0.5) * gridsnap
		local y1 = math.floor(mouse.Hit.y / gridsnap + 0.5) * gridsnap
		local z1 = math.floor(mouse.Hit.z / gridsnap + 0.5) * gridsnap	
		
		block.Position = Vector3.new(x1,y1,z1)
		if mouse.TargetSurface == Enum.NormalId.Bottom then
			block.Position = block.Position + Vector3.new(0,(gridsnap - (gridsnap * 2)),0) -- NOTE: (gridsnap - (gridsnap * 2)) = -4
		end
		if mouse.TargetSurface == Enum.NormalId.Top then
			block.Position = block.Position + Vector3.new(0,gridsnap,0)
		end
		if mouse.TargetSurface == Enum.NormalId.Front then
			block.Position = block.Position + Vector3.new(0,0,(gridsnap - gridsnap)) -- (gridsnap - gridsnap) = 0
		end
		if mouse.TargetSurface == Enum.NormalId.Back then
			block.Position = block.Position + Vector3.new(0,0,(gridsnap - gridsnap))
		end
		if mouse.TargetSurface == Enum.NormalId.Left then
			block.Position = block.Position + Vector3.new((gridsnap - (gridsnap * 1)),0,0)
		end
		if mouse.TargetSurface == Enum.NormalId.Right then
				block.Position = block.Position + Vector3.new((gridsnap - gridsnap),0,0)
			end
		wait()
	end)
end)

Nothing pops up in the Output, if you’re wondering.

Print everything, whever bugs like this come up that’s always that I do.

2 Likes

That won’t work in this situation, because everytime I move my mouse it randomly multiplies or divides.

Your mouse is hitting the block you’re trying to place, which will throw off your calculations.