Skip to content

Instantly share code, notes, and snippets.

@RealEthanPlayzDev
Last active February 16, 2022 12:38
Show Gist options
  • Save RealEthanPlayzDev/60258fb3a9282089aab838952a64895c to your computer and use it in GitHub Desktop.
Save RealEthanPlayzDev/60258fb3a9282089aab838952a64895c to your computer and use it in GitHub Desktop.
DeviceInfo
--!strict
--[[
File name: DeviceInfo.luau
Author: RadiatedExodus (ItzEthanPlayz_YT/RealEthanPlayzDev)
Version: 2
IMPORTANT NOTE: As of October 4 2021, I have noticed many ways that device detection or platform detection can be false, I no longer gurantee they 100% work and detect as expected.
Reference links:
- https://realethanplayzdev.github.io/DeviceInfo/About/
- https://www.roblox.com/library/5343169924
- https://devforum.roblox.com/t/716491
- https://gist.github.com/RealEthanPlayzDev/60258fb3a9282089aab838952a64895c
--]]
local serv = {
StarterGui = game:GetService("StarterGui");
UserInputService = game:GetService("UserInputService");
GuiService = game:GetService("GuiService");
RunService = game:GetService("RunService");
}
assert(serv.RunService:IsClient(), "DeviceInfo can only run in the client")
--// CLASS DeviceInfo
local DeviceInfo = {}
--// DeviceInfoEnum.PlatformType DeviceInfo.PlatformType
DeviceInfo.PlatformType = {
Computer = "DeviceInfo_PlatformType_Computer";
Console = "DeviceInfo_PlatformType_Console";
Mobile = "DeviceInfo_PlatformType_Mobile";
}
--// DeviceInfoEnum.InputType DeviceInfo.InputType
DeviceInfo.InputType = {
Touchscreen = "DeviceInfo_InputType_Touchscreen";
KeyboardMouse = "DeviceInfo_InputType_KeyboardMouse";
Gamepad = "DeviceInfo_InputType_Gamepad";
VR = "DeviceInfo_InputType_VR";
Keyboard = "DeviceInfo_InputType_Keyboard";
Mouse = "DeviceInfo_InputType_Mouse";
}
--// DeviceInfoEnum.DeviceType DeviceInfo.DeviceType
DeviceInfo.DeviceType = {
Computer = "DeviceInfo_DeviceType_Computer";
Phone = "DeviceInfo_DeviceType_Phone";
Tablet = "DeviceInfo_DeviceType_Tablet";
Console = "DeviceInfo_DeviceType_Console";
}
--// DeviceInfoEnum.DeviceOrientation DeviceInfo.DeviceOrientation
DeviceInfo.DeviceOrientation = {
Landscape = "DeviceInfo_DeviceOrientation_Landscape";
Portrait = "DeviceInfo_DeviceOrientation_Portrait";
}
local previousRes: Vector2, previousInput: string, previousOrientation: string, previousGraphicsQuality: Enum.SavedQualitySetting, oldCamConnection: RBXScriptConnection = nil, nil, nil, nil, nil
local userGameSettings = UserSettings():GetService("UserGameSettings")
--// Bindables to be exposed to scripts
local inputChangeBind = Instance.new("BindableEvent")
local resChangeBind = Instance.new("BindableEvent")
local orientationChangeBind = Instance.new("BindableEvent")
local graphicsQualityChangeBind = Instance.new("BindableEvent")
--// function DeviceInfo.GetDevicePlatform(): DeviceInfoEnum.PlatformType
function DeviceInfo.GetDevicePlatform(): string
if serv.GuiService:IsTenFootInterface() then --// According to Roblox, GuiService:IsTenFootInterface() is the only guranteed way to check if a player is on console or not
return DeviceInfo.PlatformType.Console
elseif serv.UserInputService.TouchEnabled and not serv.UserInputService.KeyboardEnabled and not serv.UserInputService.MouseEnabled and not serv.GuiService:IsTenFootInterface() then
return DeviceInfo.PlatformType.Mobile
else
return DeviceInfo.PlatformType.Computer
end
end
--// function DeviceInfo.GetDeviceOrientation(): DeviceInfoEnum.DeviceOrientation
function DeviceInfo.GetDeviceOrientation(): string
if (workspace.CurrentCamera.ViewportSize.X < workspace.CurrentCamera.ViewportSize.Y) then
previousOrientation = DeviceInfo.DeviceOrientation.Portrait
return DeviceInfo.DeviceOrientation.Portrait
else
previousOrientation = DeviceInfo.DeviceOrientation.Landscape
return DeviceInfo.DeviceOrientation.Landscape
end
end
--// function DeviceInfo.GetWindowSize(): Vector2
function DeviceInfo.GetWindowSize(): Vector2
previousRes = workspace.CurrentCamera.ViewportSize
return previousRes
end
--// function DeviceInfo.GetDeviceInput(): DeviceInfoEnum.InputType
function DeviceInfo.GetDeviceInput(): string
if serv.UserInputService.MouseEnabled and serv.UserInputService.KeyboardEnabled then
previousInput = DeviceInfo.InputType.KeyboardMouse
return DeviceInfo.InputType.KeyboardMouse
elseif serv.UserInputService.MouseEnabled and not serv.UserInputService.KeyboardEnabled then
previousInput = DeviceInfo.InputType.Mouse
return DeviceInfo.InputType.Mouse
elseif serv.UserInputService.KeyboardEnabled and not serv.UserInputService.MouseEnabled then
previousInput = DeviceInfo.InputType.Keyboard
return DeviceInfo.InputType.Keyboard
elseif serv.UserInputService.GamepadEnabled then
previousInput = DeviceInfo.InputType.Gamepad
return DeviceInfo.InputType.Gamepad
elseif serv.UserInputService.VREnabled then
previousInput = DeviceInfo.InputType.VR
return DeviceInfo.InputType.VR
else
previousInput = DeviceInfo.InputType.Touchscreen
return DeviceInfo.InputType.Touchscreen
end
end
--// function DeviceInfo.GetDeviceType(): DeviceInfoEnum.DeviceType
function DeviceInfo.GetDeviceType(): string
if serv.GuiService:IsTenFootInterface() then
return DeviceInfo.DeviceType.Console
elseif DeviceInfo.GetDevicePlatform() == DeviceInfo.PlatformType.Mobile and serv.UserInputService.TouchEnabled then
local orientation = DeviceInfo.GetDeviceOrientation()
if orientation == DeviceInfo.DeviceOrientation.Landscape then
if workspace.CurrentCamera.ViewportSize.Y < 600 then
return DeviceInfo.DeviceType.Phone
end
return DeviceInfo.DeviceType.Tablet
else
if workspace.CurrentCamera.ViewportSize.X < 600 then
return DeviceInfo.DeviceType.Phone
end
return DeviceInfo.DeviceType.Tablet
end
else
return DeviceInfo.DeviceType.Computer
end
end
--// function DeviceInfo.GetGraphicsQuality(): Enum.SavedQualitySetting
function DeviceInfo.GetGraphicsQuality(): Enum.SavedQualitySetting
return userGameSettings.SavedQualityLevel
end
--// Input change detection
serv.UserInputService.LastInputTypeChanged:Connect(function()
local oldPrevInput = previousInput
DeviceInfo.GetDeviceInput()
if previousInput == oldPrevInput then return end
inputChangeBind:Fire(previousInput)
return
end)
--// Orienation and window size change detection
local function CameraHook()
if oldCamConnection then
oldCamConnection:Disconnect()
end
oldCamConnection = workspace.CurrentCamera:GetPropertyChangedSignal("ViewportSize"):Connect(function()
--// Window size
local oldPrevRes = previousRes
DeviceInfo.GetWindowSize()
if previousRes ~= oldPrevRes then
resChangeBind:Fire(previousRes)
end
--// Orientation
local oldPrevOrientation = previousOrientation
DeviceInfo.GetDeviceOrientation()
if previousOrientation ~= oldPrevOrientation then
orientationChangeBind:Fire(previousOrientation)
end
return
end)
end
CameraHook()
workspace:GetPropertyChangedSignal("CurrentCamera"):Connect(CameraHook)
--// Graphics quality level change
userGameSettings:GetPropertyChangedSignal("SavedQualityLevel"):Connect(function()
local oldPrevGraphicsQuality = previousOrientation
DeviceInfo.GetGraphicsQuality()
if previousGraphicsQuality ~= oldPrevGraphicsQuality then
graphicsQualityChangeBind:Fire(previousGraphicsQuality)
end
return
end)
--// Expose bindables to script
--// RBXScriptSignal DeviceInfo.InputChanged: DeviceInfoEnum.InputType
DeviceInfo.InputChanged = inputChangeBind.Event
--// RBXScriptSignal DeviceInfo.WindowResolutionChanged: Vector2
DeviceInfo.WindowSizeChanged = resChangeBind.Event
--// RBXScriptSignal DeviceInfo.OrientationChanged: DeviceInfoEnum.DeviceOrientation
DeviceInfo.OrientationChanged = orientationChangeBind.Event
--// RBXScriptSignal DeviceInfo.GraphicsQualityChanged: Enum.SavedQualitySetting
DeviceInfo.GraphicsQualityChanged = graphicsQualityChangeBind.Event
return DeviceInfo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment