To start off, get the difference between current position and target position.
Then split that up into the length and direction, like this:
1 | local dif = targetPosition - currentPosition |
2 | local length = dif.Magnitude |
3 | local direction = dif.Unit |
Then we'll create the dif vector again, but now with a length of 25:
1 | local dif = targetPosition - currentPosition |
2 | local length = dif.Magnitude |
3 | local direction = dif.Unit |
5 | local newDif = direction * 25 |
Add this to the current position, and you'll teleport 25 studs closer each time this piece of code gets executed
1 | local dif = targetPosition - currentPosition |
2 | local length = dif.Magnitude |
3 | local direction = dif.Unit |
5 | local newDif = direction * 25 |
6 | currentPosition + = newDif |
You don't want to overshoot with teleporting, so you need to make sure that the distance that you travel is not more than the distance between you and the target position.
You can achieve this by replacing direction * 25 with direction * math.min(25, length)
1 | local dif = targetPosition - currentPosition |
2 | local length = dif.Magnitude |
3 | local direction = dif.Unit |
5 | local newDif = direction * math.min( 25 , length) |
6 | currentPosition + = newDif |
Now you want to teleport until the player is inside the block, so create a variable that keeps track when the player gets inside the part.
Then, you'll want to repeat the code until that variable is true.
1 | local targetReached = false |
3 | local dif = targetPosition - currentPosition |
4 | local length = dif.Magnitude |
5 | local direction = dif.Unit |
7 | local newDif = direction * math.min( 25 , length) |
8 | currentPosition + = newDif |
With this done, you now want to check when the player enters the brick. You could possibly use the physics engine for this, but I'll keep it a bit simpler.
If the current distance between the player and the brick is less than or equal to 25 studs, the teleport is guaranteed to teleport the player inside the brick. Checking for this, you can set targetReached to true, and exit the loop once that happens.
01 | local targetReached = false |
03 | local dif = targetPosition - currentPosition |
04 | local length = dif.Magnitude |
05 | local direction = dif.Unit |
07 | local newDif = direction * math.min( 25 , length) |
08 | currentPosition + = newDif |
With this all done, you only need to add a wait(3) inside the loop so you don't get teleported instantly.
01 | local targetReached = false |
02 | local targetPosition = ... |
05 | local dif = targetPosition - currentPosition |
06 | local length = dif.Magnitude |
07 | local direction = dif.Unit |
09 | local newDif = direction * math.min( 25 , length) |
10 | currentPosition + = newDif |
You'll need to add a bit of code yourself, which I have added in comments,
Additionally, you might need to correct syntax errors, because I have not tested this myself.