Part Not Following Mouse

The part is supposed to follow the mouse, but it dosen’t. It dosen’t even print 2.

print('1')

local part = script:WaitForChild('Part')
local uis = game:GetService('UserInputService')

uis.InputBegan:Connect(function(key, is)
	if is then
		return
	end
	
	if key.KeyCode == Enum.UserInputType.MouseMovement then
		print('2')
		part.Parent = workspace.Terrain
		part.Position = Vector3.new(uis:GetMouseLocation().X, 0, uis:GetMouseLocation().Y)
	end
end)
1 Like

That’s because you’re trying to check if a KeyCode enum is equal to a UserInputType enum, use the UserInputType property of the input object to check that its value is what you want:

if key.UserInputType == Enum.UserInputType.MouseMovement then

Also I’m not 100% sure on this one but I’m fairly certain that you have to use the .InputChanged event for mouse movement, not InputBegan

2 Likes

Part is situated in a 3d workspace so it’s point will be defined in 3 dimensions which is a Vector3.
While mouse location is on your screen and has a 2d position , Vector2.
Part position is missing the z axis value.
And another thing, mouse position on the screen doesn’t really give you the actual position of where the part should be because mouse position is calculated in pixels if I am not wrong.
So if the mouse position is 200 pixels up from the Origin and if you set part’s y axis value equal to mouse’s y value it will set the part 200 studs upwards.
So this method won’t work.

1 Like