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.
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.
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.
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)
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
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)
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)
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.