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)
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)
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
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)```
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.