Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Teleport Player1 25 studs closer to a part every 3 seconds?

Asked by 4 years ago
1while true do
2wait(3)
3game.Players["Player1"].Character.PrimaryPart.CFrame = workspace.DestinationBlock.CFrame
4end

My thoughts: Please no TweenService, I'm hoping for a different method.

I'm afraid that's as much as I know to do in this case, which instantly teleports you inside the DestinationBlock every 3 seconds.

What I want: The player to teleport, from their current position, 25 studs closer to the DestinationBlock every 3 seconds until the player is inside the DestinationBlock.

1
Perhaps you should look into .Magitude, thats a way to find the distance (in studs) between two positions, although im not sure how you could apply it to what you doing. generalYURASKO 144 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago

To start off, get the difference between current position and target position. Then split that up into the length and direction, like this:

1local dif = targetPosition - currentPosition
2local length = dif.Magnitude
3local direction = dif.Unit -- Or dif / length

Then we'll create the dif vector again, but now with a length of 25:

1local dif = targetPosition - currentPosition
2local length = dif.Magnitude
3local direction = dif.Unit -- Or dif / length
4 
5local newDif = direction * 25

Add this to the current position, and you'll teleport 25 studs closer each time this piece of code gets executed

1local dif = targetPosition - currentPosition
2local length = dif.Magnitude
3local direction = dif.Unit -- Or dif / length
4 
5local newDif = direction * 25
6currentPosition += 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)

1local dif = targetPosition - currentPosition
2local length = dif.Magnitude
3local direction = dif.Unit -- Or dif / length
4 
5local newDif = direction * math.min(25, length)
6currentPosition += 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.

1local targetReached = false
2repeat
3    local dif = targetPosition - currentPosition
4    local length = dif.Magnitude
5    local direction = dif.Unit -- Or dif / length
6 
7    local newDif = direction * math.min(25, length)
8    currentPosition += newDif
9until targetReached

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.

01local targetReached = false
02repeat
03    local dif = targetPosition - currentPosition
04    local length = dif.Magnitude
05    local direction = dif.Unit -- Or dif / length
06 
07    local newDif = direction * math.min(25, length)
08    currentPosition += newDif
09 
10    if length <= 25 then
11        targetReached = true
12    end
13until targetReached

With this all done, you only need to add a wait(3) inside the loop so you don't get teleported instantly.

01local targetReached = false
02local targetPosition = ... -- Set targetPosition to whatever you desire
03repeat
04    wait(3)
05    local dif = targetPosition - currentPosition
06    local length = dif.Magnitude
07    local direction = dif.Unit -- Or dif / length
08 
09    local newDif = direction * math.min(25, length)
10    currentPosition += newDif
11 
12    if length <= 25 then
13        targetReached = true
14    end
15 
16    -- Add code here for setting a player's CFrame to CFrame.new(currentPosition)
17until targetReached

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.

Ad

Answer this question