Mouse.Hit.Position being weird

So i’m trying to make a building game, but when I use Mouse.Hit.Position something very weird happens. take a look.

what.wmv

here’s the 2 scripts i have

the one that places the block when I click (not shown in the vid):

wait()
script.Parent.Activated:Connect(function(Touch)
	local Player = game.Players.LocalPlayer
	local Mouse = Player:GetMouse()
	
	local NewPart = Instance.new("Part")

	NewPart.Parent = workspace

	NewPart.Anchored = true
		
	NewPart.Position = Mouse.Hit.Position
		
end)

thing that makes the part that shows where the part will be placed:

script.Parent.Equipped:Connect(function()
	local Player = game.Players.LocalPlayer
	local Mouse = Player:GetMouse()	
	local NewPart = Instance.new("Part")
	
	NewPart.Material = Enum.Material.ForceField	
	NewPart.CanCollide = false
	NewPart.Parent = workspace		NewPart.Anchored = true		
	while true do
		NewPart.Position = Mouse.Hit.Position
		wait()
	end
end)

I’m probably being very stupid with my code because I’m new to it.

The problem is Mouse.Hit is hitting your preview part. Simple solution is to add the part as Mouse.TargetFilter:

local Mouse = Player:GetMouse()
local NewPart = Instance.new("Part")
Mouse.TargetFilter = NewPart   -- With this, Mouse.Hit ignores your new part

Mouse.TargetFilter is limited to a single instance though. Consider using rays if you need add an entire ignore list of instances.


As for some off-topic tips, consider that your while true do loop will run forever even if the tool is unequipped. I would recommend you use a variable instead. For example:

local isEquipped = false

tool.Equipped:Connect(function()
  isEquipped = true
  while isEquipped do
    ...
  end
end)

tool.Unequipped:Connect(function()
  isEquipped = false
end)
1 Like

Oh! I never thought of my part colliding with my mouse. Thanks!