Click not detected

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

-- For desktop: MouseButton1 click
UserInputService.InputBegan:Connect(function(Input)
	if Input.UserInputType == Enum.UserInputType.MouseButton1 then
		HandleClick()
	else
		Collect(Input)
	end
end)

-- For mobile: Touch input
if UserInputService.TouchEnabled then
	UserInputService.TouchTap:Connect(function(touchPositions, gameProcessedEvent)
		if not gameProcessedEvent then
			HandleClick()
		end
	end)
end

its not detecting any player clicking on the tree ):
but for desktop it works perfetly fine

2 Likes

Well, the reason why is because you’re detecting MouseButton1. Mobile players don’t have that.

No, he has the touchTap event connected too.
The issue would be script.Values.MouseTarget

@WHOTEI you’d have to show us how you’re updating that value cause for mobiles it doesn’t seem like its being updated, must be why.

1 Like

Thanks for your quick reply!
Here is the full localscript

task.wait(1)

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

-- For desktop: MouseButton1 click
UserInputService.InputBegan:Connect(function(Input)
	if Input.UserInputType == Enum.UserInputType.MouseButton1 then
		HandleClick()
	else
		Collect(Input)
	end
end)

-- For mobile: Touch input
if UserInputService.TouchEnabled then
	UserInputService.TouchTap:Connect(function(touchPositions, gameProcessedEvent)
		if not gameProcessedEvent then
			HandleClick()
		end
	end)
end

function AddOrRemoveSelectGui(WhatToDo, MODEL)
	if MODEL ~= nil and WhatToDo == "SELECT" then
		if script.Values.CurrentSelectedPart.Value ~= nil then
			script.Values.CurrentSelectedPart.Value = script.Values.LastSelectedPart.Value
		end
		local HoverGui = script.ASSETS.ItemHoverGui:Clone()
		HoverGui.Parent = MODEL
		HoverGui.Itemname.Text = MODEL.Name
		script.Values.CurrentSelectedPart.Value = MODEL.DROPPED_ITEM_PRIMARY
		if script.Values.LastSelectedPart.Value ~= nil then
			script.Values.LastSelectedPart.Value.ItemHoverGui:Destroy()
		end
		script.Values.LastSelectedPart.Value = MODEL
	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
		end
	end
end


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

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

-- Or use UserInputService for updates on mouse movement

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

You’re right. I didn’t see the scroll bar in OP ;-;

what. what are you talking about

1 Like

Do you need any other scripts or info?

its updating based on mouse target

can we see explorer for

script.Values.MouseTarget.Value

image

2 Likes

Can you try printing HandleClick() on the mobile touch input function to make sure that HandleClick() fired? It’s supposed to work if HandleClick() fired for both UserInputService functions.

EDIT: So like, do this:

-- For desktop: MouseButton1 click
UserInputService.InputBegan:Connect(function(Input)
	if Input.UserInputType == Enum.UserInputType.MouseButton1 then
		print("ok")
		HandleClick()
	else
		Collect(Input)
	end
end)

-- For mobile: Touch input
if UserInputService.TouchEnabled then
	UserInputService.TouchTap:Connect(function(touchPositions, gameProcessedEvent)
		if not gameProcessedEvent then
			print("ok")
			HandleClick()
		end
	end)
end
1 Like

– Make sure UserInputService.InputBegan and UserInputService.TouchTap events are correctly handling input.

Streamlined version

– Make sure HandleClick and Collect functions are correctly firing server events.

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()
		end
	end)
end

function AddOrRemoveSelectGui(WhatToDo, MODEL)
	if MODEL then
		if WhatToDo == "SELECT" then
			if script.Values.CurrentSelectedPart.Value then
				script.Values.CurrentSelectedPart.Value.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
		elseif WhatToDo == "CANCEL" then
			if script.Values.CurrentSelectedPart.Value then
				script.Values.CurrentSelectedPart.Value.ItemHoverGui:Destroy()
				script.Values.CurrentSelectedPart.Value = nil
				script.Values.LastSelectedPart.Value = nil
			end
		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)

okay i am doing this right now. please give me a second

1 Like

okay i am doing this right now. please give me a second

what is the gameprocessedevent hting doing? might that be the problem?


its detecting the mobile click but isnt passing it the same way as pc does, which results in the tree not being damaged

2 Likes

Okay cool, well, then can you also show the server script? What we need to do now is to find what the desktop version did where the mobile did not.

Also, can you try:

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

And compare the output for both desktop and mobile?

yes thats the issue. its changing the target based on mousemovement which doesnt work on mobile

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

which results in there being no mousetarget and the serverscript throwin error

image

Awesome!

Maybe you can use click or tap position on the screen → get the part that was clicked on → send to server → and on the server do chopping stuff