How do I get mouse click position/tap position with context action service

So currently I want to use context action service to get the mouse click position and tap position, how do I do this.

You can look at the contextactionsrrvice documentation here for your answers ContextActionService | Roblox Creator Documentation

I already read over that I couldn’t find much for mouse posiiton.

Interesting, you can use UserInputType | Roblox Creator Documentation with context action services it’s said right there in the documentation.

Documemtatiom ContextActionService | Roblox Creator Documentation

Any number of Enum.KeyCode or Enum.UserInputType representing the inputs to bind to the action

So you can bind touch the CAS or mouse to the CAS function

Edit: NVM there actually is a method as CAS bind action returns the input object containing position and delta of a mouse wonder if it works for touch?

Perhaps this should work

2 Likes

Yeah as the post above says it says you can on the documentation I sent

I can bind the actions pretty easily, but is there a way to get the position of the mouse or touch in world space from context action service or do I have to get it from the other services. Though the position you are talking about is screen position not the world position.

Basically I bind an action normally that detects mouse button click and touchpad touch. I just want to see the position in world space of that click or touch.

Yeah, you can just use UIS for that and this function to convert 2d screen position to 3D, previous post on this. TL;DR uses Camera | Roblox Creator Documentation or viewport point to ray.

local RAY_DISTANCE = 1000 -- default ray distance like mouse.Hit
local cam = workspace.CurrentCamera

function hitPosition(screenPosition,raycastParams, distance)
	local mousePos = screenPosition--modifying previous function a bit
	local viewportMouseRay = cam:ViewportPointToRay(mousePos.X, mousePos.Y)
	local rayCastResult = workspace:Raycast(viewportMouseRay.Origin, viewportMouseRay.Direction * (distance or RAY_DISTANCE), raycastParams)
	local hitPosition
	if rayCastResult then
		hitPosition =  rayCastResult.Position
	else
		hitPosition = viewportMouseRay.Origin+viewportMouseRay.Direction * (distance or RAY_DISTANCE)
	end
	return hitPosition
end
1 Like

Yea thanks this also works, also has more uses then mouse position.

@superbolt999 The answer is briefly mentioned here. To be more specific, the “actionHandler” function (the function that is called when a ContextAction is triggered) has 3 parameters, with the third being the InputObject. It is the InputObject where you get the touch or mouse position. Refer to the Position property of InputObject.

Example:

local function OnClick(actionName, inputState, inputObject)
	if inputState ~= Enum.UserInputState.Begin then return end
	local pos = inputObject.Position
end

ContextActionService:BindAction("ClickEvent", OnClick, false, Enum.UserInputType.MouseButton1, Enum.UserInputType.Touch)

Note that pos will be a vector3, but only the X and Y of the Vector3 will be set based on the mouse/touch position.

If this has answered your question, please mark this response or dthecoolest reply as “Solved” so future viewers can benefit from this answer. While you can use UIS, your question is more related to getting the mouse position with ContextActionService. Special thanks to @dthecoolest as their reply helped me solve this issue.