How to move a model to the mouse

I’m trying to make a tool that can build terrain.
I have this part that shows where you can build and I’m moving it to the mouse using Mouse.Hit.Position but it keeps moving up off the ground like this

How do I stop it from doing this?

Here’s my code so far:

local tool = script.Parent
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local PlacementIndicator = game.ReplicatedStorage:FindFirstChild("PlacementIndicator")
local equipped = false

local currentIndicator
tool.Equipped:Connect(function()
	equipped = true
	currentIndicator = PlacementIndicator:Clone()
	currentIndicator.Parent = workspace
	
	while equipped == true do
		currentIndicator:MoveTo(Vector3.new(math.round(mouse.Hit.Position.X), math.round(mouse.Hit.Position.Y), math.round(mouse.Hit.Position.Z)))  
		wait()
	end

end)

tool.Unequipped:Connect(function()
	equipped = false
	currentIndicator:Destroy()
end)
1 Like

This is a very common issue and is easily fixable. Your mouse gets on the position of the object your putting there (in this case currentIndicator).

Following should fix it:

mouse.TargetFilter = currentIndicator

Add this line right before the while loop begins.

-- code before comes here
currentIndicator.Parent = workspace

mouse.TargetFilter = currentIndicator
while equipped == true do
-- code continues

This should fix it.

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.