Help with using ScrW()

Hello, just to start off I am new to lua so this may be a “noobish” question.

I am creating some info text for a my server that displays on the top middle of the players screen. Now it looks fine on my screen (1280x720) but on anyone elses screen that is in a higher resolution than mine the text is a little to the right (as show in picture). Could anyone help me fix this? Code:



function SpawnShit()
			local LineOne = "Welcome "..LocalPlayer():Nick().." to NyeBlock"
			local LineTwo = "F1: Help Menu | F3: Shop | F4: Thirdperson"

			surface.SetFont("default") 
			local w, h = surface.GetTextSize(LineOne)
			local ww, hh = surface.GetTextSize(LineTwo) 

			local LongestLine = math.max(w, ww)

			local Padding = 8
			local Spacing = 2

			hook.Add("HUDPaint", "SpawnShit", function()
				local BoxWidth = LongestLine+(Padding*7)
				local BoxHeight = (h+hh)+(Padding*2)
				draw.RoundedBox(4, ScrW()*0.39, 0, BoxWidth, BoxHeight, Color( 0, 0, 0, 200 )) --My attempt to fix ( ScrW()*0.39 )
				draw.SimpleText(LineOne, "InfoFont", ScrW() - BoxWidth + -380, Padding+(h*0.2), color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER) --My attempt to fix ( ScrW() - BoxWidth + -380 )
				draw.SimpleText(LineTwo, "InfoFont", ScrW() - BoxWidth + -380,  Padding+h+Spacing+(hh*0.5), color_white, TEXT_ALIGN_CENTER,  TEXT_ALIGN_CENTER) --My attempt to fix ( ScrW() - BoxWidth + -380 )
			end)
		end
		SpawnShit()



Picture for reference (other players view):

Also is there an efficient way to test ScrW() and ScrH() on multiple screen sizes?

Your math is off, which is what’s offsetting your elements awkwardly. Your box is also out of place, not just the text. It’s placed at 39% of the screen’s width. ScrW() * 0.39 is 39% of the screen’s width.

To center it, you want to draw it at 50% of the screen minus half of the box’s width. I’d offer more help but I hate math and I hate derma.

If you do the math right, you won’t need to test it on multiple screens. 50% of the screen is going to be, relatively speaking, the exact same on all screens.

I haven’t test it, but it should work.

[lua]
local LineOne = “Welcome “…LocalPlayer():Nick()…” to NyeBlock”
local LineTwo = “F1: Help Menu | F3: Shop | F4: Thirdperson”

		surface.SetFont("DermaDefault") 
		local w, h = surface.GetTextSize(LineOne)
		local ww, hh = surface.GetTextSize(LineTwo) 

		local LongestLine = math.max(w, ww)

		local Padding = 8
		local Spacing = 2

		hook.Add("HUDPaint", "SpawnShit", function()
			local BoxWidth = LongestLine+(Padding*7)
			local BoxHeight = (h+hh)+(Padding*2)
			
			draw.RoundedBox(4, ScrW() * .5 - BoxWidth * .5 , 0, BoxWidth, BoxHeight, Color( 0, 0, 0, 200 )) --My attempt to fix ( ScrW()*0.39 )
			draw.SimpleText(LineOne, "DermaDefault", ScrW() * .5, Padding+(h*0.2), color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER) --My attempt to fix ( ScrW() - BoxWidth + -380 )
			draw.SimpleText(LineTwo, "DermaDefault", ScrW() * .5,  Padding+h+Spacing+(hh*0.5), color_white, TEXT_ALIGN_CENTER,  TEXT_ALIGN_CENTER) --My attempt to fix ( ScrW() - BoxWidth + -380 )
		end)

[/lua]

The position of your elements are in most cases originating from the top left of it, meaning that if you take ScrW() / 2, it will have its corner in the middle and then stretching out to the right, not the entire box in the middle. You’ve tried to offset it by multiplying ScrW() which will only work for certain box widths and heights matching with certain resolutions. What you’re after is this:
[lua](ScrW() / 2) - (BoxWidth / 2)[/lua]
For future reference, always remember your position originates in the top left of the element. :slight_smile:

Thanks for the help guys, solved.