Attempt to compare Vector2 < Vector2

I’m making a door script but when testing in tablet and phone mode it spits out attempt to compare Vector2 < Vector2 for no reason!

--Client-side version for the DoorModule

local GuiService = game:GetService("GuiService")
local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Camera = workspace.CurrentCamera

--Device checkers

local function CheckIfGamepad()
	--GuiService.SelectedObject is used for gamepads and xbox does so.
	if GuiService.SelectedObject ~= nil then
		return true
	end
	
	return false
end

local function CheckIfXbox()
	local IsGamepad = CheckIfGamepad()
	
	if IsGamepad then
		if Camera.ViewportSize == Vector2.new(1920, 1080) or Camera.ViewportSize < Vector2.new(1920, 1080) then
			return true
		end
	end
	
	return false
end

local function CheckIfPhone()
	local TouchEnabled = UserInputService.TouchEnabled
	
	if TouchEnabled then
		if Camera.ViewportSize == Vector2.new(852, 414) or Camera.ViewportSize < Vector2.new(852, 414) then
			return true
		end
	end
	
	return false
end

local function CheckIfTablet()
	local TouchEnabled = UserInputService.TouchEnabled

	if TouchEnabled then
		if Camera.ViewportSize == Vector2.new(1280, 800) or Camera.ViewportSize < Vector2.new(1280, 800) then
			return true
		end
	end

	return false
end

local t = {}

t.SetupDoor = function(Door, KeyBindFunc)
	--Sets up the door keybinding functions and UI.
	
	local IsXbox = CheckIfXbox()
	local IsPhone = CheckIfPhone()
	local IsTablet = CheckIfTablet()
	
	local UiKey = Door.UiKey
	local UiXboxKey = Door.UiXboxKey
	
	if IsXbox then
		--Xbox
		UiKey.Enabled = false
		UiXboxKey.Enabled = true
		
		ContextActionService:BindAction("DoorKeybind", KeyBindFunc, false, Enum.KeyCode.ButtonA)
	elseif IsPhone then
		--Phone
		UiKey.Enabled = true
		UiXboxKey.Enabled = false
		
		UiKey.Key.Visible = false

		ContextActionService:BindAction("DoorKeybind", KeyBindFunc, true)
	elseif IsTablet then
		--Tablet
		UiKey.Enabled = true
		UiXboxKey.Enabled = false

		UiKey.Key.Visible = false
		
		UiKey.Tap.Position = UDim2.new(0.05, 0, 0.05, 0)
		UiKey.Tap.Size = UDim2.new(0.9, 0, 0.9, 0)

		ContextActionService:BindAction("DoorKeybind", KeyBindFunc, true)
	else
		--PC
		UiKey.Enabled = true
		UiXboxKey.Enabled = false

		UiKey.Tap.Visible = false

		ContextActionService:BindAction("DoorKeybind", KeyBindFunc, false, Enum.KeyCode.E)
	end
end

t.CreateClientDoor = function(Door)
	--This is important for the door to work
	
	t.SetupDoor(Door, function()
		--Disables the door
		print(true)
		--ReplicatedStorage.OpenDoor:FireServer(Door.Name)
	end)
end

return t

The error is self-explantory, you cannot compare two Vector2s together. You want to do something like this:

local function IsLessThan(v1: Vector2, v2: Vector2) : boolean
  return (v1.X < v2.X) and (v1.Y < v2.Y)
end

(Note: This answer assumes the following block of code, inspired by @omega’s answer:

--- Compares two Vector2s. Returns true if a <= b.
local function lteV2(a: Vector2, b: Vector2)
     return a.X <= b.X and a.Y <= b.Y
end

)

You can combine Camera.ViewportSize == Vector2.new(1280, 800) or Camera.ViewportSize < Vector2.new(1280, 800) into lteV2(Camera.ViewportSize, Vector2.new(1280, 800)), which should make the code easier to use. Also, you don’t need to save TouchEnabled to a separate variable, and with my change listed earlier you can use and to combine the conditions.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.