Teleport a player 5 stud in front of a player

Here is my current teleportation code.

local teleporttohrp = game.Workspace:FindFirstChild(shouldberealname):FindFirstChild("HumanoidRootPart")
local shouldteleporthrp = game.Workspace:FindFirstChild(plr.Name):FindFirstChild("HumanoidRootPart")

shouldteleporthrp.CFrame = CFrame.new(teleporttohrp.Position + Vector3.new(5, 0, 0), teleporttohrp.Position)
print("Teleported!")

I did my research but I still have no idea how I could teleport the player 5 stud in front of the target player(depending on where the target is facing) instead of having a fixed Vector3.new(5, 0, 0) value.

2 Likes

maybe this?

hrp.CFrame = CFrame.new(hrp.Position * CFrame.new(0, 0, 5)
2 Likes

Please use CFrame.LookVector.

3 Likes

The code is making my eyes bleed… But anyways, setting the CFrame of the root part is really just a bad way of teleporting a player. Try using character:PivotTo(cframe)

If you wanna teleport the player 5 studs from where they are, you can try this:

character:PivotTo(hrp.CFrame * CFrame.new(0, 0, -5))

2 Likes

This code should do the trick, as @KeegansPerun says, we need to get the LookVector of the player’s CFrame, to represent the front of us.

local teleportTo: Model = game.Workspace:WaitForChild(shouldberealname, 2)
if not teleportTo then
    return
end

local teleportToPivot: CFrame = teleportTo:GetPivot()

local shouldTeleport: Model = game.Workspace:WaitForChild(plr.Name, 2)
if not shouldTeleport then
    return
end

shouldTeleport:PivotTo(teleportToPivot + (teleportToPivot.LookVector * 5))

print("Teleported!")
2 Likes

This won’t always spawn the player in front of them though, the player could be in the rotation of another axis, therefore ‘Z’ won’t be the front of the player, LookVector is a better thing to use in this case.

For future reference, I’ve done a picture (not sure how good it does of explaining it, and I’m no artist) but it should make sense with this.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.