How to detect device orientation changes in landscape mode and upside-down landscape mode?

I am trying to figure out how to change the ScreenInsets property of a screen GUI based on device screen rotation.

I am trying to make my GUI use “DeviceSafeInsets” as the ScreenInsets type only when the player flips their phone that has a notched display to where the notch is on the right side of the screen and have it use “None” ScreenInsets when the device is flipped to where the notch is on the left, to get the most space used for my sidebar GUI, but cannot figure out anything that works well.

I tried looking on the roblox documentation and tried something like this:

local UserInputService = game:GetService("UserInputService")
local screenGui = script.Parent

UserInputService.DeviceRotationChanged:Connect(function(rotation, cframe)

	if cframe.RightVector.X < 0 then
		screenGui.ScreenInsets = Enum.ScreenInsets.DeviceSafeInsets
	else
		screenGui.ScreenInsets = Enum.ScreenInsets.None
	end
end)

This code has a lot of issues. When you join the game when your phone is rotated with notch to the right, the code works the wrong way, and when you join with notch to the left, it kinda works. The code isn’t very good because the slightest movement of the phone kinda makes the property flip back and forth a bit, and it gets annoying.

Never mind, I found a solution!

local screenGui = script.Parent
local Players = game:GetService(“Players”)
local RunService = game:GetService(“RunService”)

local player = Players.LocalPlayer
local playerGui = player:WaitForChild(“PlayerGui”)

local currentOrientation = playerGui.CurrentScreenOrientation

RunService.RenderStepped:Connect(function()
if playerGui.CurrentScreenOrientation ~= currentOrientation then
currentOrientation = playerGui.CurrentScreenOrientation
if currentOrientation == Enum.ScreenOrientation.LandscapeLeft then
screenGui.ScreenInsets = Enum.ScreenInsets.DeviceSafeInsets
elseif currentOrientation == Enum.ScreenOrientation.LandscapeRight then
screenGui.ScreenInsets = Enum.ScreenInsets.None
end
end
end)

1 Like

bro

Sorry I forgot to click it i got distracted :sob:

1 Like

binding this to the render step is a very bad idea, consider using GetPropertyChangedSignal as it only calls the function whenever it’s changed instead of every frame

That’s a good idea actually! I’ll definitely do that instead