local j=nil LocalPlayer.CharacterAdded:connect(function(f)if UserInputService.TouchEnabled then createTouchGuiContainer()end if ControlState.Current==nil then ControlState:SwitchTo(j)end end)LocalPlayer.CharacterRemoving:connect(function(f)j=ControlState.Current ControlState:SwitchTo(nil)end)UserInputService.Changed:connect(function(j)if j=="\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(j)if lastInputType==Enum.UserInputType.Touch and ClickToMoveTouchControls then if j then ControlState:SwitchTo(ClickToMoveTouchControls)else ControlState:SwitchTo(nil)end end end)end local f=function(j)lastInputType=j 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(j)local f=UserInputService:GetConnectedGamepads()if#f>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(j)ControlState:SwitchTo(ControlModules.Gamepad)end)f(UserInputService:GetLastInputType())UserInputService.LastInputTypeChanged:connect(f)
--[[ Touch Events ]]
--
-- On touch devices we need to recreate the guis on character load.
local lastControlState = nil
LocalPlayer.CharacterAdded:connect(function(character)
	if UserInputService.TouchEnabled then
		createTouchGuiContainer()
	end

	if ControlState.Current == nil then
		ControlState:SwitchTo(lastControlState)
	end
end)

LocalPlayer.CharacterRemoving:connect(function(character)
	lastControlState = ControlState.Current
	ControlState:SwitchTo(nil)
end)

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)
