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 3 years ago
while true do
wait(3)
game.Players["Player1"].Character.PrimaryPart.CFrame = workspace.DestinationBlock.CFrame
end

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 — 3y

1 answer

Log in to vote
1
Answered by 3 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:

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

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

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

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

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

local newDif = direction * 25
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)

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

local newDif = direction * math.min(25, length)
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.

local targetReached = false
repeat
    local dif = targetPosition - currentPosition
    local length = dif.Magnitude
    local direction = dif.Unit -- Or dif / length

    local newDif = direction * math.min(25, length)
    currentPosition += newDif
until 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.

local targetReached = false
repeat
    local dif = targetPosition - currentPosition
    local length = dif.Magnitude
    local direction = dif.Unit -- Or dif / length

    local newDif = direction * math.min(25, length)
    currentPosition += newDif

    if length <= 25 then
        targetReached = true
    end
until targetReached

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

local targetReached = false
local targetPosition = ... -- Set targetPosition to whatever you desire
repeat
    wait(3)
    local dif = targetPosition - currentPosition
    local length = dif.Magnitude
    local direction = dif.Unit -- Or dif / length

    local newDif = direction * math.min(25, length)
    currentPosition += newDif

    if length <= 25 then
        targetReached = true
    end

    -- Add code here for setting a player's CFrame to CFrame.new(currentPosition)
until 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