Help to fix part getting stuck in ground when moved with mouse

I made a mouse move script which will move parts in your game.

Whenever I move the part it gets stuck in the ground.

Here’s my current code which moves the part and places it down with the mouse, I’m unsure how to make it not get stuck in the ground when I move it as you saw in the image above.


local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local mtarget
local down

function clickedOn()
	
		mtarget = mouse.Target
		mouse.TargetFilter = mtarget
	
		down = true
	end


mouse.Button1Down:Connect(clickedOn)

function mouseMove()
	if down and mtarget then
		local posX,posY,posZ = mouse.Hit.X,mouse.Hit.Y,mouse.Hit.Z
		
		mtarget.Position = Vector3.new(posX,posY,posZ)
	end
end

mouse.Move:Connect(mouseMove)

function mouseUp()
	
	down = false
	mtarget = nil
	mouse.TargetFilter = nil
end

mouse.Button1Up:Connect(mouseUp)
-- This is an example Lua code block

I would try using a offset on the Y Position. That way it will place it slightly above the actual mouse’s position.

1 Like

Try this:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local mtarget
local down

function clickedOn()
	
		mtarget = mouse.Target
		mouse.TargetFilter = mtarget
	
		down = true
	end


mouse.Button1Down:Connect(clickedOn)

function mouseMove()
	if down and mtarget then
		local posX,posY,posZ = mouse.Hit.X,mouse.Hit.Y,mouse.Hit.Z
		
		mtarget.Position = Vector3.new(posX,posY + (mtarget.Size.Y / 2),posZ)
	end
end

mouse.Move:Connect(mouseMove)

function mouseUp()
	
	down = false
	mtarget = nil
	mouse.TargetFilter = nil
end

mouse.Button1Up:Connect(mouseUp)

When position the part add to its CFrame half of the part’s height, since using part.Position will centre the part to that position.

1 Like

Thank you, so all you did was divide 2 from the Y to not get stuck.