Detecting the last input the player has made

  1. What do you want to achieve?
    I want to detect, what the last input was, the player has made.

  2. What is the issue?
    I tried the script below and the only thing, that happend, was that it printed, that I had all of the things, since I had a mouse, a controller and a keyboard connected. But I want, that it detects what the last input was, not what things I have connected. (The script is in StarterPlayerScripts)

local UserInputService = game:GetService("UserInputService")
local PlayerGUI = Player.PlayerGui

local lastInput = UserInputService:GetLastInputType()

local function onInputBegan(input)
	
	if UserInputService.GamepadEnabled == true then
		print("User has a controller")
	end
	
	if UserInputService.KeyboardEnabled == true then
		print("User has a keyboard")
	end
	
	if UserInputService.TouchEnabled == true then
		print("User has a touchscreen")
	end
	
	if UserInputService.MouseEnabled == true then
		print("User has a mouse")
	end
	
	if UserInputService.VREnabled == true then
		print("User has a vr headset")
	end
	
end

UserInputService.InputBegan:Connect(onInputBegan)

Output:
grafik

I hope you can help me, so I know how I can detect what the last input was.

Thanks in advance!

1 Like

variables

local UserInputService = game:GetService("UserInputService")

local lastInput = Enum.KeyCode.Unknown

local function onInputBegan(Input)
	print("Last input:", lastInput)
	print("New input:", Input.KeyCode)

	lastInput = Input.KeyCode
end

UserInputService.InputBegan:Connect(onInputBegan)

You have it print if its enabled, doesn’t matter in the code if its the last input or not. You would have to check in the if loop if it is the last input type. You also only update the last input type at first, so it would probably only print the first ever input, but I’m not sure on how GetLastInputType() works.

Thank you, that worked, but when I moved the cursour it printed “unknow” not mouse!
grafik

And another problem is, that it detects the key pressed, but I want to know if it is mobile, gamepad or computer/mouse/keyboard.

Do user input type instead:

local UserInputService = game:GetService("UserInputService")

local lastInput = Enum.UserInputType.MouseButton1

local function onInputBegan(Input)
	print("Last input:", lastInput)
	print("New input:", Input.UserInputType)

	lastInput = Input.UserInputType
end

UserInputService.InputBegan:Connect(onInputBegan)
2 Likes

That’s because mouse doesn’t have a keycode/

There’s an event variant of GetLastInputType that you could use alongside said function called UserInputService.LastInputTypeChanged.