I was bored so I wrote a cool little function that can get the user screen/window size using the AbsoluteSize
property of UIs which takes advantage of the new ScreenInsets
property of the ScreenGui
class. This function has a single parameter called fullscreen
that when set to true yields the script and waits for the user to enable fullscreen so it can fetch the real device dimensions, anyways here it is:
function GetScreenSize(fullscreen: boolean?): Vector2
if game:GetService("RunService"):IsStudio() then
warn("Call of GetScreenSize() inside studio returns the viewport dimensions, not the actual screen dimensions!")
end
if fullscreen then
local GameSettings = UserSettings().GameSettings
if not GameSettings:InFullScreen() then
GameSettings.FullscreenChanged:Wait()
end
end
local UI = Instance.new("ScreenGui")
UI.ScreenInsets = Enum.ScreenInsets.None
UI.Parent = game.Players.LocalPlayer.PlayerGui
local size = UI.AbsoluteSize
UI:Destroy()
return size*3
end
--This runs instantly, however, it will get the user's current window size not the actual dimensions of the device
print(GetScreenSize())
--This waits for the user to enable fullscreen, so it can fetch the actual device dimensions
--It will yield the local script until the user has enabled fullscreen
print(GetScreenSize(true))
Is this against the TOS? I don’t think so, the function just takes advantage of features/properties that Roblox allows us to access freely and isn’t meant to be used maliciously. Also, there is currently no way to force a client to enable fullscreen(if I’m not mistaken) so technically if a player joins a server without fullscreen on this function won’t be able to fetch their device dimensions(unless a malicious dev forces a user to enable fullscreen in order to play the game).
I also don’t consider this a good solution to protect a game against exploiters, since it runs on a LocalScript, and thus exploiters can just lie to the server about the result.
Current issues with the above function:
- This just approximates the screen size, it returns a fixed but not true result. This function can have an error of 3 to 9 pixels lower than the true X and Y values but it will remain fixed(so there’re 4 states for each coordinate: val, val-3, val-6 and val-9).
- If a user changes their display resolution(for example a PC or an Xbox One user) it will also change the output.