I'm making a game with ray casting it has a main object where it is connected and the ray casting should resize and move the end of the part to where the mouse are like in mini golf and only in the z axis but the part goes in the Y axis when i don't want it to when i have to mouse close to a another part why? and the end of the part isnt where the mouse is why?

Script:

local player = game.Players.LocalPlayer
character = player.Character

local mouse = player:GetMouse()
local ball = workspace.Ball

mouse.Button1Down:connect(function()
	if (ball.Velocity == Vector3.new(0,0,0)) then
		workspace.Ball.Velocity = (mouse.hit.p - workspace.Ball.Position).unit * ((workspace.Ball.Position - mouse.hit.p).magnitude)*5
	end
end)

coroutine.resume(coroutine.create(function()
		p = Instance.new("Part", workspace)
		p.Anchored = true
		p.CanCollide = false
		while wait() do
			local ray = Ray.new(workspace.Ball.Position, (mouse.hit.p - workspace.Ball.Position).unit * 200)
			local distance = ((workspace.Ball.Position - mouse.hit.p).magnitude*5)
			p.Size = Vector3.new(1,0.1,distance)
			p.CFrame = CFrame.new(workspace.Ball.Position, mouse.hit.p)*CFrame.new(0, 0, -distance/2)
			p.Color = Color3.new(1,0,0)
		end
end))

while wait(1) do
	print((workspace.Ball.Position - mouse.hit.p).magnitude*5)
	if (math.abs(ball.Velocity.Z) < 1) then
		ball.Velocity = Vector3.new(0,0,0)
	end
	if (math.abs(ball.Velocity.Z) == 0) then
		print("feel free to hit")
		wait(2)
	end
end