A way of getting the client device dimensions

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:

  1. 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).
  2. If a user changes their display resolution(for example a PC or an Xbox One user) it will also change the output.
2 Likes

Can you not just use Mouse.ViewSizeX and Mouse.ViewSizeY?

But not everybody has a Mouse (not sue how it works tho)

I don’t think that makes a difference since ViewSize just returns the X and Y size of the screen

(I’m not sure either)
I think this wouldn’t work because if I have Touchscreen the Mouse wouldn’t exist, and thus that may not work

Edit: oh okay, makes sense

I just checked and it seems that Mouse.ViewSizeX and Mouse.ViewSizeY return the values of absolute position but without the property ScreenInsets set to None. So the method I provided above works as a better approximation of the true screen size(I’m using the term approximation because both dimensions can have an error of 9 pixels and is always below the actual screen size).

But… what if they resize the window?

That’s what the optional parameter does, if you pass true to the function it will yield the script and wait until the client enables fullscreen, which returns the approximation of the client screen resolution.

Basically, there’s no way to force-get the client screen resolution(as far as I’m aware) but since we have access to if the user has fullscreen on and when it changes an option the script utilizes is patiently waiting for the client to enable fullscreen at some point during gameplay.

I see your point. But why aren’t you using Camera.ViewportSize?

1 Like

That’s actually another way of doing it, but I wasn’t aware of it, the result is exactly the same as my method(when multiplied by 3 obviously).

Camera.ViewportSize is also good :sob:

I am confused. Why are you doing screen size * 3? I think Roblox needs to provide a built-in way to get the ScreenSize, especially for gun games. It is currently too big of an issue for developers to have to write so much code based on each device whether or not to account for gui inset, and where to raycast from (ScreenPointToRay vs ViewportPointToRay).