How to do mouse.Target with console players?

I am currently trying to add console-compatibility to my game however, to do so i need to find out which item the player’s cursor (on console) is on.

I have this so far for the console compatible controls:

userinputservice.InputBegan:Connect(function(input, gameprocessedevent)
	if input.KeyCode == Enum.KeyCode.ButtonR2 then
		if mouse.Target and mouse.Target.Parent:IsA("Model") and mouse.Target.Parent:FindFirstChild("Grabbable") then
			if not game.Players:FindFirstChild(mouse.Target.Parent.Name) and not mouse.Target.Parent.PrimaryPart:FindFirstChild("IsGrabbed") then
				grabbing = true
				grabbeditem = mouse.Target.Parent.PrimaryPart
				_G.Item = grabbeditem
				game.ReplicatedStorage.ItemGrabbed:FireServer(grabbeditem)
				local bp = Instance.new("BodyPosition", grabbeditem)
				bp.D = 150
				bp.P = 2000
				grabbeditem.Parent.Outline.Transparency = 0
			end
		end
		
	elseif input.KeyCode == Enum.KeyCode.ButtonL2 then
		
		if grabbing == true then
			facing = true
		end
		
	end
end)

userinputservice.InputEnded:Connect(function(input, gameprocessedevent)
	if input.KeyCode == Enum.KeyCode.ButtonR2 then
		grabbing = false
		facing = false
		
		if grabbeditem and grabbeditem:FindFirstChild("BodyPosition") then
			game.ReplicatedStorage.ItemReleased:FireServer(grabbeditem, false)
			grabbeditem.Parent.Outline.Transparency = 1
			grabbeditem.BodyPosition:Destroy()
		end
		
		if grabbeditem and grabbeditem:FindFirstChild("BodyGyro") then
			grabbeditem.BodyGyro:Destroy()
		end
		
		_G.Item = nil
		
	elseif input.KeyCode == Enum.KeyCode.ButtonL2 then
		
		facing = false
		grabbing = false
		if grabbeditem and grabbeditem:FindFirstChild("BodyPosition") then
			grabbeditem.Parent.Outline.Transparency = 1
			grabbeditem.BodyPosition:Destroy()
			grabbeditem.Velocity = camera.CFrame.LookVector * 150
			game.ReplicatedStorage.ItemReleased:FireServer(grabbeditem)
		end
		
		if grabbeditem and grabbeditem:FindFirstChild("BodyGyro") then
			grabbeditem.BodyGyro:Destroy()
		end
		
		_G.Item = nil
		
	end
end)

The “Mouse.Target” Is what i’m not sure will work and am trying to change.

2 Likes

Since InputObjects have a Position property which is a Vector2 of where on the screen the Input happened, you could create your own system to get a Target.

ScreenPointToRay can be given the X and Y of the Position and then you can use FindPartOnRay to find what part the ray’s hitting.

Here’s an example of a function to accomplish this:

local function GetTarget(Input)
    local Position = Input.Position
    local Ray = workspace.CurrentCamera:ScreenPointToRay(Position.X, Position.Y)
    local Part = workspace:FindPartOnRay(Ray)

    return Part
end
3 Likes

Thank you, and how would i go about doing it if there’s no input, for example the player’s cursor only hovers over the item?

If they were to hover then the InputChanged event would fire, this includes an InputObject.

Thanks again, you’ve been an awesome help!

1 Like

Use ViewportPointToRay to be more accurate. ScreenPointToRay accounts for the Gui inset, ViewportPointToRay doesn’t and instead takes into account the whole visible screenspace available.

4 Likes