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
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)
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
– 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)
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?