Detecting where a frame is pointed towards like mouse.Target.p

Hello, today I’m trying to change up my previous script a bit. Previously it took the mouse object and got the part it was pointing towards.

With me now trying to make my game more mobile friendly, I’m trying to fix a few bugs I found while emulating a mobile device. One bug involved this, and now I’m trying to get where a GUI frame object if pointed to, not the mouse.

If you don’t understand, I have set up a gui frame object that acts as the cursor, like shown below, and since mobile touches are different from a player’s mouse, I’m completely switching over to the frame object.


(the tiny dot in the center of the sceen)

This code gets the player’s mouse, not the frame object.

local function getMouseAndTargetLocation(cursorPosition)
    local ray = workSpace.CurrentCamera:ScreenPointToRay(cursorPosition.x, cursorPosition.y, 1)
    ray = Ray.new(ray.Origin, ray.Direction * 999)
    return workSpace:FindPartOnRay(ray)
end

local function updateTooltip()
	local text = ""
	local target = getMouseAndTargetLocation(uiService:GetMouseLocation())

	if target then
		if target.Parent:FindFirstChild("Settings") then
			if target.Parent.Settings:FindFirstChild("interact") then
				if player:DistanceFromCharacter(target.Position) <= target.Parent.Settings.interact.distance.Value then
					text = target.Parent.Settings.interact.tooltip.Value
				end
			end
		end
	end

	cursor.Text = text
end

(this runs every Humanoid.Running, and so far it’s working pretty well.)

You just need to change your ScreenPointToRay parameters to the frame’s AbsolutePosition

You didn’t really ask a question. What are you trying to achieve? That’s not clear to me after reading the thread a few times.

A note that if you’re trying to search for a point in 3D space, you should use ViewportPointToRay to get more accurate results, as ViewportPoint ignores the GUI inset. Furthermore, you should be using an InputObject position over GetMouseLocation. Use InputBegan over Humanoid.Running.

That aside, if you’re trying to get a Gui object where your pointer is, you’re looking for PlayerGui.GetGuiObjectsAtPosition.

Thanks! Sorry if I couldn’t explain it clearly, it took me a while to get it this clear. I’m not good at explaining things sometimes.