Raycast attachment position set problem

So Im trying to make a raycast that when u click a part, dummy it creates a new attachment and sets it to the mouse position where the mouse is on the part/dummy
But this is my code and it places the attachment all the way to china. Do you know how to fix?

local camera = workspace.CurrentCamera
local att
local att2
local runservice = game:GetService("RunService")
function raycast()
	local mouse = uis:GetMouseLocation()
	local ray1 = camera:ViewportPointToRay(mouse.X, mouse.Y)
	local ray2 = workspace:Raycast(ray1.Origin, ray1.Direction * 100)

	
	
	return ray2
	
end

uis.InputBegan:Connect(function(int)
	if int.UserInputType == Enum.UserInputType.MouseButton1 then
		local result = raycast()
		if result and result.Instance then
		 att = Instance.new("Attachment")
			att2 = Instance.new("Attachment")
			att.Parent = result.Instance
			att.Position = result.Position
			end
	end
end)

uis.InputEnded:Connect(function(int)
	if int.UserInputType == Enum.UserInputType.MouseButton1 then
		local result = raycast()
		if result and result.Instance then
			att2.Parent = result.Instance
			att2.Position = result.Position
		end
	end
end)

runservice.Heartbeat:Connect(function()
	local result = raycast()
	if result and result.Instance then
		
		
	end
end)

Attachment positions are in local space, the position returned by raycasting is in world space. you need to use CFrame:PointToObjectSpace()

And how could I get that working. Hard time finding how. att.CFrame = CFrame:PointToObjectSpace(result.Position)?

att.Position = result.Instance.CFrame:PointToObjectSpace(result.Position)

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