Touching screen in mobile

Hello, I have a click-on-screen script to get a currency but for mobile, whenever they try to move the screen(their intention being not clicking), it triggers anyways. Could you help me fixing that? Here’s the script :

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ContextActionService = game:GetService("ContextActionService")

local Event = ReplicatedStorage.Misc:WaitForChild("Clicked")

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

local function onClicked(action, inputState, inputObject)
	if action == "Clicked" then
		if inputState == Enum.UserInputState.Begin then
			Event:FireServer()
			return Enum.ContextActionResult.Pass
		end
	end
end

ContextActionService:BindAction("Clicked", onClicked, false, Enum.UserInputType.MouseButton1, Enum.KeyCode.ButtonR2, Enum.UserInputType.Touch)
1 Like

Create a button gui somewhere on the screen that’ll have the same intentions, change the visible value to false. Check if the player is on mobile, if they are, then change the visible value to true. For mobile players, instead of clicking on the screen, they’d have to click on the button gui.

Hmm, I could’ve done that but this is not really what I want. Fine, I’ll not just try many days to get something that could’ve been fixed in some seconds. Thank you anyways!

It only takes a couple minutes to do this lol

Also, I have a question regarding ContextActionService :
How would I use MouseButton1Down on it? Or ButtonR2Held? BGS has this system implemented.

Actually, I’ll use UserInputService. I’ll erase the contextactionservice one.

If you’re talking about the button, just use MouseButton1Click. It’ll still work the same as it does for PC players.

No. I want it to work for every second you’re holding the MouseButton1 or ButtonR2.

Oh okay, then use MouseButton1Down to replicate the button being held down, and use Button1Up to replicate the button being released.

What about ButtonR2? And for mobile?

EDIT : Mobile is fine, now ButtonR2.

I’m not sure about controller, I’ve never really experimented with controller yet. You can use UserInputType, and look up the keycodes here. https://developer.roblox.com/en-us/api-reference/enum/KeyCode

1 Like

Or you can use TouchTap UserInputService | Documentation - Roblox Creator Hub

TouchTap only works in buttons, not Enum.UserInputType.TouchTap. And my issue is only clicking on screen. Also, it’s fixed, thank you anyways!

local UserInputService = game:GetService('UserInputService')

UserInputService.TouchTap:Connect(function()
    onClicked(action, inputState, inputObject)
end)

“The TouchTap event fires when the user touches/taps their finger on the screen on a TouchEnabled device.”
Also if you want to avoid the event being fired when a player presses on a ui object you can use TouchTapInWorld: UserInputService | Documentation - Roblox Creator Hub

1 Like