Invalid argument #1 (Vector2 expected, got Instance)

I try to detect when the player’s finger makes a move X on his phone, in order to perform an action depending on the movement made. I’m not good at arithmetic on Roblox, some help would be welcome

userInputService.TouchStarted:Connect(function(Position1, gameProcessed)
	print(tostring(Position1.Position))

	userInputService.TouchMoved:Connect(function(Position2, gameProcessed)		
		print("Moveok")		
		if Position2 == Position1 + Vector2.new(2,0) then
			print("hi")				
		end
	end)
end)

plpl

Try this:

local Connections = {}

userInputService.TouchStarted:Connect(function(touchObject1, gameProcessed)
	print(tostring(touchObject1.Position))

	Connections.TouchMoved = userInputService.TouchMoved:Connect(function(touchObject2, gameProcessed)		
		print("Moveok")
		
		if touchObject1.Position + Vector2.new(2, 0) == touchObject2.Position then
			print("hi")
		end
	end)
end)

userInputService.TouchEnded:Connect(function()
	local connection = Connections.TouchMoved
	
	if connection then
		connection:Disconnect()
		Connections.TouchMoved = nil
	end
end)

Thanks for your help, now the following error message is displayed

With this script I don’t get any error messages but as soon as I move my finger the “hi” is printed

userInputService.TouchStarted:Connect(function(touchObject1, gameProcessed)
	print(tostring(touchObject1.Position))

	userInputService.TouchMoved:Connect(function(touchObject2, gameProcessed)		
		print("Moveok")

		if touchObject1.Position + Vector3.new(0, 1000, 0) then
			print("hi")
		end
	end)
end)

Because you’re evaluating a number which will always return true. You need to compare it to something else.

Do you have any idea what I should compare it with so I can research it? Because right now I feel like I’m stuck. Thank you

Change your script to this:

userInputService.TouchStarted:Connect(function(touchObject1, gameProcessed)
	print(tostring(touchObject1.Position))

	userInputService.TouchMoved:Connect(function(touchObject2, gameProcessed)		
		print("Moveok")

		if touchObject1.Position + Vector3.new(0, 1000, 0) == touchObject2.Position then
			print("hi")
		end
	end)
end)

Yes I already tried that, no errors but it never finds the TouchObject2 (I also tried to put low values to Vector3.new) but nothing worked. I’ll keep on trying lots of things and then I’ll try to ask for help later if I haven’t found it, thanks

It’s because those values are never going to match unless it’s perfectly positioned.

Oh okay. I should put approximate position in there instead of looking for a specific position

I’m trying to get this post up. There are no errors in the script, but the script doesn’t work because the position2 never finds the position1 + Vector3.new because there are too many decimal and so the probabilities that the two positions coincide are extremely low. I don’t know how I could get around this problem

userInputService.TouchStarted:Connect(function(Position1, gameProcessed)
	print(tostring(Position1.Position))

	userInputService.TouchMoved:Connect(function(Position2, gameProcessed)		
		print("Moveok")
		if Position2.Position == Position1.Position + Vector3.new(0,10,0) then
			print("hi")
		end
	end)
end)```
userInputService.TouchMoved:Connect(function(Position2

Position2 is an input object, not a ‘Vector3’ value, you need to index its ‘Position’ property.

userInputService.TouchMoved:Connect(function(InputObject2
local Position2 = InputObject2.Position --'Vector3' value.

The same applies to ‘Position1’ in userInputService.TouchStarted. You’ve also got a memory leak due to the nested event connections. You should also use a rounding function to allow some leeway as the likelihood of the input object’s ‘X’ axis position remaining as ‘0’ is small.

1 Like

Indeed it is very better even if I still have some problems with my script, thanks for your solution