UserInputService.Changed:connect(function(t)if t=="\077\111\100\097\108\069\110\097\098\108\101\100"then IsModalEnabled=UserInputService.ModalEnabled if lastInputType==Enum.UserInputType.Touch then if ControlState.Current==ControlModules.Touch and IsModalEnabled then ControlState:SwitchTo(nil)elseif ControlState.Current==nil and not IsModalEnabled then ControlState:SwitchTo(ControlModules.Touch)end end end end)if BindableEvent_OnFailStateChanged then BindableEvent_OnFailStateChanged.Event:connect(function(t)if lastInputType==Enum.UserInputType.Touch and ClickToMoveTouchControls then if t then ControlState:SwitchTo(ClickToMoveTouchControls)else ControlState:SwitchTo(nil)end end end)end local t=function(t)lastInputType=t if lastInputType==Enum.UserInputType.Touch then ControlState:SwitchTo(ControlModules.Touch)elseif lastInputType==Enum.UserInputType.Keyboard or lastInputType==Enum.UserInputType.MouseButton1 or lastInputType==Enum.UserInputType.MouseButton2 or lastInputType==Enum.UserInputType.MouseButton3 or lastInputType==Enum.UserInputType.MouseWheel or lastInputType==Enum.UserInputType.MouseMovement then ControlState:SwitchTo(ControlModules.Keyboard)elseif lastInputType==Enum.UserInputType.Gamepad1 or lastInputType==Enum.UserInputType.Gamepad2 or lastInputType==Enum.UserInputType.Gamepad3 or lastInputType==Enum.UserInputType.Gamepad4 then ControlState:SwitchTo(ControlModules.Gamepad)end end if IsTouchDevice then createTouchGuiContainer()end MasterControl:Init()UserInputService.GamepadDisconnected:connect(function(t)local o=UserInputService:GetConnectedGamepads()if#o>0 then return end if UserInputService.KeyboardEnabled then ControlState:SwitchTo(ControlModules.Keyboard)elseif IsTouchDevice then ControlState:SwitchTo(ControlModules.Touch)end end)UserInputService.GamepadConnected:connect(function(t)ControlState:SwitchTo(ControlModules.Gamepad)end)t(UserInputService:GetLastInputType())UserInputService.LastInputTypeChanged:connect(t)
--[[ Touch Events ]]
--
UserInputService.Changed:connect(function(property)
	if property == "ModalEnabled" then
		IsModalEnabled = UserInputService.ModalEnabled

		if lastInputType == Enum.UserInputType.Touch then
			if ControlState.Current == ControlModules.Touch and IsModalEnabled then
				ControlState:SwitchTo(nil)
			elseif ControlState.Current == nil and not IsModalEnabled then
				ControlState:SwitchTo(ControlModules.Touch)
			end
		end
	end
end)

if BindableEvent_OnFailStateChanged then
	BindableEvent_OnFailStateChanged.Event:connect(function(isOn)
		if lastInputType == Enum.UserInputType.Touch and ClickToMoveTouchControls then
			if isOn then
				ControlState:SwitchTo(ClickToMoveTouchControls)
			else
				ControlState:SwitchTo(nil)
			end
		end
	end)
end

local switchToInputType = function(newLastInputType)
	lastInputType = newLastInputType

	if lastInputType == Enum.UserInputType.Touch then
		ControlState:SwitchTo(ControlModules.Touch)
	elseif
		lastInputType == Enum.UserInputType.Keyboard
		or lastInputType == Enum.UserInputType.MouseButton1
		or lastInputType == Enum.UserInputType.MouseButton2
		or lastInputType == Enum.UserInputType.MouseButton3
		or lastInputType == Enum.UserInputType.MouseWheel
		or lastInputType == Enum.UserInputType.MouseMovement
	then
		ControlState:SwitchTo(ControlModules.Keyboard)
	elseif
		lastInputType == Enum.UserInputType.Gamepad1
		or lastInputType == Enum.UserInputType.Gamepad2
		or lastInputType == Enum.UserInputType.Gamepad3
		or lastInputType == Enum.UserInputType.Gamepad4
	then
		ControlState:SwitchTo(ControlModules.Gamepad)
	end
end

if IsTouchDevice then
	createTouchGuiContainer()
end

MasterControl:Init()

UserInputService.GamepadDisconnected:connect(function(gamepadEnum)
	local connectedGamepads = UserInputService:GetConnectedGamepads()
	if #connectedGamepads > 0 then
		return
	end

	if UserInputService.KeyboardEnabled then
		ControlState:SwitchTo(ControlModules.Keyboard)
	elseif IsTouchDevice then
		ControlState:SwitchTo(ControlModules.Touch)
	end
end)

UserInputService.GamepadConnected:connect(function(gamepadEnum)
	ControlState:SwitchTo(ControlModules.Gamepad)
end)

switchToInputType(UserInputService:GetLastInputType())
UserInputService.LastInputTypeChanged:connect(switchToInputType)
