Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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 comment

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.

1 answer

✓ Accepted answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.