Trouble with UserInputService

I’m currently working on a tool for a new game. It’s a bow, and you can hold down to charge it, making it stronger. It works fine on PC, but when I test it on mobile, it also tracks the thumbstick as holding down, which I do not want.

Anyone know how to solve this?
Code:

local UserInputService = game:GetService("UserInputService")
local buttonDown = false
local cam = workspace.CurrentCamera

local function setZoom(charge, out) -- Changes FOV and walkspeed
	local hum = workspace:WaitForChild(game.Players.LocalPlayer.Name,30).Humanoid
	if not out then
		local fov = (charge - 1) * 20
		cam.FieldOfView = 70 - fov
		hum.WalkSpeed = 16 - ((charge - 1) * 8)
	elseif out then
		cam.FieldOfView = 70
		hum.WalkSpeed = 16
	end
end

UserInputService.InputBegan:Connect(function(input) -- For Pc, works fine
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		buttonDown = true
	end
end)

UserInputService.TouchStarted:Connect(function() -- Issue on Mobile
	buttonDown = true
end)

UserInputService.InputEnded:Connect(function(input) -- Works on PC
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		buttonDown = false
	end
end)

UserInputService.TouchEnded:Connect(function() -- Tracks when finger has been lifted
	buttonDown = false
end)

InputBegan and the related events also pass a bool, on the wiki they usually call it GameProcessedEvent. It indicates if the event has already been processed by some other part of the game, e.g. if the user types W in chat then they shouldn’t walk forward also, which can be prevented by returning early from the callback function if the bool is true.

So, what do I do? How would I use this in code?

Also, if the GameProcessedEvent returned false, what would that mean?

Try this

local UserInputService = game:GetService("UserInputService")
local buttonDown = false
local cam = workspace.CurrentCamera

local function setZoom(charge, out) -- Changes FOV and walkspeed
	local hum = workspace:WaitForChild(game.Players.LocalPlayer.Name,30).Humanoid
	if not out then
		local fov = (charge - 1) * 20
		cam.FieldOfView = 70 - fov
		hum.WalkSpeed = 16 - ((charge - 1) * 8)
	elseif out then
		cam.FieldOfView = 70
		hum.WalkSpeed = 16
	end
end

UserInputService.InputBegan:Connect(function(input) -- For Pc, works fine
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		buttonDown = true
	end
end)

UserInputService.TouchStarted:Connect(function(input, gameProcessed) -- Issue on Mobile
	if gameProcessed == false then
		buttonDown = true
	end
end)

UserInputService.InputEnded:Connect(function(input) -- Works on PC
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		buttonDown = false
	end
end)

UserInputService.TouchEnded:Connect(function(input, gameProcessed) -- Tracks when finger has been lifted
	if gameProcessed == false then
		buttonDown = false
	end
end)
1 Like

Thanks. This works with the Classic Thumbstick, but with the Dynamic Thumbstick, it still won’t work. For some reason, when I drag around the Dynamic Thumbstick, gameProcessed returns false. Any ideas?

I think you can’t change it, sometimes when you play simulators on mobile like “Eating simulators”, when you walk and stop walking, it counts you as clicked and you will eat once.

So, what do you recommend I do?

For now, I’m just using the classic thumbstick.

You can’t do anything sadly, thats a Roblox issue

UserInputService.TouchStarted:Connect(function(inputObject, gameProcessedEvent)
    if gameProcessedEvent then -- if it has been processed by the game then
        return -- "exit" the function
    end
	buttonDown = true
end)

Yes, this is what I currently have.

It works fine with the old thumbstick but won’t work with the new one.

As I said, you cannot fix it. It is a Roblox issue. You can see this problem in a lot of games if you play on mobile.

1 Like