Enum.UserInputType.Touch includes swipe gestures

Hello!

I’ve made a script to delete blocks when tapped in my building game, but it thinks swipe gestures, like to move the camera, are taps, which deletes the blocks.
The blocks are only deleted when the swipe gesture starts on the block.

Here’s a part of my script that I think is relevant:

ClickConnection = UserInputService.InputBegan:Connect(function(input)
	if deleteMode.Value == true then
		if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
			local mousePosition = UserInputService:GetMouseLocation()
			DestroyBlockRemote:FireServer(CurrentCamera:ViewportPointToRay(mousePosition.X, mousePosition.Y))
		end
	end
end)
Here's a video of this (using Studio's mobile emulator)

Here's the full script
-- References
local LocalPlayer = game:GetService("Players").LocalPlayer
local BuildGui = LocalPlayer:WaitForChild("PlayerGui", 60):WaitForChild("BuildGui", 60)
local Button = script.Parent
local UserInputService = game:GetService("UserInputService")
local CurrentCamera = game.Workspace.CurrentCamera
local TweenService = game:GetService("TweenService")

-- Remotes
local DestroyBlockRemote = game:GetService("ReplicatedStorage").Remotes.Building.DestroyBlock
local TimeTypeRemote = game.ReplicatedStorage.TimeType -- sends TimeType when match starts, 3 == end, 2 == start, 1 == intermission.

-- Variables
local ClickConnection

-- cool code :smiling_sunglasses:
local deleteMode = script.Parent.Parent.deleteMode

deleteModeConnection = Button.Activated:Connect(function()
	deleteMode.Value = not deleteMode.Value
	
	local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Exponential)

	if deleteMode.Value == false then
		local tween = TweenService:Create(Button, tweenInfo, {
			BackgroundColor3 = Color3.fromRGB(125, 19, 19),
			TextTransparency = 0.25,
			BackgroundTransparency = 0.4
		})
		tween:Play()
		Button.Text = "<i>delete mode</i>"
	else
		local tween = TweenService:Create(Button, tweenInfo, {
			BackgroundColor3 = Color3.fromRGB(197, 70, 70),
			TextTransparency = 0,
			BackgroundTransparency = 0
		})
		tween:Play()
		Button.Text = "delete mode"
	end
end)

TimeTypeRemote.OnClientEvent:Connect(function(timeType)
	if timeType[2] == 3 then

		if ClickConnection then
			ClickConnection:Disconnect()
		end

		ClickConnection = UserInputService.InputBegan:Connect(function(input)
			if deleteMode.Value == true then
				if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
					local mousePosition = UserInputService:GetMouseLocation()
					DestroyBlockRemote:FireServer(CurrentCamera:ViewportPointToRay(mousePosition.X, mousePosition.Y))
				end
			end
		end)
	else
		if ClickConnection then
			ClickConnection:Disconnect()
		end
	end

end)

I’m bumping this because I need help.

I am very bad for scripting cameras so i cannot help,but i guess the only way u can do is to aloow more user inputs to enter the if condition

hello!

your are currently using Enum.UserInputType.Touch to detect mobile input.
the problem with that is, as you mentioned, it also detects mobile input when the player moves their camera, which is unwanted.

UserInputService actually has their own event for handing this, which is called TouchTapInWorld. there is also a TouchTap event, but that also detects interacting with gui.

you can make another connection variable (TouchConnection?) and bind this event to it:

UserInputService.TouchTapInWorld:Connect(function()
		if deleteMode.Value == true then
			local mousePosition = UserInputService:GetMouseLocation()
			DestroyBlockRemote:FireServer(CurrentCamera:ViewportPointToRay(mousePosition.X, mousePosition.Y))
		end
	end
end)

(you also should remove the or input.UserInputType == Enum.UserInputType.Touch part of the ClickConnection aswell)

I’d also add that you can condition it further by checking whether the input’s UserInputState Enum is equal to End, to make sure the tap input is a release (similar to MouseButton1Up), unless the function above already does something similar.

I want to add that there’s a whole slew of events that UserInputService provides that deal with touch input such as swipe, pan, and tap.

The OP can read about them here:

https://create.roblox.com/docs/reference/engine/classes/UserInputService#events

1 Like

Hey, I know it’s been 4 days but I was busy irl, but is there an event like
MouseButton1DownInWorld?
I can’t find one, and without it my code will get really messy.

1 Like

unfortunately, i do not believe there is a MouseButton1DownInWorld event.
if you are worried about your code getting messy, something you could do is use a function for deleting blocks to avoid repetition.

local function DeleteBlock()
    if deleteMode.Value == true then
		local mousePosition = UserInputService:GetMouseLocation()
		DestroyBlockRemote:FireServer(CurrentCamera:ViewportPointToRay(mousePosition.X, mousePosition.Y))
	end
end

and then instead of repeating these 2 lines for both events:

local mousePosition = UserInputService:GetMouseLocation()	
DestroyBlockRemote:FireServer(CurrentCamera:ViewportPointToRay(mousePosition.X, mousePosition.Y))

you can just call the DeleteBlock() function.

i dont think there is a way of connecting 2 functions to a single event.

1 Like

I guess that’s too bad. Thanks for the answer, I won’t incorporate this workaround in my code though.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.