Mouse Position on a flat space?

I want to make a part follow the mouse, and Hit.Position works but whenever I hover onto the skybox, the part shoots away until I put the mouse back on the sky. How would I make it so the part just follows the mouse exactly, without detecting anything else?
The code that does this is
chr.Position = Vector3.new(mouse.Hit.Position.X,mouse.Hit.Position.Y, 0)

Try using ScreenPointToRay or ViewportPointToRay.
https://developer.roblox.com/en-us/api-reference/function/Camera/ScreenPointToRay

Example code:

local p = workspace:WaitForChild('Part') --a part parented to workspace
local cam = workspace.CurrentCamera
local mouse = game:GetService('Players').LocalPlayer:GetMouse()
game:GetService('RunService').RenderStepped:Connect(function()
	local ray = cam:ScreenPointToRay(mouse.X, mouse.Y, 20) --"20" is the depth from the camera
	p.Position = ray.Origin
end)

It works, but is there a way to get rid of the depth thing so it’s just a flat X and Y without changing the Z?

Z doesn’t change relative to the player’s camera. I’m not sure what you mean.

You want the intersection of Mouse | Roblox Creator Documentation and the X-Y plane. You could also get the unit ray through Camera | Roblox Creator Documentation, but you still would need to calculate the intersection with the XY plane.

Google “line-plane intersection” and implement it, where the line is the UnitRay and the plane is the XY plane. It will probably be easier than the general case, since Z of the plane is always zero.

While I didn’t do exactly what you said, this made me look into what I should do. Thank you both.