Get mouse target on mobile

local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local mouse = Player:GetMouse()

local function Collect(Input)
	if Input.KeyCode == Enum.KeyCode.F and script.Values.CurrentSelectedPart.Value then
		script.Server_Event.Value:FireServer("COLLECT", script.Values.CurrentSelectedPart.Value)
	end
end

local function HandleClick()
	script.Server_Event.Value:FireServer("NoString", script.Values.MouseTarget.Value)
end

-- Desktop: MouseButton1 click
UserInputService.InputBegan:Connect(function(Input, gameProcessedEvent)
	if not gameProcessedEvent then
		if Input.UserInputType == Enum.UserInputType.MouseButton1 then
			HandleClick()
		else
			Collect(Input)
		end
	end
end)

-- Mobile: Touch input
if UserInputService.TouchEnabled then
	UserInputService.TouchTap:Connect(function(_, gameProcessedEvent)
		if not gameProcessedEvent then
			HandleClick()
			print("ok")
		end
	end)
end

function AddOrRemoveSelectGui(WhatToDo, MODEL)
	if MODEL then
		if WhatToDo == "SELECT" then
			if script.Values.CurrentSelectedPart.Value then
				script.Values.CurrentSelectedPart.Value.Parent.ItemHoverGui:Destroy()
			end
			local HoverGui = script.ASSETS.ItemHoverGui:Clone()
			HoverGui.Parent = MODEL
			HoverGui.Itemname.Text = MODEL.Name
			script.Values.CurrentSelectedPart.Value = MODEL.DROPPED_ITEM_PRIMARY
			script.Values.LastSelectedPart.Value = MODEL

		end
	elseif WhatToDo == "CANCEL" then
		if script.Values.CurrentSelectedPart.Value then
			script.Values.CurrentSelectedPart.Value.Parent.ItemHoverGui:Destroy()
			script.Values.CurrentSelectedPart.Value = nil
			script.Values.LastSelectedPart.Value = nil
			warn("destroyed hovergui")
		end
	end
end

function getPartUnderMouse(Object)
	if Object.Parent:FindFirstChild("Pickable") and Object.Parent.Pickable.Value then
		script.Server_Event.Value:FireServer("SELECT", Object.Parent)
		AddOrRemoveSelectGui("SELECT", Object.Parent)
	else
		AddOrRemoveSelectGui("CANCEL")
	end
end

function CheckTarget()
	local Object = mouse.Target
	local Root = Player.Character and Player.Character:FindFirstChild("HumanoidRootPart")
	if Object and Root then
		script.Values.MouseTarget.Value = Object
		if (Root.Position - Object.Position).Magnitude <= script.Values.MaxSelectionDist.Value then
			getPartUnderMouse(Object)
		else
			AddOrRemoveSelectGui("CANCEL")
		end
	end
end

UserInputService.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		CheckTarget()
	end
end)

its not reacting to mousemovement on mobile which means it doesnt update the target.value. how can i make this mobile compitable?

there is no event that is the same as MouseMovement but for mobile. on pc the mouse is always in the game, but on mobile you can just take your fingers off the screen. if you want you can check tap/swipe events. otherwise maybe detect camera movement to compensate althiough it might not work for your case.

so uh how would i make that script mobile compitable

/: i am still looking for a working solution

If you want to get an instance or object then use raycast starting from the camera origin to the front of the camera.

You would have to use params to ignore the character model.
I’m not sure how much you know about raycast but this would be the best option for getting a target .

You’d want to use raycasting from the camera to a certain distance.

If you follow something like this, it should work,

1 Like

not exactly what i was looking for /: