Trouble Detecting last input type “Touch”

I’m trying to get my local script code to detect the last input used to switch a Toggle on or off depending on if the last input was touch screen or not

I don’t have my exact code but this is what I did mostly.

local UserInputService =  game:GetService("UserInputService")
local Touch = Enum.UserInputType.Touch
local Touchscreen = false
local function ToggleMouse(lastInputType)
if lastInputType == Touch then
Touchscreen = true
else
Touchscreen = false
return
end

Sorry I’m on mobile and I don’t have access to formatting that I can see if.

I used the developer ROBLOX website for most of the code using their last input type page and Input page.

Edit: From pc: So im trying to make my vehicle seat detect when touch is being used to change method of controlling vehicle and it works for detecting keyboard and mouse and gamepad1 but its not working touch devices wich im trying to figure out. reason im using last used is so that incase if a computer has a touchscreen and gyro and accelerometer. so im trying to make it so that it will use the method they are using for controls.

Hey,

to get last input type you have to use :GetLastInputType().

local UserInputService = game:GetService("UserInputService")
local Touch = Enum.UserInputType.Touch
local Touchscreen = false
local function ToggleMouse(lastInputType)
if lastInputType == Touch then
Touchscreen = true
else
Touchscreen = false
return
end

This is the formatted code so it is easier to read.

1 Like

Also, give us more information about what is the issue, because I don’t see any issue that you are asking to be helped with.

Actually I think what you said is correct actually I just don’t have access to my computer anymore and I’ll try it out but for now I’ll mark your post as solution.

I think I just forgot to use that command

ok so i got on my computer and this is the code and im unsure why this isnt working i tried what you said but its not activating. it dosnt print anything when using touchscreen but when i use pc it detects last input type

local UserInputService = game:GetService("UserInputService")

local Touch = Enum.UserInputType.Touch
local TouchMode = false
local inputing

local function ToggleMouse(lastInputType)

	if lastInputType == Touch then
		local TouchMode = true

		UserInputService.MouseIconEnabled = false
		inputing = "Touch"	
				print("Touch")

else
			local TouchMode = false
		print(" Not Touch")
		return
	end	
end

UserInputService.LastInputTypeChanged:Connect(ToggleMouse)

You’re not actually setting TouchMode, since inside the ToggleMouse function you’re doing

local TouchMode = ...

Remove the local so it actually sets the variable like this:

TouchMode = ...

i fixed that now (i know to do that i forgot to do that thanks) but its not activating any of the function. if so it would print either Not touch or touch but it prints none. its acting like that touchscreen isnt acting as a input for it to activate as lastInput

Can you send the current code then?

local UserInputService = game:GetService("UserInputService")

local Touch = Enum.UserInputType.Touch
local TouchMode = false
local inputing

local function ToggleMouse(lastInputType)

	if lastInputType == Touch then
		TouchMode = true

		UserInputService.MouseIconEnabled = false
		inputing = "Touch"	
				print("Touch")

else
			TouchMode = false
		print("Not Touch")
		return
	end	
	print("Activated")
end

UserInputService.LastInputTypeChanged:Connect(ToggleMouse)

is this a bug with lua?

Are you on a mobile device/mobile emulator?
Enum.UserInputType.Touch is for when a mobile device touches their screen.

Edit:
I highly doubt it’s a Lua bug.

yes i was using the emulator to test and the exact one i used was iphone 7 plus 736x414 resolution.

with the print i have for activated isnt printing when i do touch related stuff

I don’t think the exact emulator device is relevant.
Could you put a print before the if statements to see if the Event is firing, and also what the lastInputType is?

nothing outputs at the begining of the script at all it only activates when i use anything but touch

local UserInputService = game:GetService("UserInputService")

local Touch = Enum.UserInputType.Touch
local TouchMode = false
local inputing

local function ToggleMouse(lastInputType)
	print("Activated")
	print(lastInputType)
	if lastInputType == Touch then
		TouchMode = true

		UserInputService.MouseIconEnabled = false
		inputing = "Touch"	
				print("Touch")

else
			TouchMode = false
		print("Not Touch")
		return
	end	
end

UserInputService.LastInputTypeChanged:Connect(ToggleMouse)

im unsure what LastInputType dose inside the function its somthing from UserInputService | Documentation - Roblox Creator Hub

That’s rather odd.
I’ll look into it myself tomorrow, and try to come up with a solution for you.

could it possibly just not detecting with emulator should i try on my iphone 8+ rn?

I tryed it and it dosnt work nothing prints from the script.

It’s up to you. In my opinion you should try it.

Edit:
I’ll look into it tomorrow. It’s pretty late for me.

is there a method to detect from user input began? because i could use that i just dont know how i would detect it.

i see its 4:31pm here

After further testing, I’ve come to the conclusion that your definition of LastInputType is a bit wrong.
UserInputService.LastInputTypeChanged is used to detect when the LastInputType has changed.
So say on PC, there’s a lot of buttons, meaning there’s a lot of different InputType’s.
This means unless your user has a touchscreen laptop, the only InputType on a mobile device is Enum.UserInputType.Touch.
If you could give us the use case of why are you are doing LastInputTypeChanged, maybe we can come up with an alternate solution.

I’m doing it so I make mobile support for my car chassis and for reasons I need to use a if statement in a renderstep wich dose have its problems but I need to use it for performance. So later on I can also use the touch input detection to add their buttons on their screens. They don’t really say that about touched inside last input type. I would think it would be a universal parameter for all devices for compatibility and lessen code.

Would this mean that I need a alternative method for doing mobile support on my vehicle.

I know methods like touch enabled isn’t a good way of detecting if touchscreen is being used for example on pc because someone could have a mouse and keyboard. I’ve seen people use mouse and keyboard on a android device also so I’m trying to do a universal solution to solve my problem.

I did look up last input type and it should detect it is it just new because in this one post had suggestion for using what I did also so I’m a little confused. I dont know why but when i place down the mobile button code it stops running my script for pc - #3 by colbert2677

Would that method work in this context?

If there’s anything else you need to know let me know because I have been busy lately and haven’t been able to get on the forums.