Invalid argument #1 (UDim2 expected, got Vector3)

Hi!
So I’m trying to make a rangefinder which tell’s you the distance of what you are pointing at but while I was testing the code, It gave me this error which I am finding hard to fix and thought of asking smarter people on how to go about fixing it…

Here’s my code.

local character = player.Character

local textLabel = script.Parent

local function updateDistance()
	local distance = (character.HumanoidRootPart.Position - textLabel.Position).Magnitude
	textLabel.Text = "Distance: " .. tostring(distance)
end

local userInputService = game:GetService("UserInputService")
local function onKeyPressed(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.Z then
		updateDistance()
	end
end

userInputService.InputBegan:Connect(onKeyPressed)

--The above code is placed inside a localscript in a textlabel btw

GUI elements don’t have a world position, just a position on screen. You’ll probably want to look into raycasting, specifically with ScreenPointToRay. That will allow you to turn GUI coordinates on the screen into world coordinates so you can get distance.

3 Likes