Sizing UIStroke on all platforms help

I made a uistroke sizer for all platforms, it works, but only in studio and if i play test. But when I play in game in roblox it does not size?

for i, v in pairs(script.Parent.Parent:GetDescendants()) do

	if v:IsA("UIStroke") then

		local BASE_SIZE = 1700
		local uiStroke = v
		local initialStrokeThickness = uiStroke.Thickness
		local camera = game:GetService("Workspace").CurrentCamera

		local function updateStrokeThickness()
			uiStroke.Thickness = initialStrokeThickness * camera.ViewportSize.X / BASE_SIZE
		end

		camera:GetPropertyChangedSignal("ViewportSize"):Connect(updateStrokeThickness)
		updateStrokeThickness()

	end

end
1 Like

Here for you is a simplified version of a module that I did a few years ago. Put the code in a LocalScript and your problems should be solved.

--< Services
local PlayerService = game:GetService("Players")

--< Variables
local BASE_SIZE = 1920 -- 1700 for you

local Player = PlayerService.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")

local CurrentCamera = workspace.CurrentCamera

local UIStrokeStore = {}

--< Functions
local function GetResolution() -- Get current screen resolution
	local viewportSize = CurrentCamera.ViewportSize
	return viewportSize.X, viewportSize.Y
end

local function StrokeResolution(uiStroke) -- Update one UIStroke
	--// Variables
	local x, y = GetResolution()
	local thickness = uiStroke:GetAttribute("SavedThickness")
	local newThickness = ((thickness / BASE_SIZE) * x)

	--// Update Thickness
	uiStroke.Thickness = newThickness
end

local function ScanForAutoStroke(inst) -- Scan an Instance to find some UIStroke
	for _, uiObject in next, inst:GetDescendants() do
		local ignore = uiObject:GetAttribute("Ignore") -- You can put an boolean attribute set to true on a UIStroke and it will be ignored by the script

		if uiObject:IsA("UIStroke") and (not table.find(UIStrokeStore, uiObject)) and (not ignore) then
			uiObject:SetAttribute("SavedThickness", uiObject.Thickness)
			StrokeResolution(uiObject)

			table.insert(UIStrokeStore, uiObject)
		end
	end
end

local function UpdateStrokeResolution() -- Update all UIStroke registered
	for i = 1, #UIStrokeStore do
		StrokeResolution(UIStrokeStore[i])
	end
end

--< Initialize
ScanForAutoStroke(PlayerGui) -- Scan complete PlayerGui

--< Connections
CurrentCamera:GetPropertyChangedSignal("ViewportSize"):Connect(UpdateStrokeResolution) -- Resize if window size update

PlayerGui.DescendantAdded:Connect(function() -- Scan again to auto stroke new potential UIStroke
	ScanForAutoStroke(PlayerGui)
end)

I advise you to have a ScreenGui which will always be visible with the “ResetOnSpawn” set to false and use it instead of CurrentCamera to be updated from changing screen resolution and use ScreenGui.AbsoluteSize to obtain the screen resolution. I had resizing issues from using CurrentCamera.ViewportSize and using a ScreenGui instead of ViewportSize have solve my issues.

Let me know if this helped you. You can also ask me your questions if you have any.